-
Notifications
You must be signed in to change notification settings - Fork 4.2k
290 lines (251 loc) · 13.1 KB
/
backport.yml
File metadata and controls
290 lines (251 loc) · 13.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Backport pull requests to the next-older release branch by adding
# a "backport" label. Works on PRs against `main` (targets the latest
# release-v* branch) and on PRs against `release-v*` branches
# (targets the next-older release-v* branch).
name: Backport
on:
pull_request_target:
types: [closed, labeled]
branches:
- main
- release-v*
concurrency: ${{ github.workflow }}-${{ github.event.pull_request.number }}
permissions:
contents: read
pull-requests: write
jobs:
find-branch:
name: Find target release branch
runs-on: ubuntu-latest
timeout-minutes: 5
if: |
github.repository_owner == 'vercel' &&
github.event.pull_request.merged == true &&
contains(github.event.pull_request.labels.*.name, 'backport')
outputs:
release-branch: ${{ steps.find-target.outputs.release-branch }}
steps:
- name: Checkout Repository
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Find target release branch
id: find-target
run: |
git fetch --all
BASE_REF="${{ github.event.pull_request.base.ref }}"
RELEASE_BRANCHES=$(git branch -r | grep -E 'origin/release-v[0-9]+\.[0-9]+$' | sed 's/.*origin\///' | sort -V)
if [ -z "$RELEASE_BRANCHES" ]; then
echo "::error::No release branches found matching pattern release-vX.Y"
exit 1
fi
echo "Found release branches: $RELEASE_BRANCHES"
if [ "$BASE_REF" = "main" ]; then
TARGET=$(echo "$RELEASE_BRANCHES" | tail -n 1)
else
TARGET=$(echo "$RELEASE_BRANCHES" | grep -B1 "^${BASE_REF}$" | head -n 1)
if [ "$TARGET" = "$BASE_REF" ] || [ -z "$TARGET" ]; then
echo "::error::No older release branch found before $BASE_REF"
exit 1
fi
fi
echo "Target release branch: $TARGET"
echo "release-branch=$TARGET" >> "$GITHUB_OUTPUT"
backport:
name: Backport to ${{ needs.find-branch.outputs.release-branch }}
runs-on: ubuntu-latest
timeout-minutes: 10
needs: find-branch
if: |
github.repository_owner == 'vercel' &&
github.event.pull_request.merged == true &&
contains(github.event.pull_request.labels.*.name, 'backport')
steps:
- name: Create access token for GitHub App
uses: actions/create-github-app-token@v2
id: app-token
with:
app-id: ${{ vars.VERCEL_AI_SDK_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.VERCEL_AI_SDK_GITHUB_APP_PRIVATE_KEY_PKCS8 }}
- name: Get GitHub App User ID
id: app-user-id
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Configure git user for GitHub App
run: |
git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
git config --global user.email '${{ steps.app-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'
- name: Check for existing backport PR
id: check-existing
run: |
BACKPORT_BRANCH="backport-pr-${{ github.event.pull_request.number }}-to-${{ needs.find-branch.outputs.release-branch }}"
EXISTING_PR=$(gh api "repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${BACKPORT_BRANCH}&state=open" --jq '.[0].html_url // empty')
if [ -n "$EXISTING_PR" ]; then
echo "Backport PR already exists: $EXISTING_PR — skipping."
echo "existing-pr=$EXISTING_PR" >> "$GITHUB_OUTPUT"
fi
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Remove backport label (already backported)
if: steps.check-existing.outputs.existing-pr
run: |
gh pr edit ${{ github.event.pull_request.number }} --remove-label backport || true
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Checkout Repository
if: '!steps.check-existing.outputs.existing-pr'
uses: actions/checkout@v5
with:
fetch-depth: 0
token: ${{ steps.app-token.outputs.token }}
ref: ${{ needs.find-branch.outputs.release-branch }}
- name: Create backport branch
if: '!steps.check-existing.outputs.existing-pr'
run: |
# Create a new branch from the latest release branch for the backport
git checkout -b backport-pr-${{ github.event.pull_request.number }}-to-${{ needs.find-branch.outputs.release-branch }} origin/${{ needs.find-branch.outputs.release-branch }}
- name: Cherry-pick commits
if: '!steps.check-existing.outputs.existing-pr'
id: cherry-pick
run: |
# Get the merge commit hash
MERGE_COMMIT="${{ github.event.pull_request.merge_commit_sha }}"
# Cherry-pick the merge commit and capture output
CHERRY_PICK_OUTPUT=$(git cherry-pick -m 1 "$MERGE_COMMIT" 2>&1) || CHERRY_PICK_EXIT_CODE=$?
echo "$CHERRY_PICK_OUTPUT"
echo "git-output<<EOF" >> "$GITHUB_OUTPUT"
echo "$CHERRY_PICK_OUTPUT" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
if [ "${CHERRY_PICK_EXIT_CODE:-0}" -ne 0 ]; then
echo "Cherry-pick failed. This backport requires manual intervention."
echo "::error::Failed to cherry-pick merge commit $MERGE_COMMIT to ${{ needs.find-branch.outputs.release-branch }} branch"
echo "has-conflicts=true" >> "$GITHUB_OUTPUT"
else
echo "has-conflicts=false" >> "$GITHUB_OUTPUT"
fi
- name: Remap example paths for release-v5.0
if: "!steps.check-existing.outputs.existing-pr && needs.find-branch.outputs.release-branch == 'release-v5.0' && steps.cherry-pick.outputs.has-conflicts == 'false'"
run: |
# On release-v5.0, examples are in examples/ai-core/ but on main they are in examples/ai-functions/
# If the cherry-pick created examples/ai-functions/, move the files to examples/ai-core/
if [ -d "examples/ai-functions" ]; then
echo "Found examples/ai-functions directory, remapping to examples/ai-core..."
# Move all files from ai-functions to ai-core using git mv
for file in $(find examples/ai-functions -type f); do
target="${file/examples\/ai-functions/examples/ai-core}"
mkdir -p "$(dirname "$target")"
git mv "$file" "$target"
done
# Remove the now-empty ai-functions directory
rm -rf examples/ai-functions
# Amend the cherry-pick commit with the corrected paths
git add .
git commit --amend --no-edit
echo "Successfully remapped example paths from examples/ai-functions/ to examples/ai-core/"
else
echo "No examples/ai-functions directory found, skipping remap"
fi
- name: Commit changes in case of errors
if: "!steps.check-existing.outputs.existing-pr && steps.cherry-pick.outputs.has-conflicts == 'true'"
run: |
# In case of failure, commit the conflicts to allow inspection
git add .
git commit -m "Backport conflicts for PR #${{ github.event.pull_request.number }} to ${{ needs.find-branch.outputs.release-branch }}"
- name: Push backport branch
if: '!steps.check-existing.outputs.existing-pr'
run: |
BACKPORT_BRANCH="backport-pr-${{ github.event.pull_request.number }}-to-${{ needs.find-branch.outputs.release-branch }}"
if git ls-remote --exit-code origin "refs/heads/${BACKPORT_BRANCH}" > /dev/null 2>&1; then
echo "Branch ${BACKPORT_BRANCH} already exists on remote (orphaned from a previous run). Force-pushing."
git push --force-with-lease origin "${BACKPORT_BRANCH}"
else
git push origin "${BACKPORT_BRANCH}"
fi
- name: Determine PR assignee
if: '!steps.check-existing.outputs.existing-pr'
id: assignee
run: |
if [ "$MERGED_BY_TYPE" = "User" ]; then
echo "login=$MERGED_BY_LOGIN" >> "$GITHUB_OUTPUT"
elif [ "$PR_AUTHOR_TYPE" = "User" ]; then
echo "login=$PR_AUTHOR_LOGIN" >> "$GITHUB_OUTPUT"
else
REVIEWER=$(gh api "repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews" \
--jq '[.[] | select(.user.type == "User")] | last | .user.login // empty')
if [ -n "$REVIEWER" ]; then
echo "login=$REVIEWER" >> "$GITHUB_OUTPUT"
fi
fi
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
MERGED_BY_TYPE: ${{ github.event.pull_request.merged_by.type }}
MERGED_BY_LOGIN: ${{ github.event.pull_request.merged_by.login }}
PR_AUTHOR_TYPE: ${{ github.event.pull_request.user.type }}
PR_AUTHOR_LOGIN: ${{ github.event.pull_request.user.login }}
- name: Create backport pull request
if: '!steps.check-existing.outputs.existing-pr'
id: create-pr
run: |
ASSIGNEE_FLAG=""
if [ -n "$ASSIGNEE_LOGIN" ]; then
ASSIGNEE_FLAG="--assignee $ASSIGNEE_LOGIN"
fi
# Create the backport PR
if [ "${{ steps.cherry-pick.outputs.has-conflicts }}" = "true" ]; then
PR_URL=$(gh pr create \
--title "$PR_TITLE" \
--body "$PR_BODY_CONFLICTS" \
--base ${{ needs.find-branch.outputs.release-branch }} \
--head backport-pr-${{ github.event.pull_request.number }}-to-${{ needs.find-branch.outputs.release-branch }} \
$ASSIGNEE_FLAG \
--draft)
else
PR_URL=$(gh pr create \
--title "$PR_TITLE" \
--body "$PR_BODY_NO_CONFLICTS" \
--base ${{ needs.find-branch.outputs.release-branch }} \
--head backport-pr-${{ github.event.pull_request.number }}-to-${{ needs.find-branch.outputs.release-branch }} \
$ASSIGNEE_FLAG)
fi
echo "backport-pr-url=$PR_URL" >> "$GITHUB_OUTPUT"
gh pr merge "$PR_URL" --auto --squash || echo "Auto-merge could not be enabled"
echo "Created backport PR $PR_URL"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
ASSIGNEE_LOGIN: ${{ steps.assignee.outputs.login }}
PR_TITLE: 'Backport: ${{ github.event.pull_request.title }}'
PR_BODY_NO_CONFLICTS: 'This is an automated backport of #${{ github.event.pull_request.number }} to the ${{ needs.find-branch.outputs.release-branch }} branch. FYI @${{ github.event.pull_request.user.login }}'
PR_BODY_CONFLICTS: |
This is an automated backport of #${{ github.event.pull_request.number }} to the ${{ needs.find-branch.outputs.release-branch }} branch. FYI @${{ github.event.pull_request.user.login }}
This backport has conflicts that need to be resolved manually.
### `git cherry-pick` output
```
${{ steps.cherry-pick.outputs.git-output }}
```
- name: Remove backport label from original PR
if: '!steps.check-existing.outputs.existing-pr && steps.create-pr.outputs.backport-pr-url'
run: |
# Remove the backport label from the original PR
gh pr edit ${{ github.event.pull_request.number }} --remove-label backport
echo "Removed backport label from original PR #${{ github.event.pull_request.number }}"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Success Comment on original PR
if: "!steps.check-existing.outputs.existing-pr && steps.cherry-pick.outputs.has-conflicts == 'false' && steps.create-pr.outputs.backport-pr-url"
run: |
gh pr comment ${{ github.event.pull_request.number }} --body "✅ Backport PR created: ${{ steps.create-pr.outputs.backport-pr-url }}"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Error Comment on original PR
if: "!steps.check-existing.outputs.existing-pr && steps.cherry-pick.outputs.has-conflicts == 'true' && steps.create-pr.outputs.backport-pr-url"
run: |
gh pr comment ${{ github.event.pull_request.number }} --body "⚠️ Backport to ${{ needs.find-branch.outputs.release-branch }} created but has conflicts: ${{ steps.create-pr.outputs.backport-pr-url }}"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Pull request failure Comment on original PR
if: "!steps.check-existing.outputs.existing-pr && steps.cherry-pick.outputs.has-conflicts == 'true' && !steps.create-pr.outputs.backport-pr-url"
run: |
gh pr comment ${{ github.event.pull_request.number }} --body "❌ Backport to ${{ needs.find-branch.outputs.release-branch }} failed. This backport requires manual intervention. [View workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}