|
| 1 | +name:Sync Next Branch |
| 2 | + |
| 3 | +on: |
| 4 | +schedule: |
| 5 | +# Run daily at 9:00 AM UTC (6:00 AM EST, 3:00 AM PST) |
| 6 | + -cron:'0 9 * * *' |
| 7 | +workflow_dispatch: |
| 8 | +# Allow manual triggering |
| 9 | + |
| 10 | +permissions: |
| 11 | +contents:write |
| 12 | +pull-requests:write |
| 13 | + |
| 14 | +jobs: |
| 15 | +check-and-sync: |
| 16 | +runs-on:ubuntu-latest |
| 17 | + |
| 18 | +steps: |
| 19 | + -name:Checkout repository |
| 20 | +uses:actions/checkout@v4 |
| 21 | +with: |
| 22 | +fetch-depth:0 |
| 23 | +token:${{ secrets.GITHUB_TOKEN }} |
| 24 | + |
| 25 | + -name:Configure Git |
| 26 | +run:| |
| 27 | + git config user.name "github-actions[bot]" |
| 28 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 29 | +
|
| 30 | + -name:Check branch status |
| 31 | +id:branch-status |
| 32 | +run:| |
| 33 | + echo "Checking if next branch is up-to-date with main..." |
| 34 | +
|
| 35 | + # Fetch latest branches |
| 36 | + git fetch origin main |
| 37 | + git fetch origin next |
| 38 | +
|
| 39 | + # Check if next is behind main |
| 40 | + BEHIND_COUNT=$(git rev-list --count origin/next..origin/main) |
| 41 | + AHEAD_COUNT=$(git rev-list --count origin/main..origin/next) |
| 42 | +
|
| 43 | + echo "Next branch is ${AHEAD_COUNT} commits ahead of main" |
| 44 | + echo "Next branch is ${BEHIND_COUNT} commits behind main" |
| 45 | +
|
| 46 | + echo "behind-count=${BEHIND_COUNT}" >> $GITHUB_OUTPUT |
| 47 | + echo "ahead-count=${AHEAD_COUNT}" >> $GITHUB_OUTPUT |
| 48 | +
|
| 49 | + if [ "$BEHIND_COUNT" -gt 0 ]; then |
| 50 | + echo "needs-sync=true" >> $GITHUB_OUTPUT |
| 51 | + echo "🔄 Next branch needs to be synced (${BEHIND_COUNT} commits behind)" |
| 52 | + else |
| 53 | + echo "needs-sync=false" >> $GITHUB_OUTPUT |
| 54 | + echo "✅ Next branch is up-to-date with main" |
| 55 | + fi |
| 56 | +
|
| 57 | + -name:Check for existing sync PR |
| 58 | +id:existing-pr |
| 59 | +if:steps.branch-status.outputs.needs-sync == 'true' |
| 60 | +run:| |
| 61 | + # Check if there's already an open PR from main to next for syncing |
| 62 | + EXISTING_PR=$(gh pr list \ |
| 63 | + --base next \ |
| 64 | + --head main \ |
| 65 | + --state open \ |
| 66 | + --json number,title \ |
| 67 | + --jq '.[] | select(.title | test("^(Sync|Update) next branch")) | .number') |
| 68 | +
|
| 69 | + if [ -n "$EXISTING_PR" ]; then |
| 70 | + echo "existing-pr=${EXISTING_PR}" >> $GITHUB_OUTPUT |
| 71 | + echo "⚠️ Sync PR already exists: #${EXISTING_PR}" |
| 72 | + echo "has-existing-pr=true" >> $GITHUB_OUTPUT |
| 73 | + else |
| 74 | + echo "has-existing-pr=false" >> $GITHUB_OUTPUT |
| 75 | + echo "No existing sync PR found" |
| 76 | + fi |
| 77 | +env: |
| 78 | +GH_TOKEN:${{ secrets.GITHUB_TOKEN }} |
| 79 | + |
| 80 | + -name:Create sync PR |
| 81 | +id:create-sync-pr |
| 82 | +if:steps.branch-status.outputs.needs-sync == 'true' && steps.existing-pr.outputs.has-existing-pr == 'false' |
| 83 | +run:| |
| 84 | + BEHIND_COUNT="${{ steps.branch-status.outputs.behind-count }}" |
| 85 | + AHEAD_COUNT="${{ steps.branch-status.outputs.ahead-count }}" |
| 86 | +
|
| 87 | + # Create PR from main to next |
| 88 | + PR_RESPONSE=$(gh pr create \ |
| 89 | + --base next \ |
| 90 | + --head main \ |
| 91 | + --title "Sync next branch with main" \ |
| 92 | + --body "## 🔄 Automated Branch Sync |
| 93 | +
|
| 94 | + This PR syncs the \`next\` branch with the latest changes from \`main\`. |
| 95 | +
|
| 96 | + ### Status: |
| 97 | + - **Behind main**: ${BEHIND_COUNT} commits |
| 98 | + - **Ahead of main**: ${AHEAD_COUNT} commits |
| 99 | +
|
| 100 | + ### What to do: |
| 101 | + 1. 🔍 Review the changes in this PR |
| 102 | + 2. ✅ Ensure all checks pass |
| 103 | + 3. 🔀 Merge this PR to sync the \`next\` branch |
| 104 | + 4. 🗑️ The \`next\` branch will then be ready for new development |
| 105 | +
|
| 106 | + > **Note**: This PR was automatically created by the daily branch sync workflow. |
| 107 | + > If you have any concerns about these changes, please review them carefully before merging." \ |
| 108 | + --label "automated" \ |
| 109 | + --label "sync" \ |
| 110 | + --json number,url) |
| 111 | +
|
| 112 | + PR_NUMBER=$(echo "$PR_RESPONSE" | jq -r '.number') |
| 113 | + PR_URL=$(echo "$PR_RESPONSE" | jq -r '.url') |
| 114 | +
|
| 115 | + echo "pr-number=${PR_NUMBER}" >> $GITHUB_OUTPUT |
| 116 | + echo "pr-url=${PR_URL}" >> $GITHUB_OUTPUT |
| 117 | +
|
| 118 | + echo "✅ Created sync PR #${PR_NUMBER}: ${PR_URL}" |
| 119 | +env: |
| 120 | +GH_TOKEN:${{ secrets.GITHUB_TOKEN }} |
| 121 | + |
| 122 | + -name:Job Summary |
| 123 | +if:always() |
| 124 | +run:| |
| 125 | + BEHIND_COUNT="${{ steps.branch-status.outputs.behind-count }}" |
| 126 | + AHEAD_COUNT="${{ steps.branch-status.outputs.ahead-count }}" |
| 127 | + NEEDS_SYNC="${{ steps.branch-status.outputs.needs-sync }}" |
| 128 | + HAS_EXISTING_PR="${{ steps.existing-pr.outputs.has-existing-pr }}" |
| 129 | + EXISTING_PR="${{ steps.existing-pr.outputs.existing-pr }}" |
| 130 | + NEW_PR_URL="${{ steps.create-sync-pr.outputs.pr-url }}" |
| 131 | + NEW_PR_NUMBER="${{ steps.create-sync-pr.outputs.pr-number }}" |
| 132 | +
|
| 133 | + cat << EOF >> $GITHUB_STEP_SUMMARY |
| 134 | + # 🔄 Branch Sync Status |
| 135 | +
|
| 136 | + ## Current Status: |
| 137 | + - **Next branch**: ${AHEAD_COUNT} commits ahead, ${BEHIND_COUNT} commits behind main |
| 138 | + - **Needs sync**: ${NEEDS_SYNC} |
| 139 | +
|
| 140 | + EOF |
| 141 | +
|
| 142 | + if [ "$NEEDS_SYNC" = "true" ]; then |
| 143 | + if [ "$HAS_EXISTING_PR" = "true" ]; then |
| 144 | + cat << EOF >> $GITHUB_STEP_SUMMARY |
| 145 | + ## ⚠️ Action Required: |
| 146 | + There is already an existing sync PR: [#${EXISTING_PR}](https://github.com/${{ github.repository }}/pull/${EXISTING_PR}) |
| 147 | +
|
| 148 | + Please review and merge the existing PR to sync the next branch. |
| 149 | + EOF |
| 150 | + elif [ -n "$NEW_PR_NUMBER" ]; then |
| 151 | + cat << EOF >> $GITHUB_STEP_SUMMARY |
| 152 | + ## ✅ Action Taken: |
| 153 | + Created a new sync PR: [#${NEW_PR_NUMBER}](${NEW_PR_URL}) |
| 154 | +
|
| 155 | + **Next steps:** |
| 156 | + 1. Review the changes in the PR |
| 157 | + 2. Ensure all checks pass |
| 158 | + 3. Merge the PR to sync the next branch |
| 159 | + EOF |
| 160 | + fi |
| 161 | + else |
| 162 | + cat << EOF >> $GITHUB_STEP_SUMMARY |
| 163 | + ## ✅ All Good! |
| 164 | + The next branch is up-to-date with main. No action needed. |
| 165 | + EOF |
| 166 | + fi |