-
Notifications
You must be signed in to change notification settings - Fork 188
354 lines (318 loc) · 15.8 KB
/
deploy-pr.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
name: Deploy Website
on:
workflow_run:
workflows:
- Build Website
types:
- completed
jobs:
deploy:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
steps:
- name: Set Build Context File Prefix
id: build_context_file_prefix
run: |
if [[ "${{ github.event.workflow_run.event }}" == "pull_request" ]]; then
echo "build_context_prefix=pr" >> $GITHUB_ENV
elif [[ "${{ github.event.workflow_run.event }}" == "push" ]]; then
echo "build_context_prefix=push" >> $GITHUB_ENV
fi
- name: Download Build Context
id: build_context
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const workflowContextFilePrefix = process.env.build_context_prefix;
const workflowContextFileName = `${workflowContextFilePrefix}_info.zip`;
let artifactsOpts = github.rest.actions.listWorkflowRunArtifacts.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id
});
let artifacts = await github.paginate(artifactsOpts);
let prArtifact = artifacts.filter((artifact) => {
return artifact.name == workflowContextFileName
})[0];
// Build was skipped
if (!prArtifact) return '';
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: prArtifact.id,
archive_format: 'zip'
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/${workflowContextFileName}`, Buffer.from(download.data));
return workflowContextFileName;
- name: Unpack Build Information
if: steps.build_context.outputs.result != ''
run: |
unzip ${{ steps.build_context.outputs.result }}
- name: Read Build Information
id: read_build_info
if: steps.build_context.outputs.result != ''
uses: actions/github-script@v6
with:
script: |
let fs = require('fs');
const workflowContextFilePrefix = process.env.build_context_prefix;
const buildData = fs.readFileSync(`${workflowContextFilePrefix}_info.json`);
return JSON.parse(buildData);
- name: Parse Pull Request Event Information
id: pr_info
if: github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
run: |
PR_ID=$(echo '${{ steps.read_build_info.outputs.result }}' | jq '.id')
PR_ID_NO_QUOTE="${PR_ID%\"}"
PR_ID_NO_QUOTE="${PR_ID_NO_QUOTE#\"}"
echo "pr_id => $PR_ID_NO_QUOTE"
echo "pr_id=$PR_ID_NO_QUOTE" >> $GITHUB_OUTPUT
echo "pr_site=https://${{ secrets.AWS_WEBSITE }}/${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/$PR_ID_NO_QUOTE/demonstrations.html" >> $GITHUB_OUTPUT
PR_REF=$(echo '${{ steps.read_build_info.outputs.result }}' | jq '.ref')
PR_REF_NO_QUOTE="${PR_REF%\"}"
PR_REF_NO_QUOTE="${PR_REF_NO_QUOTE#\"}"
echo "pr_ref => $PR_REF_NO_QUOTE"
echo "pr_ref=$PR_REF_NO_QUOTE" >> $GITHUB_OUTPUT
PR_REF_NAME=$(echo '${{ steps.read_build_info.outputs.result }}' | jq '.ref_name')
PR_REF_NAME_NO_QUOTE="${PR_REF_NAME%\"}"
PR_REF_NAME_NO_QUOTE="${PR_REF_NAME_NO_QUOTE#\"}"
echo "pr_ref_name => $PR_REF_NAME_NO_QUOTE"
echo "pr_ref_name=$PR_REF_NAME_NO_QUOTE" >> $GITHUB_OUTPUT
- name: Parse Push Event Information
id: push_info
if: github.event.workflow_run.event == 'push' && steps.build_context.outputs.result != ''
run: |
PUSH_REF=$(echo '${{ steps.read_build_info.outputs.result }}' | jq '.ref')
PUSH_REF_NO_QUOTE="${PUSH_REF%\"}"
PUSH_REF_NO_QUOTE="${PUSH_REF_NO_QUOTE#\"}"
echo "push_ref => $PUSH_REF_NO_QUOTE"
echo "push_ref=$PUSH_REF_NO_QUOTE" >> $GITHUB_OUTPUT
PUSH_REF_NAME=$(echo '${{ steps.read_build_info.outputs.result }}' | jq '.ref_name')
PUSH_REF_NAME_NO_QUOTE="${PUSH_REF_NAME%\"}"
PUSH_REF_NAME_NO_QUOTE="${PUSH_REF_NAME_NO_QUOTE#\"}"
echo "push_ref_name_raw => $PUSH_REF_NAME_NO_QUOTE"
echo "push_ref_name_raw=$PUSH_REF_NAME_NO_QUOTE" >> $GITHUB_OUTPUT
BRANCH_NAME=${PUSH_REF_NAME_NO_QUOTE#refs/heads/}
BRANCH_NAME_FORMATTED=${BRANCH_NAME^^}
echo "push_ref_name => $BRANCH_NAME_FORMATTED"
echo "push_ref_name=$BRANCH_NAME_FORMATTED" >> $GITHUB_OUTPUT
- name: Create Deployment
id: deployment
if: github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const deploymentStage = 'in_progress';
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: '${{ steps.pr_info.outputs.pr_ref }}',
environment: 'preview',
task: 'deploy:pr-${{ steps.pr_info.outputs.pr_id }}',
required_contexts: [],
transient_environment: true,
auto_merge: false,
description: `QML doc deployment from pull request ${{ steps.pr_info.outputs.pr_id }}`
});
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.data.id,
state: deploymentStage,
log_url: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}',
environment_url: '${{ steps.pr_info.outputs.pr_site }}'
});
return deployment.data.id
- name: Download HTML
uses: actions/github-script@v6
if: steps.build_context.outputs.result != ''
with:
script: |
let fs = require('fs');
const maxRetry = 15;
const backoffDelay = (retryCount) => new Promise(resolve => setTimeout(resolve, 1000 * retryCount));
const downloadHtmlArtifact = async (artifact) => {
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip'
});
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/${artifact.name}`, Buffer.from(download.data));
};
let artifactsOpts = github.rest.actions.listWorkflowRunArtifacts.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id
});
let artifacts = await github.paginate(artifactsOpts);
let htmlArtifacts = artifacts.filter((artifact) => {
return artifact.name.startsWith('html-')
});
/*
Attempt to download the artifact one at a time and save them to disk.
In the event of an error, back-off (incrementally) and try again.
Each error increases the back-off delay by 1s, in other words:
> failure -> wait (1s) -> try-again -> failure -> wait (2s) -> try-again -> ...
This deals with network congestion and rate-limiting errors that occur if the artifact
download happens during a time of high load on GitHub Actions.
*/
for (const artifact of htmlArtifacts) {
for (let i = 1; i < maxRetry + 1; i++) {
try {
console.log(`Attempting to download artifact: ${artifact.name}`);
await downloadHtmlArtifact(artifact);
console.log(`Successfully downloaded artifact: ${artifact.name}`);
break;
} catch (e) {
console.log(`Error while trying to download artifact: ${artifact.name}, i: ${i}, error: ${e}`);
['message', 'status', 'request', 'response'].forEach((attr) => {
if (e.hasOwnProperty(attr)) {
console.log(`error_${attr}:`);
console.log(e[attr]);
}
});
if (i === maxRetry) throw new Error(e);
console.log(`Retrying download of artifact: ${artifact.name}`);
await backoffDelay(i);
}
}
}
- name: Unpack HTML
if: steps.build_context.outputs.result != ''
run: |
mkdir -p website/demos
for f in html-*.zip; do
unzip -o -d website $f
done
- name: Upload HTML (Pull Request)
if: github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run:
aws s3 sync website s3://${{ secrets.AWS_PR_S3_BUCKET_ID }}/${{ secrets.AWS_PR_BUCKET_BUILD_DIR }}/${{ steps.pr_info.outputs.pr_id }}/ --delete
- name: Upload HTML (Push to master / dev)
if: github.event.workflow_run.event == 'push' && steps.build_context.outputs.result != ''
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
BUCKET_ACTIONS_ID: ${{ format('AWS_ENV_S3_BUCKET_{0}', steps.push_info.outputs.push_ref_name) }}
run: |
echo "Got actions Bucket ID: ${{ env.BUCKET_ACTIONS_ID }}"
AWS_BUCKET_NAME=${{ secrets[env.BUCKET_ACTIONS_ID] }}
aws s3 sync website s3://$AWS_BUCKET_NAME/qml/ --delete
- name: Upload HTML (dev)
if: github.event.workflow_run.event == 'push' && steps.push_info.outputs.push_ref_name == 'dev' && steps.build_context.outputs.result != ''
uses: XanaduAI/cloud-actions/push-to-s3-and-invalidate-cloudfront@main
with:
build-directory: website
aws-cloudfront-distribution-id: ${{ secrets.PL_SITE_DEV_NON_REACT_CLOUDFRONT_DISTRIBUTION_ID }}
aws-region: ${{ secrets.AWS_REGION }}
aws-access-key-id: ${{ secrets.PL_SITE_DEV_NON_REACT_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.PL_SITE_DEV_NON_REACT_SECRET_ACCESS_KEY }}
s3-bucket: ${{ secrets.PL_SITE_DEV_S3_BUCKET_NAME }}
s3-directory: qml
s3-delete-stale-files: true
s3-action: upload
invalidate-cloudfront-cache: true
- name: Upload HTML (prod)
if: github.event.workflow_run.event == 'push' && steps.push_info.outputs.push_ref_name == 'master' && steps.build_context.outputs.result != ''
uses: XanaduAI/cloud-actions/push-to-s3-and-invalidate-cloudfront@main
with:
build-directory: website
aws-cloudfront-distribution-id: ${{ secrets.PL_SITE_PROD_NON_REACT_CLOUDFRONT_DISTRIBUTION_ID }}
aws-region: ${{ secrets.AWS_REGION }}
aws-access-key-id: ${{ secrets.PL_SITE_PROD_NON_REACT_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.PL_SITE_PROD_NON_REACT_SECRET_ACCESS_KEY }}
s3-bucket: ${{ secrets.PL_SITE_PROD_S3_BUCKET_NAME }}
s3-directory: qml
s3-delete-stale-files: true
s3-action: upload
invalidate-cloudfront-cache: true
- name: Comment on PR
if: github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
uses: actions/github-script@v6
with:
script: |
const actionsBotUserId = 41898282;
const prNumber = ${{ steps.pr_info.outputs.pr_id }};
const commentHeader = '**Thank you for opening this pull request.**'
const commentBody = `
You can find the built site [at this link](${{ steps.pr_info.outputs.pr_site }}).
**Deployment Info:**
- Pull Request ID: \`${{ steps.pr_info.outputs.pr_id }}\`
- Deployment SHA: \`${{ steps.pr_info.outputs.pr_ref }}\`
(The \`Deployment SHA\` refers to the latest commit hash the docs were built from)
**Note:** It may take several minutes for updates to this pull request to be reflected on the deployed site.
`;
const commentText = `
${commentHeader}
${commentBody}
`;
// Get the existing comments.
const {data: comments} = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber
});
// Find any comment already made by the bot.
const botComment = comments.find(comment => comment.user.id === actionsBotUserId && comment.body.trim().startsWith(commentHeader));
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: commentText
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: commentText
});
}
- name: Update Deployment (success)
if: success() && github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
uses: actions/github-script@v6
env:
DEPLOYMENT_STAGE: success
DEPLOYMENT_ID: ${{ steps.deployment.outputs.result }}
with:
script: |
const deploymentId = process.env.DEPLOYMENT_ID;
const deploymentEnv = process.env.DEPLOYMENT_ENV;
const deploymentStage = process.env.DEPLOYMENT_STAGE;
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deploymentId,
state: deploymentStage,
log_url: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}',
environment_url: '${{ steps.pr_info.outputs.pr_site }}'
});
- name: Update Deployment (failure)
if: failure() && github.event.workflow_run.event == 'pull_request' && steps.build_context.outputs.result != ''
uses: actions/github-script@v6
env:
DEPLOYMENT_STAGE: failure
DEPLOYMENT_ID: ${{ steps.deployment.outputs.result }}
with:
script: |
const deploymentId = process.env.DEPLOYMENT_ID;
const deploymentEnv = process.env.DEPLOYMENT_ENV;
const deploymentStage = process.env.DEPLOYMENT_STAGE;
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deploymentId,
state: deploymentStage,
log_url: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}',
environment_url: '${{ steps.pr_info.outputs.pr_site }}'
});