-
-
Notifications
You must be signed in to change notification settings - Fork 14
315 lines (270 loc) · 11 KB
/
build-and-deploy-static-web-app.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
name: Static Web App - Build and Deploy 🏗️
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main
workflow_dispatch:
permissions:
id-token: write
contents: write
pull-requests: write
env:
RESOURCE_GROUP: rg-blog-johnnyreilly-com
LOCATION: westeurope
STATICWEBAPPNAME: blog.johnnyreilly.com
ROOTCUSTOMDOMAINNAME: johnnyreilly.com
BLOGCUSTOMDOMAINNAME: blog.johnnyreilly.com
TAGS: '{"owner":"johnnyreilly", "email":"johnny_reilly@hotmail.com"}'
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
jobs:
build_and_deploy_swa_job:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
name: Site build and deploy 🏗️
steps:
- name: Checkout 📥
uses: actions/checkout@v3
with:
# Number of commits to fetch. 0 indicates all history for all branches and tags.
fetch-depth: 0
submodules: true
- name: AZ CLI login 🔑
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Set deployment name 📝
id: deployment_name
run: |
GIT_SHA='${{ github.sha }}'
SHORT_SHA=${GIT_SHA:0:7}
BRANCH_NAME='${{ env.BRANCH_NAME }}'
DEPLOYMENT_NAME="${BRANCH_NAME////-}.$SHORT_SHA"
echo "DEPLOYMENT_NAME=$DEPLOYMENT_NAME" >> $GITHUB_OUTPUT
- name: Infra - detect changes & validate 📝
id: static_web_app_what_if
if: github.event_name == 'pull_request'
uses: azure/CLI@v1
with:
inlineScript: |
# https://github.com/Azure/azure-cli/issues/25710
az config set bicep.use_binary_from_path=false
complexData='{"allowedIPAddresses": ${{ secrets.ALLOWEDIPADDRESS }} }'
az deployment group what-if \
--resource-group ${{ env.RESOURCE_GROUP }} \
--name "${{ steps.deployment_name.outputs.DEPLOYMENT_NAME }}" \
--template-file ./infra/main.bicep \
--parameters \
branch='main' \
location='${{ env.LOCATION }}' \
staticWebAppName='${{ env.STATICWEBAPPNAME }}' \
tags='${{ env.TAGS }}' \
repositoryToken='${{ secrets.WORKFLOW_TOKEN }}' \
rootCustomDomainName='${{ env.ROOTCUSTOMDOMAINNAME }}' \
blogCustomDomainName='${{ env.BLOGCUSTOMDOMAINNAME }}' \
complexData="$complexData"
az deployment group validate \
--resource-group ${{ env.RESOURCE_GROUP }} \
--name "${{ steps.deployment_name.outputs.DEPLOYMENT_NAME }}" \
--template-file ./infra/main.bicep \
--parameters \
branch='main' \
location='${{ env.LOCATION }}' \
staticWebAppName='${{ env.STATICWEBAPPNAME }}' \
tags='${{ env.TAGS }}' \
repositoryToken='${{ secrets.WORKFLOW_TOKEN }}' \
rootCustomDomainName='${{ env.ROOTCUSTOMDOMAINNAME }}' \
blogCustomDomainName='${{ env.BLOGCUSTOMDOMAINNAME }}' \
complexData="$complexData"
- name: Infra - deploy 🔧
id: static_web_app_deploy
if: github.event_name != 'pull_request'
uses: azure/CLI@v1
with:
inlineScript: |
# https://github.com/Azure/azure-cli/issues/25710
az config set bicep.use_binary_from_path=false
complexData='{"allowedIPAddresses": ${{ secrets.ALLOWEDIPADDRESS }} }'
az deployment group create \
--resource-group ${{ env.RESOURCE_GROUP }} \
--name "${{ steps.deployment_name.outputs.DEPLOYMENT_NAME }}" \
--template-file ./infra/main.bicep \
--parameters \
branch='main' \
location='${{ env.LOCATION }}' \
staticWebAppName='${{ env.STATICWEBAPPNAME }}' \
tags='${{ env.TAGS }}' \
repositoryToken='${{ secrets.WORKFLOW_TOKEN }}' \
rootCustomDomainName='${{ env.ROOTCUSTOMDOMAINNAME }}' \
blogCustomDomainName='${{ env.BLOGCUSTOMDOMAINNAME }}' \
complexData="$complexData"
- name: Get preview URL 📝
id: static_web_app_preview_url
uses: azure/CLI@v1
with:
inlineScript: |
DEFAULTHOSTNAME=$(az staticwebapp show -n '${{ env.STATICWEBAPPNAME }}' | jq -r '.defaultHostname')
PREVIEW_URL="https://${DEFAULTHOSTNAME/.[1-9]./-${{github.event.pull_request.number }}.${{ env.LOCATION }}.1.}"
echo "PREVIEW_URL=$PREVIEW_URL" >> $GITHUB_OUTPUT
- name: Setup Node.js 🔧
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'
- name: Setup bun 🔧
uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Install and build site 🔧
run: |
cd blog-website
yarn install --frozen-lockfile
DOCUSAURUS_SSR_CONCURRENCY=5 USE_CLOUDINARY=${{ github.event_name != 'pull_request' }} yarn run build
cp staticwebapp.config.json build/staticwebapp.config.json
- name: Get API key 🔑
id: static_web_app_apikey
uses: azure/CLI@v1
with:
inlineScript: |
APIKEY=$(az staticwebapp secrets list --name '${{ env.STATICWEBAPPNAME }}' | jq -r '.properties.apiKey')
echo "APIKEY=$APIKEY" >> $GITHUB_OUTPUT
- name: Deploy site 🚀
id: static_web_app_build_and_deploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ steps.static_web_app_apikey.outputs.APIKEY }}
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
action: 'upload'
skip_app_build: true
app_location: '/blog-website/build' # App source code path
api_location: '/blog-website/api' # Api source code path - optional
outputs:
preview-url: ${{steps.static_web_app_preview_url.outputs.PREVIEW_URL}}
integration_tests_job:
name: Integration tests 💡🏠
needs: build_and_deploy_swa_job
if: github.event_name == 'pull_request' && github.event.action != 'closed'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Wait for preview ${{ needs.build_and_deploy_swa_job.outputs.preview-url }} ⌚
id: static_web_app_wait_for_preview
uses: nev7n/wait_for_response@v1
with:
url: '${{ needs.build_and_deploy_swa_job.outputs.preview-url }}'
responseCode: 200
timeout: 600000
interval: 1000
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: npm ci
working-directory: ./blog-website-tests
- name: Install Playwright Browsers
run: npx playwright install --with-deps
working-directory: ./blog-website-tests
- name: Run Playwright tests
env:
PLAYWRIGHT_TEST_BASE_URL: '${{ needs.build_and_deploy_swa_job.outputs.preview-url }}'
run: npx playwright test
working-directory: ./blog-website-tests
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: blog-website-tests/playwright-report/
retention-days: 30
lighthouse_report_job:
name: Lighthouse report 💡🏠
needs: build_and_deploy_swa_job
if: github.event_name == 'pull_request' && github.event.action != 'closed'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Wait for preview ${{ needs.build_and_deploy_swa_job.outputs.preview-url }} ⌚
id: static_web_app_wait_for_preview
uses: nev7n/wait_for_response@v1
with:
url: '${{ needs.build_and_deploy_swa_job.outputs.preview-url }}'
responseCode: 200
timeout: 600000
interval: 1000
- name: Audit URLs 🧐
id: lighthouse_audit
uses: treosh/lighthouse-ci-action@v10
with:
urls: |
${{ needs.build_and_deploy_swa_job.outputs.preview-url }}
configPath: ./.github/workflows/lighthousesrc.json
uploadArtifacts: true
temporaryPublicStorage: true
runs: 5
- name: Dump steps.lighthouse_audit.outputs
id: github_context_step
run: echo '${{ toJSON(steps.lighthouse_audit.outputs) }}'
- name: Format lighthouse score
id: format_lighthouse_score
uses: actions/github-script@v6
with:
script: |
const lighthouseCommentMaker = require('./.github/workflows/lighthouseCommentMaker.js');
const lighthouseOutputs = {
manifest: ${{ steps.lighthouse_audit.outputs.manifest }},
links: ${{ steps.lighthouse_audit.outputs.links }}
};
const comment = lighthouseCommentMaker({ lighthouseOutputs });
core.setOutput("comment", comment);
- name: Add Lighthouse stats as comment ✍️
id: comment_to_pr
uses: marocchino/sticky-pull-request-comment@v2.6.2
with:
number: ${{ github.event.pull_request.number }}
header: lighthouse
message: ${{ steps.format_lighthouse_score.outputs.comment }}
deploy_to_devto_job:
name: Publish to dev.to 🗞️
needs: build_and_deploy_swa_job
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js 🔧
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'
- name: Publish to dev.to 🗞️
run: |
cd from-docusaurus-to-devto
yarn install --frozen-lockfile
DEVTO_APIKEY=${{ secrets.DEVTO_APIKEY }} yarn start
close_pull_request_job:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Cleanup staging 💥
steps:
- name: AZ CLI login 🔑
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Get API key 🔑
id: apikey
uses: azure/CLI@v1
with:
inlineScript: |
APIKEY=$(az staticwebapp secrets list --name '${{ env.STATICWEBAPPNAME }}' | jq -r '.properties.apiKey')
echo "APIKEY=$APIKEY" >> $GITHUB_OUTPUT
- name: Destroy staging environment 💥
id: closepullrequest
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ steps.apikey.outputs.APIKEY }}
action: 'close'