-
-
Notifications
You must be signed in to change notification settings - Fork 110
434 lines (377 loc) · 27.4 KB
/
deploy-vercel-staging.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# Summary:
# Creates a new deployment on Vercel's platform, when anything is pushed in any branch (except for the "master" branch).
# Read ./README.md for extensive documentation
name: Deploy to Vercel (staging)
on:
# There are several ways to trigger Github actions - See https://help.github.com/en/actions/reference/events-that-trigger-workflows#example-using-a-single-event for a comprehensive list:
# - "push": Triggers each time a commit is pushed
# - "pull_request": Triggers each time a commit is pushed within a pull request, it makes it much easier to write comments within the PR, but it suffers some strong limitations:
# - There is no way to trigger when a PR is merged into another - See https://github.community/t/pull-request-action-does-not-run-on-merge/16092?u=vadorequest
# - It won't trigger when the PR is conflicting with its base branch - See https://github.community/t/run-actions-on-pull-requests-with-merge-conflicts/17104/2?u=vadorequest
push: # Triggers on each pushed commit
branches-ignore:
- 'master'
# Allow manual trigger via a button in github or a HTTP call - See https://docs.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#manually-running-a-workflow
# XXX Read more about how to use it with NRN in .github/WORKFLOW_DISPATCH.md
workflow_dispatch:
inputs:
customer:
description: 'Customer to deploy'
required: true
jobs:
# Configures the deployment environment, install dependencies (like node, npm, etc.) that are requirements for the upcoming jobs
# Ex: Necessary to run `yarn deploy`
setup-environment:
name: Setup deployment environment (Ubuntu 18.04 - Node 12.x)
runs-on: ubuntu-18.04
steps:
- name: Installing node.js
uses: actions/setup-node@v1 # Used to install node environment - XXX https://github.com/actions/setup-node
with:
node-version: '12.x' # Use the same node.js version as the one Vercel's uses (currently node12.x)
# Starts a Vercel deployment, using the staging configuration file of the default institution
# The default institution is the one defined in the `vercel.json` file (which is a symlink to the actual file)
# N.B: It's Vercel that will perform the actual deployment
start-staging-deployment:
name: Starts Vercel deployment (staging) (Ubuntu 18.04)
runs-on: ubuntu-18.04
needs: setup-environment
steps:
- uses: actions/checkout@v1 # Get last commit pushed - XXX See https://github.com/actions/checkout
- name: Deploying on Vercel (staging)
# Workflow overview:
# - Resolve customer to deploy from github event input (falls back to resolving it from vercel.json file)
# - Resolve $VERCEL_DEPLOYMENT_URL
# - Get stdout from deploy command (stderr prints build steps and stdout prints deployment url, which is what we are really looking for)
# - Set the deployment url that will be included in the eventual PR comment
# - Create a deployment alias based on the branch name, and link it to the deployment (so that each branch has its own domain automatically aliased to the latest commit)
# XXX You can use https://jqplay.org/ if you want to play around with "jq" to manipulate JSON
run: |
# Print the version of the "vercel" CLI being used (helps debugging)
vercel --version
echo "Deploying using git commit SHA: $GIT_COMMIT_SHA and branch/tag: $GIT_COMMIT_REF"
# Resolving customer to deploy based on the github event input, when using manual deployment triggerer through "workflow_dispatch" event
# Falls back to the customer specified in the vercel.json file, which is most useful when deployment is triggered through "push" event
MANUAL_TRIGGER_CUSTOMER="${{ github.event.inputs.customer}}"
echo "MANUAL_TRIGGER_CUSTOMER: " $MANUAL_TRIGGER_CUSTOMER
echo "MANUAL_TRIGGER_CUSTOMER=$MANUAL_TRIGGER_CUSTOMER" >> $GITHUB_ENV
CUSTOMER_REF_TO_DEPLOY="${MANUAL_TRIGGER_CUSTOMER:-$(cat vercel.json | jq --raw-output '.build.env.NEXT_PUBLIC_CUSTOMER_REF')}"
echo "Customer to deploy: " $CUSTOMER_REF_TO_DEPLOY
echo "CUSTOMER_REF_TO_DEPLOY=$CUSTOMER_REF_TO_DEPLOY" >> $GITHUB_ENV
# Deploy the customer on Vercel using the customer ref
# Store the output in a variable so we can extract metadata from it
VERCEL_DEPLOYMENT_OUTPUT=`GIT_COMMIT_REF=${GIT_COMMIT_REF} GIT_COMMIT_SHA=${GIT_COMMIT_SHA} CUSTOMER_REF=${CUSTOMER_REF_TO_DEPLOY} yarn deploy:ci:gha --token $VERCEL_TOKEN`
# Extract the Vercel deployment url from the deployment output
VERCEL_DEPLOYMENT_URL=`echo $VERCEL_DEPLOYMENT_OUTPUT | egrep -o 'https?://[^ ]+.vercel.app'`
echo "Deployment url: " $VERCEL_DEPLOYMENT_URL
echo "VERCEL_DEPLOYMENT_URL=$VERCEL_DEPLOYMENT_URL" >> $GITHUB_ENV
# Build the alias domain based on our own rules (NRN own rules)
# Basically, if a branch starts with "v[0-9]" then we consider it to be a "preset branch" and we use the branch name as alias (e.g: "v2-mst-xxx")
# Otherwise, we use the deployment name and suffix it using the branch name (e.g: "v2-mst-xxx-branch-name")
if [[ ${GIT_COMMIT_REF##*/} =~ ^v[0-9]{1,}- ]]; then # Checking if pattern matches with "vX-" where X is a number
VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS=${GIT_COMMIT_REF##*/}
else
VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS=$(cat vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json | jq --raw-output '.name')-${GIT_COMMIT_REF##*/}
fi
echo "Resolved domain alias: " $VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS
# Vercel alias only allows 41 characters in the domain name, so we only keep the first 41 (41 = 53 - 11 - 1) characters (because Vercel needs 7 chars for ".vercel.app" at the end of the domain name, and count starts at 1, not 0)
# Also, in order to remove forbidden characters, we transform every characters which are not a "alnum" and \n or \r into '-'
VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS=$(echo $VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS | head -c 41 | tr -c '[:alnum:]\r\n' - | tr '[:upper:]' '[:lower:]')
echo "Sanitized domain alias (1): " $VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS
# Recursively remove any trailing dash ('-') to protect against invalid alias domain (not allowed to end with a trailing slash)
while [[ "$VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS" == *- ]]
do
VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS=${VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS::-1}
done
echo "Sanitized domain alias (2): " $VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS
# Build alias url
VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS="${VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS}.vercel.app"
echo "VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS=$VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS" >> $GITHUB_ENV
echo "Alias domain: "$VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS
echo "Aliasing the deployment using the git branch alias:"
echo "npx vercel alias "$VERCEL_DEPLOYMENT_URL $VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS
# Create Vercel alias, use "||" to allow failure (keep the build going) even if the alias fails
VERCEL_ALIASING_OUTPUT=`npx vercel alias $VERCEL_DEPLOYMENT_URL $VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS --token $VERCEL_TOKEN || echo "Aliasing failed for '$VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS', but the build will continue regardless."`
echo $VERCEL_ALIASING_OUTPUT
# TODO Resolve whether the aliasing has worked and change the GitHub comment "Comment PR (Deployment success)"
# Find all custom aliases configured in the customer deployment configuration file (vercel.json)
VERCEL_DEPLOYMENT_ALIASES_JSON=$(cat vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json | jq --raw-output '.alias')
echo "Custom aliases: " $VERCEL_DEPLOYMENT_ALIASES_JSON
# Convert the JSON array into a bash array - See https://unix.stackexchange.com/a/615717/60329
readarray -t VERCEL_DEPLOYMENT_ALIASES < <(jq --raw-output '.alias[]' < vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json)
# Count the number of element in the array, will be 0 if it's an empty array, or if the "alias" key wasn't defined
VERCEL_DEPLOYMENT_ALIASES_COUNT=${#VERCEL_DEPLOYMENT_ALIASES[@]}
# Check if there are no aliases configured
if [ "$VERCEL_DEPLOYMENT_ALIASES" > 0 ]
then
echo "$VERCEL_DEPLOYMENT_ALIASES_COUNT alias(es) found. Aliasing them now..."
# For each alias configured, then assign it to the deployed domain
for DEPLOYMENT_ALIAS in "${VERCEL_DEPLOYMENT_ALIASES[@]}"; do
echo "npx vercel alias "$VERCEL_DEPLOYMENT_URL $DEPLOYMENT_ALIAS
npx vercel alias $VERCEL_DEPLOYMENT_URL $DEPLOYMENT_ALIAS --token $VERCEL_TOKEN || echo "Aliasing failed for '$DEPLOYMENT_ALIAS', but the build will continue regardless."
done
else
# $VERCEL_DEPLOYMENT_ALIASES is null, this happens when it was not defined in the vercel.json file
echo "There are no more aliases to configure. You can add more aliases from your vercel.json 'alias' property. See https://vercel.com/docs/configuration?query=alias%20domain#project/alias"
echo "$VERCEL_DEPLOYMENT_ALIASES"
fi
env:
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} # Passing github's secret to the worker
GIT_COMMIT_REF: ${{ github.ref }} # Passing current branch/tag to the worker
GIT_COMMIT_SHA: ${{ github.sha }} # Passing current commit SHA to the worker
# We need to find the PR id. Will be used later to comment on that PR.
- name: Finding Pull Request ID
uses: jwalton/gh-find-current-pr@v1
id: pr_id_finder
if: always() # It forces the job to be always executed, even if a previous job fail.
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# On deployment failure, add a comment to the PR, if there is an open PR for the current branch
- name: Comment PR (Deployment failure)
uses: peter-evans/create-or-update-comment@v1
if: steps.pr_id_finder.outputs.number && failure()
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
body: |
:x: Deployment **FAILED**
Commit ${{ github.sha }} failed to deploy to [${{ env.VERCEL_DEPLOYMENT_URL }}](${{ env.VERCEL_DEPLOYMENT_URL }})
[click to see logs](https://github.com/UnlyEd/next-right-now/pull/${{ steps.pr_id_finder.outputs.number }}/checks)
# On deployment success, add a comment to the PR, if there is an open PR for the current branch
- name: Comment PR (Deployment success)
uses: peter-evans/create-or-update-comment@v1
if: steps.pr_id_finder.outputs.number && success()
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
body: |
:white_check_mark: Deployment **SUCCESS**
Commit ${{ github.sha }} successfully deployed to [${{ env.VERCEL_DEPLOYMENT_URL }}](${{ env.VERCEL_DEPLOYMENT_URL }})
Deployment aliased as [${{ env.VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS }}](https://${{ env.VERCEL_DEPLOYMENT_AUTO_BRANCH_ALIAS }})
await-for-vercel-deployment:
name: Await current deployment to be ready (Ubuntu 18.04)
runs-on: ubuntu-18.04
needs: start-staging-deployment
steps:
- name: Resolving deployment url from Vercel
# Workflow overview:
# - Resolve customer to deploy from github event input (falls back to resolving it from vercel.json file)
# - Resolve $VERCEL_DEPLOYMENT_URL
# - Fetch all deployments data (by using the scope in `vercel.json`)
# - Resolve the last url (from `response.deployments[0].url`)
# - Remove the `"` character to pre-format url
# We need to set env the url for next step, formatted as `https://${$VERCEL_DEPLOYMENT}`
# XXX You can use https://jqplay.org/ if you want to play around with "jq" to manipulate JSON
run: |
apt update -y >/dev/null && apt install -y jq >/dev/null
MANUAL_TRIGGER_CUSTOMER="${{ github.event.inputs.customer}}"
echo "MANUAL_TRIGGER_CUSTOMER: " $MANUAL_TRIGGER_CUSTOMER
echo "MANUAL_TRIGGER_CUSTOMER=$MANUAL_TRIGGER_CUSTOMER" >> $GITHUB_ENV
CUSTOMER_REF_TO_DEPLOY="${MANUAL_TRIGGER_CUSTOMER:-$(cat vercel.json | jq --raw-output '.build.env.NEXT_PUBLIC_CUSTOMER_REF')}"
echo "Customer that was deployed: " $CUSTOMER_REF_TO_DEPLOY
echo "CUSTOMER_REF_TO_DEPLOY=$CUSTOMER_REF_TO_DEPLOY" >> $GITHUB_ENV
# Resolve Vercel team ID
VERCEL_TEAM_ID=`cat vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json | jq --raw-output '.scope'`
echo "Vercel team ID: " $VERCEL_TEAM_ID
echo "VERCEL_TEAM_ID=$VERCEL_TEAM_ID" >> $GITHUB_ENV
# Resolve Vercel project name
VERCEL_PROJECT_NAME=`cat vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json | jq --raw-output '.name'`
echo "Vercel project name: " $VERCEL_PROJECT_NAME
echo "VERCEL_PROJECT_NAME=$VERCEL_PROJECT_NAME" >> $GITHUB_ENV
# Build Vercel API endpoint used to fetch deployments
VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT="https://api.zeit.co/v5/now/deployments?teamId=$VERCEL_TEAM_ID"
echo "Fetching Vercel deployments using API endpoint: " $VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT
# Fetch all Vercel deployment from this project
ALL_VERCEL_DEPLOYMENTS=`curl -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: Bearer ${{ secrets.VERCEL_TOKEN }}' $VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT`
echo "Vercel deployments for current team: " $ALL_VERCEL_DEPLOYMENTS
# Parse the deployments (as json) to find the latest deployment url, while stripping the double quotes
# TODO Do not use '.deployments [0].url' blindly, but filter the deployments array first to make sure to consider only the deployments where "name" is equal to $VERCEL_PROJECT_NAME
VERCEL_DEPLOYMENT=`echo $ALL_VERCEL_DEPLOYMENTS | jq '.deployments [0].url' | tr -d \"`
VERCEL_DEPLOYMENT_URL="https://$VERCEL_DEPLOYMENT"
echo "Url where to run E2E tests (VERCEL_DEPLOYMENT_URL): " $VERCEL_DEPLOYMENT_URL
echo "VERCEL_DEPLOYMENT_URL=$VERCEL_DEPLOYMENT_URL" >> $GITHUB_ENV
echo "VERCEL_DEPLOYMENT=$VERCEL_DEPLOYMENT" >> $GITHUB_ENV
# Wait for deployment to be ready, before running E2E (otherwise Cypress might start testing too early, and gets redirected to Vercel's "Login page", and tests fail)
- name: Awaiting Vercel deployment to be ready
uses: UnlyEd/github-action-await-vercel@v1.1.1
id: await-vercel
env:
VERCEL_TOKEN: ${{ secrets.VERCEl_TOKEN }}
with:
deployment-url: ${{ env.VERCEL_DEPLOYMENT }} # Must only contain the domain name (no http prefix, etc.)
timeout: 90 # Wait for 90 seconds before failing
- name: Display deployment status
run: "echo My deployment is ${{ fromJson(steps.await-vercel.outputs.deploymentDetails).readyState }}"
# Runs E2E tests against the Vercel deployment
run-2e2-tests:
name: Run end to end (E2E) tests (Ubuntu 18.04)
runs-on: ubuntu-18.04
# Docker image with Cypress pre-installed
# https://github.com/cypress-io/cypress-docker-images/tree/master/included
container: cypress/included:3.8.3
needs: await-for-vercel-deployment
steps:
- uses: actions/checkout@v1 # Get last commit pushed - XXX See https://github.com/actions/checkout
- name: Resolving deployment url from Vercel
run: |
apt update -y >/dev/null && apt install -y jq >/dev/null
MANUAL_TRIGGER_CUSTOMER="${{ github.event.inputs.customer}}"
echo "MANUAL_TRIGGER_CUSTOMER: " $MANUAL_TRIGGER_CUSTOMER
echo "MANUAL_TRIGGER_CUSTOMER=$MANUAL_TRIGGER_CUSTOMER" >> $GITHUB_ENV
CUSTOMER_REF_TO_DEPLOY="${MANUAL_TRIGGER_CUSTOMER:-$(cat vercel.json | jq --raw-output '.build.env.NEXT_PUBLIC_CUSTOMER_REF')}"
echo "Customer that was deployed: " $CUSTOMER_REF_TO_DEPLOY
echo "CUSTOMER_REF_TO_DEPLOY=$CUSTOMER_REF_TO_DEPLOY" >> $GITHUB_ENV
# Resolve Vercel team ID
VERCEL_TEAM_ID=`cat vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json | jq --raw-output '.scope'`
echo "Vercel team ID: " $VERCEL_TEAM_ID
echo "VERCEL_TEAM_ID=$VERCEL_TEAM_ID" >> $GITHUB_ENV
# Resolve Vercel project name
VERCEL_PROJECT_NAME=`cat vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json | jq --raw-output '.name'`
echo "Vercel project name: " $VERCEL_PROJECT_NAME
echo "VERCEL_PROJECT_NAME=$VERCEL_PROJECT_NAME" >> $GITHUB_ENV
# Build Vercel API endpoint used to fetch deployments
VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT="https://api.zeit.co/v5/now/deployments?teamId=$VERCEL_TEAM_ID"
echo "Fetching Vercel deployments using API endpoint: " $VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT
# Fetch all Vercel deployment from this project
ALL_VERCEL_DEPLOYMENTS=`curl -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: Bearer ${{ secrets.VERCEL_TOKEN }}' $VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT`
echo "Vercel deployments for current team: " $ALL_VERCEL_DEPLOYMENTS
# Parse the deployments (as json) to find the latest deployment url, while stripping the double quotes
# TODO Do not use '.deployments [0].url' blindly, but filter the deployments array first to make sure to consider only the deployments where "name" is equal to $VERCEL_PROJECT_NAME
VERCEL_DEPLOYMENT=`echo $ALL_VERCEL_DEPLOYMENTS | jq '.deployments [0].url' | tr -d \"`
VERCEL_DEPLOYMENT_URL="https://$VERCEL_DEPLOYMENT"
echo "Url where to run E2E tests (VERCEL_DEPLOYMENT_URL): " $VERCEL_DEPLOYMENT_URL
echo "VERCEL_DEPLOYMENT_URL=$VERCEL_DEPLOYMENT_URL" >> $GITHUB_ENV
# Run the E2E tests against the new Vercel deployment
- name: Run E2E tests (Cypress)
uses: cypress-io/github-action@v2 # XXX See https://github.com/cypress-io/github-action
with:
wait-on: ${{ env.VERCEL_DEPLOYMENT_URL }} # Be sure that the endpoint is ready by pinging it before starting tests, it has a timeout of 60seconds
config-file: cypress/config-customer-ci-cd.json # The config file itself doesn't matter because we will override most settings anyway. We just need `projectId` to run the tests.
config: baseUrl=${{ env.VERCEL_DEPLOYMENT_URL }} # Overriding baseUrl provided by config file to test the new deployment
env:
# Enables Cypress debugging logs, very useful if Cypress crashes, like out-of-memory issues.
# DEBUG: "cypress:*" # Enable all logs. See https://docs.cypress.io/guides/references/troubleshooting.html#Print-DEBUG-logs
DEBUG: "cypress:server:util:process_profiler" # Enable logs for "memory and CPU usage". See https://docs.cypress.io/guides/references/troubleshooting.html#Log-memory-and-CPU-usage
# On E2E failure, upload screenshots
- name: Upload screenshots artifacts (E2E failure)
uses: actions/upload-artifact@v1 # On failure we upload artifacts, https://help.github.com/en/actions/automating-your-workflow-with-github-actions/persisting-workflow-data-using-artifacts
if: failure()
with:
name: screenshots
path: cypress/screenshots/
# On E2E failure, upload videos
- name: Upload videos artifacts (E2E failure)
uses: actions/upload-artifact@v1 # On failure we upload artifacts, https://help.github.com/en/actions/automating-your-workflow-with-github-actions/persisting-workflow-data-using-artifacts
if: failure()
with:
name: videos
path: cypress/videos/
# We need to find the PR id. Will be used later to comment on that PR.
- name: Finding Pull Request ID
uses: jwalton/gh-find-current-pr@v1
id: pr_id_finder
if: always() # It forces the job to be always executed, even if a previous job fail.
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
# On E2E failure, add a comment to the PR with additional information, if there is an open PR for the current branch
- name: Comment PR (E2E failure)
uses: peter-evans/create-or-update-comment@v1
if: steps.pr_id_finder.outputs.number && failure()
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
body: |
:x: E2E tests **FAILED** for commit ${{ github.sha }} previously deployed at [${{ env.VERCEL_DEPLOYMENT_URL }}](${{ env.VERCEL_DEPLOYMENT_URL }})
Download artifacts (screenshots + videos) from [`checks`](https://github.com/UnlyEd/next-right-now/pull/${{ steps.pr_id_finder.outputs.number }}/checks) section
# On E2E success, add a comment to the PR, if there is an open PR for the current branch
- name: Comment PR (E2E success)
uses: peter-evans/create-or-update-comment@v1
if: steps.pr_id_finder.outputs.number && success()
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ steps.pr_id_finder.outputs.number }}
body: |
:white_check_mark: E2E tests **SUCCESS** for commit ${{ github.sha }} previously deployed at [${{ env.VERCEL_DEPLOYMENT_URL }}](${{ env.VERCEL_DEPLOYMENT_URL }})
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Run Lighthouse reports in parallel of E2E tests
run-lighthouse-tests:
name: Run LightHouse checks (Ubuntu 18.04)
runs-on: ubuntu-18.04
needs: await-for-vercel-deployment
steps:
- uses: actions/checkout@v1 # Get last commit pushed - XXX See https://github.com/actions/checkout
- name: Resolving deployment url from Vercel
# Workflow overview:
# - Resolve customer to deploy from github event input (falls back to resolving it from vercel.json file)
# - Resolve $VERCEL_DEPLOYMENT_URL
# - Fetch all deployments data (by using the scope in `vercel.json`)
# - Resolve the last url (from `response.deployments[0].url`)
# - Remove the `"` character to pre-format url
# We need to set env the url for next step, formatted as `https://${$VERCEL_DEPLOYMENT}/en` using /en endpoint to improve perfs by avoiding the url redirect on /
# XXX You can use https://jqplay.org/ if you want to play around with "jq" to manipulate JSON
run: |
apt update -y >/dev/null && apt install -y jq >/dev/null
MANUAL_TRIGGER_CUSTOMER="${{ github.event.inputs.customer}}"
echo "MANUAL_TRIGGER_CUSTOMER: " $MANUAL_TRIGGER_CUSTOMER
echo "MANUAL_TRIGGER_CUSTOMER=$MANUAL_TRIGGER_CUSTOMER" >> $GITHUB_ENV
CUSTOMER_REF_TO_DEPLOY="${MANUAL_TRIGGER_CUSTOMER:-$(cat vercel.json | jq --raw-output '.build.env.NEXT_PUBLIC_CUSTOMER_REF')}"
echo "Customer that was deployed: " $CUSTOMER_REF_TO_DEPLOY
echo "CUSTOMER_REF_TO_DEPLOY=$CUSTOMER_REF_TO_DEPLOY" >> $GITHUB_ENV
# Resolve Vercel team ID
VERCEL_TEAM_ID=`cat vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json | jq --raw-output '.scope'`
echo "Vercel team ID: " $VERCEL_TEAM_ID
echo "VERCEL_TEAM_ID=$VERCEL_TEAM_ID" >> $GITHUB_ENV
# Resolve Vercel project name
VERCEL_PROJECT_NAME=`cat vercel.$CUSTOMER_REF_TO_DEPLOY.staging.json | jq --raw-output '.name'`
echo "Vercel project name: " $VERCEL_PROJECT_NAME
echo "VERCEL_PROJECT_NAME=$VERCEL_PROJECT_NAME" >> $GITHUB_ENV
# Build Vercel API endpoint used to fetch deployments
VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT="https://api.zeit.co/v5/now/deployments?teamId=$VERCEL_TEAM_ID"
echo "Fetching Vercel deployments using API endpoint: " $VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT
# Fetch all Vercel deployment from this project
ALL_VERCEL_DEPLOYMENTS=`curl -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: Bearer ${{ secrets.VERCEL_TOKEN }}' $VERCEL_FETCH_DEPLOYMENTS_API_ENDPOINT`
echo "Vercel deployments for current team: " $ALL_VERCEL_DEPLOYMENTS
# Parse the deployments (as json) to find the latest deployment url, while stripping the double quotes
# TODO Do not use '.deployments [0].url' blindly, but filter the deployments array first to make sure to consider only the deployments where "name" is equal to $VERCEL_PROJECT_NAME
VERCEL_DEPLOYMENT=`echo $ALL_VERCEL_DEPLOYMENTS | jq '.deployments [0].url' | tr -d \"`
VERCEL_DEPLOYMENT_URL="https://$VERCEL_DEPLOYMENT"
echo "Url where to run LightHouse tests (VERCEL_DEPLOYMENT_URL): " $VERCEL_DEPLOYMENT_URL
echo "VERCEL_DEPLOYMENT_URL=$VERCEL_DEPLOYMENT_URL" >> $GITHUB_ENV
env:
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} # Passing github's secret to the worker
# In order to store reports and then upload it, we need to create the folder before any tests
- name: Create temporary folder for artifacts storage
run: mkdir /tmp/lighthouse-artifacts
# It runs the lighthouse report for a provided url and create a HTML report in the specified directory
# Action documentation: https://github.com/marketplace/actions/lighthouse-check#usage-standard-example
- name: Run Lighthouse
uses: foo-software/lighthouse-check-action@v2.0.5
id: lighthouseCheck
with: # See https://github.com/marketplace/actions/lighthouse-check#inputs for all options
accessToken: ${{ secrets.GITHUB_TOKEN }} # Providing a token to comment the PR if you are using a "pull_request" as trigger, or using "commentUrl" option.
commentUrl: https://api.github.com/repos/UnlyEd/next-right-now/commits/${{ github.sha }}/comments
prCommentEnabled: true # Whether to comment on the PR (default: true). Disabled because we use our own "Comment PR (LightHouse report)" action
prCommentSaveOld: true # If true, then add a new comment for each commit. Otherwise, update the latest comment (default: false).
outputDirectory: /tmp/lighthouse-artifacts # Used to upload artifacts.
emulatedFormFactor: all # Run LightHouse against "mobile", "desktop", or "all" devices
urls: ${{ env.VERCEL_DEPLOYMENT_URL }}, ${{ env.VERCEL_DEPLOYMENT_URL }}/en, ${{ env.VERCEL_DEPLOYMENT_URL }}/fr
locale: en
# Upload HTML report create by lighthouse, could be useful
- name: Upload artifacts
uses: actions/upload-artifact@v1
with:
name: Lighthouse reports
path: /tmp/lighthouse-artifacts
# Using a pre-build action to make the action fail if your score is too low. It can be really interesting to track a low score on a commit
# You can remove this action IF you don't want lighthouse to be a blocking point in your CI
# Official documentation: https://github.com/foo-software/lighthouse-check-status-action
- name: Handle Lighthouse Check results
uses: foo-software/lighthouse-check-status-action@v1.0.1
with:
lighthouseCheckResults: ${{ steps.lighthouseCheck.outputs.lighthouseCheckResults }}
minAccessibilityScore: "50"
minBestPracticesScore: "50"
minPerformanceScore: "30"
minProgressiveWebAppScore: "50"
minSeoScore: "50"