|
| 1 | +# New BSD 3-Clause License (https://github.com/Krypton-Suite/Standard-Toolkit/blob/master/LICENSE) |
| 2 | +# Modifications by Peter Wagner (aka Wagnerp), Simon Coghlan (aka Smurf-IV), Giduac, tobitege et al. 2026 - 2026. All rights reserved. |
| 3 | +# |
| 4 | +# Syncs alpha to alpha-backup when alpha has commits in the last 24 hours by creating |
| 5 | +# a PR from alpha into alpha-backup. Creates alpha-backup branch if it does not exist. |
| 6 | +# Enable auto-merge on the repo for these PRs to make the backup fully automated. |
| 7 | +# |
| 8 | +# KILL-SWITCH: To disable this workflow, set repository variable ALPHA_BACKUP_SYNC_DISABLED=true |
| 9 | +# Location: Repository Settings → Secrets and variables → Actions → Variables tab |
| 10 | +# To re-enable: Set ALPHA_BACKUP_SYNC_DISABLED=false or delete the variable |
| 11 | +# |
| 12 | +# Discord: Set secret DISCORD_WEBHOOK_ALPHA_BACKUP to send notifications (optional). |
| 13 | +# |
| 14 | +# Separate repo backup: Set variable BACKUP_REPO (e.g. Krypton-Suite/Standard-Toolkit-Backup) and |
| 15 | +# secret BACKUP_REPO_TOKEN (PAT with push access). Backups go into dated dirs: "Standard Toolkit Backup - YYYY-MM-DD". |
| 16 | +# Optional: BACKUP_DIR_PREFIX (default "Standard Toolkit Backup"), BACKUP_BRANCH (default "main"). |
| 17 | + |
| 18 | +name: Alpha to Alpha-Backup Sync |
| 19 | + |
| 20 | +on: |
| 21 | + schedule: |
| 22 | + # Run at midnight UTC daily |
| 23 | + - cron: '0 0 * * *' |
| 24 | + workflow_dispatch: |
| 25 | + |
| 26 | +permissions: |
| 27 | + contents: write |
| 28 | + pull-requests: write |
| 29 | + |
| 30 | +jobs: |
| 31 | + sync: |
| 32 | + runs-on: ubuntu-latest |
| 33 | + steps: |
| 34 | + - name: Alpha Backup Sync Kill Switch Check |
| 35 | + id: kill_switch |
| 36 | + run: | |
| 37 | + disabled="${{ vars.ALPHA_BACKUP_SYNC_DISABLED }}" |
| 38 | + if [ "$disabled" = "true" ]; then |
| 39 | + echo "::warning:: Alpha Backup Sync workflow is currently DISABLED via kill switch (ALPHA_BACKUP_SYNC_DISABLED=true)" |
| 40 | + echo "To re-enable: Go to Repository Settings -> Secrets and Variables -> Actions -> Variables -> Set ALPHA_BACKUP_SYNC_DISABLED to 'false'." |
| 41 | + echo "enabled=false" >> "$GITHUB_OUTPUT" |
| 42 | + else |
| 43 | + echo "Alpha Backup Sync kill switch check has passed, continuing..." |
| 44 | + echo "enabled=true" >> "$GITHUB_OUTPUT" |
| 45 | + fi |
| 46 | +
|
| 47 | + - name: Checkout |
| 48 | + if: steps.kill_switch.outputs.enabled == 'true' |
| 49 | + uses: actions/checkout@v6 |
| 50 | + with: |
| 51 | + ref: alpha |
| 52 | + fetch-depth: 0 |
| 53 | + |
| 54 | + - name: Check for changes on alpha in last 24 hours |
| 55 | + if: steps.kill_switch.outputs.enabled == 'true' |
| 56 | + id: check_changes |
| 57 | + run: | |
| 58 | + yesterday=$(date -u -d '24 hours ago' '+%Y-%m-%d %H:%M:%S') |
| 59 | + commit_count=$(git rev-list --count --since="$yesterday" alpha 2>/dev/null || echo "0") |
| 60 | + echo "Commits on alpha in last 24 hours: $commit_count" |
| 61 | + if [ "${commit_count:-0}" -gt 0 ]; then |
| 62 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 63 | + else |
| 64 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 65 | + fi |
| 66 | +
|
| 67 | + - name: Ensure alpha-backup exists |
| 68 | + if: steps.kill_switch.outputs.enabled == 'true' && steps.check_changes.outputs.has_changes == 'true' |
| 69 | + uses: actions/github-script@v8 |
| 70 | + id: ensure_backup |
| 71 | + with: |
| 72 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + script: | |
| 74 | + const { owner, repo } = context.repo; |
| 75 | + try { |
| 76 | + await github.rest.repos.getBranch({ owner, repo, branch: 'alpha-backup' }); |
| 77 | + console.log('Branch alpha-backup already exists.'); |
| 78 | + return 'exists'; |
| 79 | + } catch (err) { |
| 80 | + if (err.status !== 404) throw err; |
| 81 | + } |
| 82 | + const { data: ref } = await github.rest.git.getRef({ owner, repo, ref: 'heads/alpha' }); |
| 83 | + await github.rest.git.createRef({ |
| 84 | + owner, |
| 85 | + repo, |
| 86 | + ref: 'refs/heads/alpha-backup', |
| 87 | + sha: ref.object.sha |
| 88 | + }); |
| 89 | + console.log('Created branch alpha-backup from alpha.'); |
| 90 | + return 'created'; |
| 91 | +
|
| 92 | + - name: Create PR from alpha to alpha-backup |
| 93 | + if: steps.kill_switch.outputs.enabled == 'true' && steps.check_changes.outputs.has_changes == 'true' |
| 94 | + id: create_pr |
| 95 | + uses: actions/github-script@v8 |
| 96 | + with: |
| 97 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + script: | |
| 99 | + const { owner, repo } = context.repo; |
| 100 | + let result = { pr_created: false, pr_number: '', pr_url: '', pr_node_id: '' }; |
| 101 | + const { data: compare } = await github.rest.repos.compareCommitsWithBasehead({ |
| 102 | + owner, |
| 103 | + repo, |
| 104 | + basehead: 'alpha-backup...alpha' |
| 105 | + }); |
| 106 | + if (compare.ahead_by === 0) { |
| 107 | + console.log('alpha-backup is already up to date with alpha; no PR needed.'); |
| 108 | + return result; |
| 109 | + } |
| 110 | + const { data: prs } = await github.rest.pulls.list({ |
| 111 | + owner, |
| 112 | + repo, |
| 113 | + state: 'open', |
| 114 | + head: `${owner}:alpha`, |
| 115 | + base: 'alpha-backup' |
| 116 | + }); |
| 117 | + if (prs.length > 0) { |
| 118 | + console.log('Open PR from alpha to alpha-backup already exists: #' + prs[0].number); |
| 119 | + result.pr_number = prs[0].number; |
| 120 | + result.pr_url = prs[0].html_url; |
| 121 | + result.pr_node_id = prs[0].node_id; |
| 122 | + return result; |
| 123 | + } |
| 124 | + const { data: pr } = await github.rest.pulls.create({ |
| 125 | + owner, |
| 126 | + repo, |
| 127 | + title: 'chore: sync alpha → alpha-backup (automated)', |
| 128 | + head: 'alpha', |
| 129 | + base: 'alpha-backup', |
| 130 | + body: 'Automated PR: alpha has had commits in the last 24 hours. Merge to update alpha-backup.\n\n*Triggered by Alpha to Alpha-Backup Sync workflow.*' |
| 131 | + }); |
| 132 | + console.log('Created PR #' + pr.number + ' from alpha to alpha-backup.'); |
| 133 | + result.pr_created = true; |
| 134 | + result.pr_number = pr.number; |
| 135 | + result.pr_url = pr.html_url; |
| 136 | + result.pr_node_id = pr.node_id; |
| 137 | + return result; |
| 138 | +
|
| 139 | + - name: Enable auto-merge on PR |
| 140 | + if: steps.kill_switch.outputs.enabled == 'true' && steps.check_changes.outputs.has_changes == 'true' |
| 141 | + uses: actions/github-script@v8 |
| 142 | + env: |
| 143 | + PR_NODE_ID: ${{ fromJson(steps.create_pr.outputs.result).pr_node_id }} |
| 144 | + with: |
| 145 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 146 | + script: | |
| 147 | + const nodeId = process.env.PR_NODE_ID; |
| 148 | + if (!nodeId) { |
| 149 | + console.log('No PR to enable auto-merge on.'); |
| 150 | + return; |
| 151 | + } |
| 152 | + try { |
| 153 | + await github.graphql(` |
| 154 | + mutation($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod) { |
| 155 | + enablePullRequestAutoMerge(input: { pullRequestId: $pullRequestId, mergeMethod: $mergeMethod }) { |
| 156 | + pullRequest { autoMergeRequest { enabledAt } } |
| 157 | + } |
| 158 | + } |
| 159 | + `, { pullRequestId: nodeId, mergeMethod: 'MERGE' }); |
| 160 | + console.log('Auto-merge enabled on PR.'); |
| 161 | + } catch (err) { |
| 162 | + console.warn('Could not enable auto-merge (repo may not have Allow auto-merge enabled):', err.message); |
| 163 | + } |
| 164 | +
|
| 165 | + - name: Push alpha to backup repository (dated directory) |
| 166 | + if: steps.kill_switch.outputs.enabled == 'true' && steps.check_changes.outputs.has_changes == 'true' |
| 167 | + id: backup_repo |
| 168 | + env: |
| 169 | + BACKUP_REPO: ${{ vars.BACKUP_REPO }} |
| 170 | + BACKUP_REPO_TOKEN: ${{ secrets.BACKUP_REPO_TOKEN }} |
| 171 | + BACKUP_DIR_PREFIX: ${{ vars.BACKUP_DIR_PREFIX }} |
| 172 | + BACKUP_BRANCH: ${{ vars.BACKUP_BRANCH }} |
| 173 | + run: | |
| 174 | + if [ -z "$BACKUP_REPO_TOKEN" ] || [ -z "$BACKUP_REPO" ]; then |
| 175 | + echo "BACKUP_REPO (variable) or BACKUP_REPO_TOKEN (secret) not set - skipping backup repo push" |
| 176 | + echo "pushed=false" >> "$GITHUB_OUTPUT" |
| 177 | + exit 0 |
| 178 | + fi |
| 179 | + prefix="${BACKUP_DIR_PREFIX:-Standard Toolkit Backup}" |
| 180 | + branch="${BACKUP_BRANCH:-main}" |
| 181 | + today=$(date -u +%Y-%m-%d) |
| 182 | + dir_name="$prefix - $today" |
| 183 | + echo "dir_name=$dir_name" >> "$GITHUB_OUTPUT" |
| 184 | + git config user.name "github-actions[bot]" |
| 185 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 186 | + git clone --depth 1 "https://x-access-token:$BACKUP_REPO_TOKEN@github.com/$BACKUP_REPO.git" backup-repo |
| 187 | + cd backup-repo |
| 188 | + git checkout -b "$branch" 2>/dev/null || git checkout "$branch" 2>/dev/null || true |
| 189 | + mkdir -p "$dir_name" |
| 190 | + rsync -a --exclude='.git' ../. "$dir_name/" |
| 191 | + git add "$dir_name" |
| 192 | + git commit -m "chore: backup alpha to $dir_name (automated)" |
| 193 | + git push origin "$branch" |
| 194 | + echo "pushed=true" >> "$GITHUB_OUTPUT" |
| 195 | + echo "Pushed alpha to $BACKUP_REPO ($dir_name)" |
| 196 | +
|
| 197 | + - name: Discord notification |
| 198 | + if: steps.kill_switch.outputs.enabled == 'true' && steps.check_changes.outputs.has_changes == 'true' |
| 199 | + env: |
| 200 | + DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_ALPHA_BACKUP }} |
| 201 | + BRANCH_CREATED: ${{ steps.ensure_backup.outputs.result == 'created' }} |
| 202 | + PR_RESULT: ${{ steps.create_pr.outputs.result }} |
| 203 | + BACKUP_PUSHED: ${{ steps.backup_repo.outputs.pushed }} |
| 204 | + BACKUP_REPO: ${{ vars.BACKUP_REPO }} |
| 205 | + BACKUP_DIR: ${{ steps.backup_repo.outputs.dir_name }} |
| 206 | + run: | |
| 207 | + if [ -z "$DISCORD_WEBHOOK" ]; then |
| 208 | + echo "DISCORD_WEBHOOK_ALPHA_BACKUP not set - skipping Discord notification" |
| 209 | + exit 0 |
| 210 | + fi |
| 211 | + pr_link="" |
| 212 | + if [ -n "$PR_RESULT" ]; then |
| 213 | + pr_url=$(echo "$PR_RESULT" | jq -r '.pr_url // empty') |
| 214 | + pr_number=$(echo "$PR_RESULT" | jq -r '.pr_number // empty') |
| 215 | + if [ -n "$pr_url" ]; then |
| 216 | + pr_link="\n• [PR #$pr_number]($pr_url)" |
| 217 | + fi |
| 218 | + fi |
| 219 | + backup_line="" |
| 220 | + if [ "$BACKUP_PUSHED" = "true" ] && [ -n "$BACKUP_REPO" ]; then |
| 221 | + backup_line="\n• Pushed to backup repo: $BACKUP_REPO" |
| 222 | + if [ -n "$BACKUP_DIR" ]; then |
| 223 | + backup_line="$backup_line ($BACKUP_DIR)" |
| 224 | + fi |
| 225 | + fi |
| 226 | + branch_msg="alpha-backup branch already existed" |
| 227 | + if [ "$BRANCH_CREATED" = "true" ]; then |
| 228 | + branch_msg="alpha-backup branch was created (did not exist)" |
| 229 | + fi |
| 230 | + payload=$(jq -n \ |
| 231 | + --arg branch "$branch_msg" \ |
| 232 | + --arg pr_link "$pr_link" \ |
| 233 | + --arg backup_line "$backup_line" \ |
| 234 | + --arg run_url "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" \ |
| 235 | + '{ |
| 236 | + embeds: [{ |
| 237 | + title: "📦 Alpha → Alpha-Backup Sync", |
| 238 | + description: ("Alpha had commits in the last 24 hours. Sync workflow ran.\n\n• " + $branch + $pr_link + $backup_line + "\n• [Workflow run](" + $run_url + ")"), |
| 239 | + color: 3447003, |
| 240 | + timestamp: (now | todate), |
| 241 | + footer: { text: "Alpha Backup Sync" } |
| 242 | + }] |
| 243 | + }') |
| 244 | + curl -sS -X POST "$DISCORD_WEBHOOK" -H "Content-Type: application/json" -d "$payload" |
0 commit comments