Skip to content

Sync Next Branch

Sync Next Branch #5

name: Sync Next Branch
on:
schedule:
# Run daily at 9:00 AM UTC (6:00 AM EST, 3:00 AM PST)
- cron: '0 9 * * *'
workflow_dispatch:
# Allow manual triggering
permissions:
contents: write
pull-requests: write
jobs:
check-and-sync:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Check branch status
id: branch-status
run: |
echo "Checking if next branch is up-to-date with main..."
# Fetch latest branches
git fetch origin main
git fetch origin next
# Check if next is behind main
BEHIND_COUNT=$(git rev-list --count origin/next..origin/main)
AHEAD_COUNT=$(git rev-list --count origin/main..origin/next)
echo "Next branch is ${AHEAD_COUNT} commits ahead of main"
echo "Next branch is ${BEHIND_COUNT} commits behind main"
echo "behind-count=${BEHIND_COUNT}" >> $GITHUB_OUTPUT
echo "ahead-count=${AHEAD_COUNT}" >> $GITHUB_OUTPUT
if [ "$BEHIND_COUNT" -gt 0 ]; then
echo "needs-sync=true" >> $GITHUB_OUTPUT
echo "πŸ”„ Next branch needs to be synced (${BEHIND_COUNT} commits behind)"
else
echo "needs-sync=false" >> $GITHUB_OUTPUT
echo "βœ… Next branch is up-to-date with main"
fi
- name: Check for existing sync PR
id: existing-pr
if: steps.branch-status.outputs.needs-sync == 'true'
run: |
# Check if there's already an open PR from main to next for syncing
EXISTING_PR=$(gh pr list \
--base next \
--head main \
--state open \
--json number,title \
--jq '.[] | select(.title | test("^(Sync|Update) next branch")) | .number')
if [ -n "$EXISTING_PR" ]; then
echo "existing-pr=${EXISTING_PR}" >> $GITHUB_OUTPUT
echo "⚠️ Sync PR already exists: #${EXISTING_PR}"
echo "has-existing-pr=true" >> $GITHUB_OUTPUT
else
echo "has-existing-pr=false" >> $GITHUB_OUTPUT
echo "No existing sync PR found"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Provide sync instructions
id: sync-instructions
if: steps.branch-status.outputs.needs-sync == 'true' && steps.existing-pr.outputs.has-existing-pr == 'false'
run: |
BEHIND_COUNT="${{ steps.branch-status.outputs.behind-count }}"
AHEAD_COUNT="${{ steps.branch-status.outputs.ahead-count }}"
echo "action-needed=true" >> $GITHUB_OUTPUT
echo "πŸ”„ Next branch needs syncing (${BEHIND_COUNT} commits behind main)"
echo "οΏ½ Manual PR creation required due to organization policies"
- name: Job Summary
if: always()
run: |
BEHIND_COUNT="${{ steps.branch-status.outputs.behind-count }}"
AHEAD_COUNT="${{ steps.branch-status.outputs.ahead-count }}"
NEEDS_SYNC="${{ steps.branch-status.outputs.needs-sync }}"
HAS_EXISTING_PR="${{ steps.existing-pr.outputs.has-existing-pr }}"
EXISTING_PR="${{ steps.existing-pr.outputs.existing-pr }}"
ACTION_NEEDED="${{ steps.sync-instructions.outputs.action-needed }}"
cat << EOF >> $GITHUB_STEP_SUMMARY
# πŸ”„ Branch Sync Status
## Current Status:
- **Next branch**: ${AHEAD_COUNT} commits ahead, ${BEHIND_COUNT} commits behind main
- **Needs sync**: ${NEEDS_SYNC}
EOF
if [ "$NEEDS_SYNC" = "true" ]; then
if [ "$HAS_EXISTING_PR" = "true" ]; then
cat << EOF >> $GITHUB_STEP_SUMMARY
## ⚠️ Action Required:
There is already an existing sync PR: [#${EXISTING_PR}](https://github.com/${{ github.repository }}/pull/${EXISTING_PR})
Please review and merge the existing PR to sync the next branch.
EOF
elif [ "$ACTION_NEEDED" = "true" ]; then
cat << EOF >> $GITHUB_STEP_SUMMARY
## πŸ“ Manual Action Required:
The \`next\` branch is ${BEHIND_COUNT} commits behind \`main\` and needs to be synced.
**Please create a pull request manually:**
1. 🌐 [Create PR: main β†’ next](https://github.com/${{ github.repository }}/compare/next...main)
2. πŸ“ Use title: **"Sync next branch with main"**
3. πŸ“„ Use this description:
\`\`\`markdown
## πŸ”„ Branch Sync
This PR syncs the \`next\` branch with the latest changes from \`main\`.
### Status:
- **Behind main**: ${BEHIND_COUNT} commits
- **Ahead of main**: ${AHEAD_COUNT} commits
### What to do:
1. πŸ” Review the changes in this PR
2. βœ… Ensure all checks pass
3. πŸ”€ Merge this PR to sync the \`next\` branch
4. πŸ—‘οΈ The \`next\` branch will then be ready for new development
\`\`\`
4. 🏷️ Add labels: \`automated\`, \`sync\`
5. βœ… Review and merge when ready
EOF
fi
else
cat << EOF >> $GITHUB_STEP_SUMMARY
## βœ… All Good!
The next branch is up-to-date with main. No action needed.
EOF
fi