-
Notifications
You must be signed in to change notification settings - Fork 0
318 lines (257 loc) · 12.7 KB
/
update-from-template.yml
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
name: Update from Template
# This workflow keeps the repo up to date with changes from the template repo (REMOTE_URL)
# It duplicates the REMOTE_BRANCH (into UPDATE_BRANCH) and tries to merge it into
# this repos default branch (which is checked out here)
# Note that this requires a PAT (Personal Access Token) - at best from a servicing account
# PAT permissions: read:discussion, read:org, repo, workflow
# Also note that you should have at least once merged the template repo into the current repo manually
# otherwise a "refusing to merge unrelated histories" error might occur.
on:
schedule:
- cron: '55 2 * * 1'
workflow_dispatch:
inputs:
no_automatic_merge:
type: boolean
description: 'No automatic merge'
default: false
env:
UPDATE_BRANCH: update-from-template
UPDATE_BRANCH_MERGED: update-from-template-merged
REMOTE_URL: https://github.com/xdev-software/base-template.git
REMOTE_BRANCH: master
permissions:
contents: write
pull-requests: write
jobs:
update:
runs-on: ubuntu-latest
outputs:
update_branch_merged_commit: ${{ steps.manage-branches.outputs.update_branch_merged_commit }}
create_update_branch_merged_pr: ${{ steps.manage-branches.outputs.create_update_branch_merged_pr }}
steps:
- uses: actions/checkout@v4
with:
# Required because otherwise there are always changes detected when executing diff/rev-list
fetch-depth: 0
# If no PAT is used the following error occurs on a push:
# refusing to allow a GitHub App to create or update workflow `.github/workflows/xxx.yml` without `workflows` permission
token: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
- name: Init Git
run: |
git config --global user.email "[email protected]"
git config --global user.name "XDEV Bot"
- name: Manage branches
id: manage-branches
run: |
echo "Adding remote template-repo"
git remote add template ${{ env.REMOTE_URL }}
echo "Fetching remote template repo"
git fetch template
echo "Deleting local branches that will contain the updates - if present"
git branch -D ${{ env.UPDATE_BRANCH }} || true
git branch -D ${{ env.UPDATE_BRANCH_MERGED }} || true
echo "Checking if the remote template repo has new commits"
git rev-list ..template/${{ env.REMOTE_BRANCH }}
if [ $(git rev-list --count ..template/${{ env.REMOTE_BRANCH }}) -eq 0 ]; then
echo "There are no commits new commits on the template repo"
echo "Deleting origin branch(es) that contain the updates - if present"
git push -f origin --delete ${{ env.UPDATE_BRANCH }} || true
git push -f origin --delete ${{ env.UPDATE_BRANCH_MERGED }} || true
echo "create_update_branch_pr=0" >> $GITHUB_OUTPUT
echo "create_update_branch_merged_pr=0" >> $GITHUB_OUTPUT
exit 0
fi
echo "Found new commits on the template repo"
echo "Creating update branch"
git branch ${{ env.UPDATE_BRANCH }} template/${{ env.REMOTE_BRANCH }}
git branch --unset-upstream ${{ env.UPDATE_BRANCH }}
echo "Pushing update branch"
git push -f -u origin ${{ env.UPDATE_BRANCH }}
echo "Getting base branch"
base_branch=$(git branch --show-current)
echo "Base branch is $base_branch"
echo "base_branch=$base_branch" >> $GITHUB_OUTPUT
echo "Trying to create auto-merged branch ${{ env.UPDATE_BRANCH_MERGED }}"
git branch ${{ env.UPDATE_BRANCH_MERGED }} ${{ env.UPDATE_BRANCH }}
git checkout ${{ env.UPDATE_BRANCH_MERGED }}
echo "Merging branch $base_branch into ${{ env.UPDATE_BRANCH_MERGED }}"
git merge $base_branch && merge_exit_code=$? || merge_exit_code=$?
if [ $merge_exit_code -ne 0 ]; then
echo "Auto merge failed! Manual merge required"
echo "::notice ::Auto merge failed - Manual merge required"
echo "Cleaning up failed merge"
git merge --abort
git checkout $base_branch
git branch -D ${{ env.UPDATE_BRANCH_MERGED }} || true
echo "Deleting auto-merge branch - if present"
git push -f origin --delete ${{ env.UPDATE_BRANCH_MERGED }} || true
echo "create_update_branch_pr=1" >> $GITHUB_OUTPUT
echo "create_update_branch_merged_pr=0" >> $GITHUB_OUTPUT
exit 0
fi
echo "Post processing: Trying to automatically fill in template variables"
find . -type f \
-not -path "./.git/**" \
-not -path "./.github/workflows/update-from-template.yml" -print0 \
| xargs -0 sed -i "s/oidc-server-mock/${GITHUB_REPOSITORY#*/}/g"
git status
git add --all
if [[ "$(git status --porcelain)" != "" ]]; then
echo "Filled in template; Committing"
git commit -m "Fill in template"
fi
echo "Pushing auto-merged branch"
git push -f -u origin ${{ env.UPDATE_BRANCH_MERGED }}
echo "update_branch_merged_commit=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
echo "Restoring base branch $base_branch"
git checkout $base_branch
echo "create_update_branch_pr=0" >> $GITHUB_OUTPUT
echo "create_update_branch_merged_pr=1" >> $GITHUB_OUTPUT
echo "try_close_update_branch_pr=1" >> $GITHUB_OUTPUT
- name: Create/Update PR update_branch
if: steps.manage-branches.outputs.create_update_branch_pr == 1
env:
GH_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
run: |
gh_pr_up() {
gh pr create -H "${{ env.UPDATE_BRANCH }}" "$@" || (git checkout "${{ env.UPDATE_BRANCH }}" && gh pr edit "$@")
}
gh_pr_up -B "${{ steps.manage-branches.outputs.base_branch }}" \
--title "Update from template" \
--body "An automated PR to sync changes from the template into this repo"
# Ensure that only a single PR is open (otherwise confusion and spam)
- name: Close PR update_branch
if: steps.manage-branches.outputs.try_close_update_branch_pr == 1
env:
GH_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
run: |
gh pr close "${{ env.UPDATE_BRANCH }}" || true
- name: Create/Update PR update_branch_merged
if: steps.manage-branches.outputs.create_update_branch_merged_pr == 1
env:
GH_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
run: |
gh_pr_up() {
gh pr create -H "${{ env.UPDATE_BRANCH_MERGED }}" "$@" || (git checkout "${{ env.UPDATE_BRANCH_MERGED }}" && gh pr edit "$@")
}
gh_pr_up -B "${{ steps.manage-branches.outputs.base_branch }}" \
--title "Update from template (auto-merged)" \
--body "An automated PR to sync changes from the template into this repo"
# Wait a moment so that checks of PR have higher prio than following job
sleep 3
# Split into two jobs to help with executor starvation
auto-merge:
needs: [update]
if: needs.update.outputs.create_update_branch_merged_pr == 1
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
# Required because otherwise there are always changes detected when executing diff/rev-list
fetch-depth: 0
# If no PAT is used the following error occurs on a push:
# refusing to allow a GitHub App to create or update workflow `.github/workflows/xxx.yml` without `workflows` permission
token: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
- name: Init Git
run: |
git config --global user.email "[email protected]"
git config --global user.name "XDEV Bot"
- name: Checking if auto-merge for PR update_branch_merged can be done
id: auto-merge-check
env:
GH_TOKEN: ${{ secrets.UPDATE_FROM_TEMPLATE_PAT }}
run: |
not_failed_conclusion="skipped|neutral|success"
not_relevant_app_slug="dependabot|github-pages|sonarcloud"
echo "Waiting for checks to start..."
sleep 40s
for i in {1..20}; do
echo "Checking if PR can be auto-merged. Try: $i"
echo "Checking if update-branch-merged exists"
git fetch
if [[ $(git rev-parse origin/${{ env.UPDATE_BRANCH_MERGED }}) ]]; then
echo "Branch still exists; Continuing..."
else
echo "Branch origin/${{ env.UPDATE_BRANCH_MERGED }} is missing"
exit 0
fi
echo "Fetching checks"
cs_response=$(curl -sL \
--fail-with-body \
--connect-timeout 60 \
--max-time 120 \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/commits/${{ needs.update.outputs.update_branch_merged_commit }}/check-suites)
cs_data=$(echo $cs_response | jq '.check_suites[] | { conclusion: .conclusion, slug: .app.slug, check_runs_url: .check_runs_url }')
echo $cs_data
if [[ -z "$cs_data" ]]; then
echo "No check suite data - Assuming that there are no checks to run"
echo "perform=1" >> $GITHUB_OUTPUT
exit 0
fi
cs_failed=$(echo $cs_data | jq --arg x "$not_failed_conclusion" 'select ((.conclusion == null or (.conclusion | test($x))) | not)')
if [[ -z "$cs_failed" ]]; then
echo "No check failed so far; Checking if relevant checks are still running"
cs_relevant_still_running=$(echo $cs_data | jq --arg x "$not_relevant_app_slug" 'select (.conclusion == null and (.slug | test($x) | not))')
if [[ -z $cs_relevant_still_running ]]; then
echo "All relevant checks finished - PR can be merged"
echo "perform=1" >> $GITHUB_OUTPUT
exit 0
else
echo "Relevant checks are still running"
echo $cs_relevant_still_running
fi
else
echo "Detected failed check"
echo $cs_failed
echo "perform=0" >> $GITHUB_OUTPUT
exit 0
fi
echo "Waiting before next run..."
sleep 30s
done
echo "Timed out - Assuming executor starvation - Forcing merge"
echo "perform=1" >> $GITHUB_OUTPUT
- name: Auto-merge update_branch_merged
if: steps.auto-merge-check.outputs.perform == 1
run: |
echo "Getting base branch"
base_branch=$(git branch --show-current)
echo "Base branch is $base_branch"
echo "Fetching..."
git fetch
if [[ $(git rev-parse origin/${{ env.UPDATE_BRANCH_MERGED }}) ]]; then
echo "Branch still exists; Continuing..."
else
echo "Branch origin/${{ env.UPDATE_BRANCH_MERGED }} is missing"
exit 0
fi
expected_commit="${{ needs.update.outputs.update_branch_merged_commit }}"
actual_commit=$(git rev-parse origin/${{ env.UPDATE_BRANCH_MERGED }})
if [[ "$expected_commit" != "$actual_commit" ]]; then
echo "Branch ${{ env.UPDATE_BRANCH_MERGED }} contains unexpected commit $actual_commit"
echo "Expected: $expected_commit"
exit 0
fi
echo "Ensuring that current branch $base_branch is up-to-date"
git pull
echo "Merging origin/${{ env.UPDATE_BRANCH_MERGED }} into $base_branch"
git merge origin/${{ env.UPDATE_BRANCH_MERGED }} && merge_exit_code=$? || merge_exit_code=$?
if [ $merge_exit_code -ne 0 ]; then
echo "Unexpected merge failure $merge_exit_code - Requires manual resolution"
exit 0
fi
if [[ "${{ inputs.no_automatic_merge }}" == "true" ]]; then
echo "Exiting due no_automatic_merge"
exit 0
fi
echo "Pushing"
git push
echo "Cleaning up"
git branch -D ${{ env.UPDATE_BRANCH }} || true
git branch -D ${{ env.UPDATE_BRANCH_MERGED }} || true
git push -f origin --delete ${{ env.UPDATE_BRANCH }} || true
git push -f origin --delete ${{ env.UPDATE_BRANCH_MERGED }} || true