generated from CatChen/typescript-github-action-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
234 lines (202 loc) · 9.09 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
name: Test
on:
push:
branches: [main]
pull_request:
branches: [main, '[0-9]+-[0-9]+-*']
workflow_run:
workflows: ['Dependabot']
branches-ignore: ['workflows/tests/*']
types:
- completed
workflow_dispatch:
workflow_call:
jobs:
test:
name: Test
if: ${{ !startsWith(github.head_ref, 'workflows/tests/') }}
runs-on: ubuntu-latest
permissions: write-all
env:
SHA: ${{ github.sha }}
PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number }}
PULL_REQUEST_HEAD_REF: |-
${{
(github.event.pull_request.head.repo.full_name == github.repository)
&& (github.event.pull_request.head.ref || github.event.workflow_run.head_branch)
|| (github.event.pull_request.head.sha || github.event.workflow_run.head_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) }}
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) }}
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
- uses: CatChen/config-git-with-token-action@v1
with:
github-token: ${{ secrets.CREATE_PULL_REQUEST_ACTION_TOKEN }}
- name: Git checkout PR head commit
id: git-checkout
run: |
echo "::notice::PR head ref is $PULL_REQUEST_HEAD_REF"
git checkout $PULL_REQUEST_HEAD_REF
- name: Git checkout test base branch
run: |
echo "::notice::Test base branch is $TEST_BASE_BRANCH_NAME"
git checkout -b "$TEST_BASE_BRANCH_NAME"
- name: Make test base commit
run: |
git commit --allow-empty -m "Test base commit for #$PULL_REQUEST_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT"
echo "::notice::Test base commit is `git rev-parse --verify HEAD`"
- name: Git push test base branch
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 }}
run: |
echo "::notice::Run number is $RUN_NUMBER"
echo "::notice::Run attempt is $RUN_ATTEMPT"
echo "::notice::Base PR number is $PULL_REQUEST_NUMBER"
gh pr create --base "main" --title "Test base PR for #$PULL_REQUEST_NUMBER ($SHA)" --body "Generated from #$PULL_REQUEST_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
run: |
echo "::notice::Test head branch is $TEST_HEAD_BRANCH_NAME"
git checkout -b "$TEST_HEAD_BRANCH_NAME"
- name: Make test head commit
run: |
git commit --allow-empty -m "Test head commit for #$PULL_REQUEST_NUMBER ($SHA) by $RUN_NUMBER-$RUN_ATTEMPT"
echo "::notice::Test head commit is `git rev-parse --verify HEAD`"
- name: Git push test head branch
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 }}
run: |
echo "::notice::Base PR number is $PULL_REQUEST_NUMBER"
gh pr create --base "$TEST_BASE_BRANCH_NAME" --title "Test head PR for #$PULL_REQUEST_NUMBER ($SHA)" --body "Generated from #$PULL_REQUEST_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 != '' && (github.event_name == 'pull_request' || github.event_name == 'workflow_run')}}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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 $PULL_REQUEST_NUMBER --body "**Checks from 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 }}
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }}
run: |
TEST_PR_STATE=`gh pr view $TEST_HEAD_PR_NUMBER --json 'state' -q '.state'`
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 }}
TEST_HEAD_PR_NUMBER: ${{ steps.open-head-pr.outputs.test-head-pr-number }}
run: |
TEST_PR_STATE=`gh pr view $TEST_HEAD_PR_NUMBER --json 'state' -q '.state'`
if [ "$TEST_PR_STATE" = 'MERGED' ]
then
exit
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
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 }}
TEST_BASE_PR_NUMBER: ${{ steps.open-base-pr.outputs.test-base-pr-number }}
run: |
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
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