-
Notifications
You must be signed in to change notification settings - Fork 4.1k
166 lines (137 loc) Β· 6.02 KB
/
sync-next-branch.yml
File metadata and controls
166 lines (137 loc) Β· 6.02 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
156
157
158
159
160
161
162
163
164
165
166
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: Create sync PR
id: create-sync-pr
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 }}"
# Create PR from main to next
PR_RESPONSE=$(gh pr create \
--base next \
--head main \
--title "Sync next branch with main" \
--body "## π Automated 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
> **Note**: This PR was automatically created by the daily branch sync workflow.
> If you have any concerns about these changes, please review them carefully before merging." \
--label "automated" \
--label "sync" \
--json number,url)
PR_NUMBER=$(echo "$PR_RESPONSE" | jq -r '.number')
PR_URL=$(echo "$PR_RESPONSE" | jq -r '.url')
echo "pr-number=${PR_NUMBER}" >> $GITHUB_OUTPUT
echo "pr-url=${PR_URL}" >> $GITHUB_OUTPUT
echo "β
Created sync PR #${PR_NUMBER}: ${PR_URL}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- 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 }}"
NEW_PR_URL="${{ steps.create-sync-pr.outputs.pr-url }}"
NEW_PR_NUMBER="${{ steps.create-sync-pr.outputs.pr-number }}"
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 [ -n "$NEW_PR_NUMBER" ]; then
cat << EOF >> $GITHUB_STEP_SUMMARY
## β
Action Taken:
Created a new sync PR: [#${NEW_PR_NUMBER}](${NEW_PR_URL})
**Next steps:**
1. Review the changes in the PR
2. Ensure all checks pass
3. Merge the PR to sync the next branch
EOF
fi
else
cat << EOF >> $GITHUB_STEP_SUMMARY
## β
All Good!
The next branch is up-to-date with main. No action needed.
EOF
fi