-
Notifications
You must be signed in to change notification settings - Fork 4.1k
155 lines (126 loc) Β· 5.7 KB
/
sync-next-branch.yml
File metadata and controls
155 lines (126 loc) Β· 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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