242 lines (231 loc) · 11.2 KB
/
pull-request.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
name: Pull Request
on: [pull_request]
env:
# NOTE: APP_SERVER is used as output in few steps so it cannot be
# secret (Github would fail to output). As the server is also
# used in public github comments it's fine to have it here.
APP_SERVER: https://onlineinquirytool.org
jobs:
build:
name: Build
runs-on: ubuntu-latest
outputs:
result: ${{ steps.result.outputs.result }}
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Install pnpm
uses: pnpm/action-setup@d882d12c64e032187b2edb46d3a0d003b7a43598 # v2.4.0
with:
version: latest
- name: Setup Node.js
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
with:
node-version: lts/*
cache: 'pnpm' # cache example form: https://pnpm.io/continuous-integration#github-actions
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build
env:
PR_NUM: ${{ github.event.number }}
PR_URL: ${{ github.event.pull_request.html_url }}
run: |
echo "APP_VERSION=v$(jq -r .version package.json)-pr-${PR_NUM}-build-${GITHUB_RUN_NUMBER}-preview" >> .env.production.local
echo "APP_LINK=${PR_URL}" >> .env.production.local
cat .env.production.local
pnpm run build && touch build-ok
- name: Archive build artifacts
uses: actions/upload-artifact@c7d193f32edcb7bfad88892161225aeda64e9392 # v4.0.0
with:
name: online-inquiry-tool-pr-${{ github.event.number }}-build-${{ github.run_number }}
path: |
dist
retention-days: 30 # default 90 days
- name: Set result
if: ${{ always() }}
id: result
run: 'test -f build-ok && echo "result=- **✅ build**: Success 🛠️" >> $GITHUB_OUTPUT || echo "result=- **🛑 build**: FAILED 💩" >> $GITHUB_OUTPUT'
deploy:
name: Deploy
if: ${{ always() }}
needs: build
runs-on: ubuntu-latest
outputs:
result: ${{ steps.result.outputs.result }}
test-link: ${{ steps.test-link.outputs.test-link }}
steps:
- name: Get build artifact
id: build-and-deploy
uses: actions/download-artifact@f44cd7b40bfd40b6aa1cc1b9b5b7bf03d3c67110 # v4.1.0
with:
name: online-inquiry-tool-pr-${{ github.event.number }}-build-${{ github.run_number }}
path: dist
- name: Configure AWS credentials for S3 deployment
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
# aws cli is available already in GH image
# see: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-software
# https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-Readme.md
- name: Deploy artifacts to S3
env:
PR_NUM: ${{ github.event.number }}
run: aws s3 cp --recursive dist/ "s3://${{ secrets.AWS_BUCKET_NAME }}/pr/${PR_NUM}/${GITHUB_RUN_NUMBER}/" && touch deploy-ok
- name: Set result
if: ${{ always() }}
id: result
env:
PR_NUM: ${{ github.event.number }}
# NOTE: using secrets in output make GitHub to skip output!
run: 'test -f deploy-ok && echo "result=- **✅ deploy**: Success 🚀 Test it here: <${APP_SERVER}/pr/${PR_NUM}/${GITHUB_RUN_NUMBER}/index.html>" >> $GITHUB_OUTPUT || echo "result=- **🛑 deploy**: FAILED 💔" >> $GITHUB_OUTPUT'
- name: Set test link
if: ${{ always() }}
id: test-link
env:
PR_NUM: ${{ github.event.number }}
# NOTE: using secrets in output make GitHub to skip output!
run: echo "test-link=${APP_SERVER}/pr/${PR_NUM}/${GITHUB_RUN_NUMBER}/index.html" >> $GITHUB_OUTPUT
commitlint:
name: Run commitlint
runs-on: ubuntu-latest
outputs:
result: ${{ steps.result.outputs.result }}
warn-many-commits: ${{ steps.warn-many-commits.outputs.warn-many-commits }}
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
fetch-depth: 0
- name: Run commitlint
uses: wagoid/commitlint-github-action@0d749a1a91d4770e983a7b8f83d4a3f0e7e0874e # v5.4.4
with:
configFile: .commitlintrc.yaml
- name: Fail if too many commits
if: ${{ github.event.pull_request.commits > 30 }}
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
core.setFailed('You have ${{ github.event.pull_request.commits }} commits in your PR! Hard limit is 30.')
- name: Mark passed
run: touch commits-ok
- name: Set result
if: ${{ always() }}
id: result
run: 'test -f commits-ok && echo "result=- **✅ commitlint**: Success 👍" >> $GITHUB_OUTPUT || echo "result=- **🛑 commitlint**: FAILED 🧐" >> $GITHUB_OUTPUT'
- name: Set many commits warning
if: ${{ always() }}
id: warn-many-commits
# NOTE: Multiline strings need special handling.
# See: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-a-multiline-string
env:
N: ${{ github.event.pull_request.commits }}
run: |
if [[ "${N}" -gt 30 ]]; then \
printf "warn-many-commits<<EOF\n\n**🛑 ERROR: You have ${N} commits in your PR. GitHub API and/or wagoid/commitlint-github-action seems to be limited to 30 commits / PR, so commitlint check will fail! 🛑**\n\nEOF\n" >> $GITHUB_OUTPUT; \
elif [[ "${N}" -ge 20 ]]; then \
printf "warn-many-commits<<EOF\n\n**⚠️ WARNING: You have ${N} commits in your PR. GitHub API and/or wagoid/commitlint-github-action seems to be limited to 30 commits / PR, so commitlint check starts failing after that count! ⚠️**\n\nEOF\n" >> $GITHUB_OUTPUT; \
else echo "warn-many-commits=" >> $GITHUB_OUTPUT; fi
src-lints:
name: Run eslint and translations check
# Run after build just in case to avoid cache getting messed up, but run always.
# (Not sure if setup-node could handle parallel access).
if: ${{ always() }}
needs: [build]
runs-on: ubuntu-latest
outputs:
es-result: ${{ steps.es-result.outputs.result }}
tr-result: ${{ steps.tr-result.outputs.result }}
tr-details: ${{ steps.tr-result.outputs.details }}
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Install pnpm
uses: pnpm/action-setup@d882d12c64e032187b2edb46d3a0d003b7a43598 # v2.4.0
with:
version: latest
- name: Setup Node.js
uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1
with:
node-version: lts/*
cache: 'pnpm' # cache example form: https://pnpm.io/continuous-integration#github-actions
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run eslint
run: pnpm run lint && touch eslint-ok
- name: Run translation check
if: ${{ always() }}
run: |
pnpm run build:tools
node dist.local/tools/lang-check.js -m dist.local/tr-out.md && touch tr-ok
- name: Set eslint result
if: ${{ always() }}
id: es-result
run: 'test -f eslint-ok && echo "result=- **✅ eslint**: Success 👍" >> $GITHUB_OUTPUT || echo "result=- **🛑 eslint**: FAILED 🧐" >> $GITHUB_OUTPUT'
- name: Set translations check result
if: ${{ always() }}
id: tr-result
# NOTE: Multiline strings need special handling.
# See: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-a-multiline-string
run: |
test -f tr-ok && echo "result=- **✅ translations check**: Success 👍" >> $GITHUB_OUTPUT || echo "result=- **🛑 translations check**: FAILED 🧐 (see \"Translation check problems below\")" >> $GITHUB_OUTPUT
output=$(cat dist.local/tr-out.md)
test -f tr-ok && echo "details=" >> $GITHUB_OUTPUT || printf "details<<EOF\n${output}\nEOF\n" >> $GITHUB_OUTPUT
comment-success:
name: Leave a comment on success
needs: [build, deploy, commitlint, src-lints]
runs-on: ubuntu-latest
steps:
- name: Leave a comment
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
github-token: ${{ secrets.GH_TOKEN_RELEASE_BOT }}
# Note the indentation of body!
# See: https://github.com/actions/github-script/issues/247#issuecomment-1079839739
# and: https://github.com/actions/github-script#welcome-a-first-time-contributor
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Great success! 🥳 Your pull request was built and has been deployed for testing: <${{ needs.deploy.outputs.test-link }}>
${{ needs.commitlint.outputs.warn-many-commits }}
**Job summary:**
${{ needs.build.outputs.result }}
${{ needs.deploy.outputs.result }}
${{ needs.commitlint.outputs.result }}
${{ needs.src-lints.outputs.es-result }}
${{ needs.src-lints.outputs.tr-result }}
${{ needs.src-lints.outputs.tr-details }}
Build job with artifacts is here: <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}>`
})
comment-failure:
name: Leave a comment on failure
if: ${{ failure() }}
needs: [build, deploy, commitlint, src-lints]
runs-on: ubuntu-latest
steps:
- name: Leave a comment
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
github-token: ${{ secrets.GH_TOKEN_RELEASE_BOT }}
# Note the indentation of body!
# See: https://github.com/actions/github-script/issues/247#issuecomment-1079839739
# and: https://github.com/actions/github-script#welcome-a-first-time-contributor
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Something went wrong with your PR's build job! 😭 See summary and build job link below 👇
${{ needs.commitlint.outputs.warn-many-commits }}
**Job summary:**
${{ needs.build.outputs.result }}
${{ needs.deploy.outputs.result }}
${{ needs.commitlint.outputs.result }}
${{ needs.src-lints.outputs.es-result }}
${{ needs.src-lints.outputs.tr-result }}
${{ needs.src-lints.outputs.tr-details }}
Build job with artifacts is here: <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}>`
})