generated from CatChen/typescript-github-action-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
306 lines (274 loc) · 12.5 KB
/
test.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
name: Test
on:
push:
branches: [main]
pull_request:
branches: [main, '[0-9]+-[0-9]+-*']
workflow_run:
workflows: ['Dependabot']
types:
- completed
workflow_dispatch:
jobs:
test:
name: Test
if: |-
${{
!startsWith(github.head_ref, 'workflows/tests/') && ((
github.actor == 'dependabot[bot]' &&
github.event_name != 'push' &&
github.event_name != 'pull_request'
) || (
github.actor != 'dependabot[bot]' &&
github.event_name != 'workflow_run'
))
}}
runs-on: ubuntu-latest
permissions: write-all
steps:
- uses: actions/checkout@v4
if: ${{ github.event_name != 'workflow_run' }}
with:
fetch-depth: 0
- uses: actions/checkout@v4
if: ${{ github.event_name == 'workflow_run' }}
with:
ref: ${{ github.event.workflow_run.head_branch }}
fetch-depth: 0
- name: Config Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Git checkout base branch
id: git-checkout
env:
PULL_REQUEST_BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
PULL_REQUEST_WORKFLOW_RUN_BRANCH_NAME: ${{ github.event.workflow_run.head_branch }}
run: |
BASE_BRANCH_NAME=`git branch --show-current`
if [ -z "$BASE_BRANCH_NAME" ]
then
BASE_BRANCH_NAME="$PULL_REQUEST_BRANCH_NAME"
fi
if [ -z "$BASE_BRANCH_NAME" ]
then
BASE_BRANCH_NAME="$PULL_REQUEST_WORKFLOW_RUN_BRANCH_NAME"
fi
if [ -z "$BASE_BRANCH_NAME" ]
then
echo "::error::Fail to identify Pull Request branch"
exit 1
fi
echo "::notice::Base branch is $BASE_BRANCH_NAME"
git checkout $BASE_BRANCH_NAME
echo "base-branch-name=$BASE_BRANCH_NAME" >> $GITHUB_OUTPUT
echo "::notice::Base commit is `git rev-parse --verify HEAD`"
- name: Git checkout test base branch
env:
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }}
run: |
echo "::notice::Base branch is $TEST_BASE_BRANCH_NAME"
git checkout -b "$TEST_BASE_BRANCH_NAME"
- name: Make test base commit
env:
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }}
run: |
git commit --allow-empty -m "Test base commit for #$BASE_PR_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT"
echo "::notice::Test base commit is `git rev-parse --verify HEAD`"
- name: Git push test base branch
env:
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }}
run: |
git push -f --set-upstream origin "$TEST_BASE_BRANCH_NAME"
echo "::notice::Remote test branch is origin/$TEST_BASE_BRANCH_NAME"
- name: Open base PR
id: open-base-pr
env:
GH_TOKEN: ${{ secrets.CREATE_PULL_REQUEST_ACTION_TOKEN }}
SHA: ${{ github.sha }}
RUN_ID: ${{ github.run_id }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }}
run: |
echo "::notice::Run number is $RUN_NUMBER"
echo "::notice::Run attempt is $RUN_ATTEMPT"
echo "::notice::Base PR number is $BASE_PR_NUMBER"
gh pr create --base "main" --title "Test base PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER.
Run ID: [$RUN_ID]($(gh run view $RUN_ID --json 'url' --jq '.url')). Number: $RUN_NUMBER. Attempt: $RUN_ATTEMPT."
TEST_BASE_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2`
echo "test-base-pr-number=$TEST_BASE_PR_NUMBER" >> $GITHUB_OUTPUT
echo "::notice::Test base PR number is $TEST_BASE_PR_NUMBER"
- name: Git checkout test head branch
env:
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }}
run: |
echo "::notice::Test head branch is $TEST_HEAD_BRANCH_NAME"
git checkout -b "$TEST_HEAD_BRANCH_NAME"
- name: Make test head commit
env:
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }}
run: |
git commit --allow-empty -m "Test head commit for #$BASE_PR_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT"
echo "::notice::Test head commit is `git rev-parse --verify HEAD`"
- name: Git push test head branch
env:
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }}
run: |
git push -f --set-upstream origin "$TEST_HEAD_BRANCH_NAME"
echo "::notice::Remote test branch is origin/$TEST_HEAD_BRANCH_NAME"
- name: Open head PR
id: open-head-pr
env:
GH_TOKEN: ${{ secrets.CREATE_PULL_REQUEST_ACTION_TOKEN }}
SHA: ${{ github.sha }}
RUN_ID: ${{ github.run_id }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }}
run: |
echo "::notice::Base PR number is $BASE_PR_NUMBER"
gh pr create --base "$TEST_BASE_BRANCH_NAME" --title "Test head PR for #$BASE_PR_NUMBER ($SHA)" --body "Generated from #$BASE_PR_NUMBER.
Run ID: [$RUN_ID]($(gh run view $RUN_ID --json 'url' --jq '.url')). Number: $RUN_NUMBER. Attempt: $RUN_ATTEMPT.
#accept2ship"
TEST_HEAD_PR_NUMBER=`gh pr view | grep 'number:' | cut -f 2`
echo "test-head-pr-number=$TEST_HEAD_PR_NUMBER" >> $GITHUB_OUTPUT
echo "::notice::Test head PR number is $TEST_HEAD_PR_NUMBER"
- name: Approve PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr review --approve
- name: Check PR
id: check-pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }}
run: |
CHECKS_TEMP_FILE="$(mktemp)"
echo "checks-temp-file=$CHECKS_TEMP_FILE" >> $GITHUB_OUTPUT;
sleep 10
CHECKS=$(gh pr checks "$TEST_HEAD_PR_NUMBER" | tee -a $CHECKS_TEMP_FILE)
PENDING=$(echo "$CHECKS" | cut -f2 | grep pending | wc -l)
while [[ $PENDING -gt 0 && -n "$PENDING" ]]
do
echo "::group::Pending checks: $PENDING"
gh pr checks "$TEST_HEAD_PR_NUMBER" --watch --fail-fast | tee -a $CHECKS_TEMP_FILE
sleep 10
CHECKS=$(gh pr checks "$TEST_HEAD_PR_NUMBER" | tee -a $CHECKS_TEMP_FILE)
PENDING=$(echo "$CHECKS" | cut -f2 | grep pending | wc -l)
echo "::endgroup::"
done
if [[ -s $CHECKS_TEMP_FILE ]]
then
echo "::group::Temp file content"
cat $CHECKS_TEMP_FILE
echo "::endgroup::"
else
echo "checks-temp-file=" >> $GITHUB_OUTPUT;
fi
gh pr checks "$TEST_HEAD_PR_NUMBER" # Let check outcome determine the step exit code
- name: Report Checks
if: ${{ always() && steps.check-pr.outputs.checks-temp-file != '' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_PR_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }}
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }}
CHECKS_TEMP_FILE: ${{ steps.check-pr.outputs.checks-temp-file }}
run: |
gh pr comment "$BASE_PR_NUMBER" --body "**Checks from Push Test PR #$TEST_HEAD_PR_NUMBER**
| Check | Status | Time |
| --- | --- | --: |
$(cat $CHECKS_TEMP_FILE |
grep -E 'https://' |
tac |
awk 'BEGIN { FS ="\t" } ; { if (! seen[$1]++) print }' |
sed -E 's/(.*)\t(pass)\t(.*)\t(.*)\t(.*)/| [**\1**](\4) | ✅ | \3 |/;
s/(.*)\t(fail)\t(.*)\t(.*)\t(.*)/| [*\1*](\4) | ⛔ | \3 |/;
s/(.*)\t(skipping)\t(.*)\t(.*)\t(.*)/| [~\1~](\4) | ⏭️ | |/;
s/(.*)\t(pending)\t(.*)\t(.*)\t(.*)/| [\1](\4) | 🔄 | |/;
s/(.*)\t(.*)\t(.*)\t(.*)\t(.*)/| [\1](\4) | \2 | \3 |/')"
rm -f $CHECKS_TEMP_FILE
- name: Verify PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TEST_PR_STATE=`gh pr view --json 'state' -q '.state'`
echo "test_pr_state=$TEST_PR_STATE" >> $GITHUB_OUTPUT
echo "::notice::Test PR state is $TEST_PR_STATE"
if [ "$TEST_PR_STATE" != 'MERGED' ]
then
exit 1
fi
- name: Close head PR
if: ${{ failure() || cancelled() }}
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_BRANCH_NAME: ${{ steps.git-checkout.outputs.base-branch-name }}
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }}
run: |
TEST_PR_STATE=`gh pr view --json 'state' -q '.state'`
if [ "$TEST_PR_STATE" = 'MERGED' ]
then
exit
fi
if [ -n "$BASE_BRANCH_NAME" ]
then
git checkout "$BASE_BRANCH_NAME"
echo "::notice::Base branch is `git branch --show-current`"
else
git checkout "$SHA"
echo "::notice::Base commit is `git rev-parse --verify HEAD`"
fi
gh pr close $TEST_HEAD_PR_NUMBER -d -c "Finished testing $SHA by $RUN_NUMBER-$RUN_ATTEMPT. Closing PR."
- name: Delete remote test head branch
if: ${{ always() }}
continue-on-error: true
env:
TEST_HEAD_BRANCH_NAME: ${{ format('workflows/tests/head/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }}
run: |
if test $(git ls-remote origin --heads "$TEST_HEAD_BRANCH_NAME" | wc -l ) != 0
then
git push -d origin "$TEST_HEAD_BRANCH_NAME"
fi
- name: Close base PR
if: ${{ always() }}
continue-on-error: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHA: ${{ github.sha }}
RUN_NUMBER: ${{ github.run_number }}
RUN_ATTEMPT: ${{ github.run_attempt }}
BASE_BRANCH_NAME: ${{ steps.git-checkout.outputs.base-branch-name }}
TEST_BASE_PR_NUMBER: ${{ steps.open-base-pr.outputs.test-base-pr-number }}
run: |
if [ -n "$BASE_BRANCH_NAME" ]
then
git checkout "$BASE_BRANCH_NAME"
echo "::notice::Base branch is `git branch --show-current`"
else
git checkout "$SHA"
echo "::notice::Base commit is `git rev-parse --verify HEAD`"
fi
gh pr close $TEST_BASE_PR_NUMBER -d -c "Finished testing $SHA by $RUN_NUMBER-$RUN_ATTEMPT. Closing PR."
- name: Delete remote test base branch
if: ${{ always() }}
continue-on-error: true
env:
TEST_BASE_BRANCH_NAME: ${{ format('workflows/tests/base/id-{0}/run-{1}/attempt-{2}/sha-{3}', github.run_id, github.run_number, github.run_attempt, github.sha) }}
run: |
if test $(git ls-remote origin --heads "$TEST_BASE_BRANCH_NAME" | wc -l ) != 0
then
git push -d origin "$TEST_BASE_BRANCH_NAME"
fi