-
Notifications
You must be signed in to change notification settings - Fork 1
354 lines (325 loc) · 13.6 KB
/
Copy pathdocs-preview-deploy.yml
File metadata and controls
354 lines (325 loc) · 13.6 KB
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: Docs preview deploy
"on":
workflow_run:
workflows:
- Docs preview build
types:
- completed
permissions:
actions: read
contents: write
pages: write
pull-requests: write
id-token: write
concurrency:
group: github-pages
queue: max
jobs:
deploy-preview:
name: Publish documentation preview
if: >-
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.pages.outputs.page_url }}
steps:
- name: Resolve trusted preview request
id: request
uses: actions/github-script@v8
with:
script: |
const run = context.payload.workflow_run;
const { data: artifacts } = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: run.id,
per_page: 100,
});
const requests = artifacts.artifacts.flatMap((artifact) => {
const match = artifact.name.match(/^docs-preview-([1-9][0-9]*)-(deploy|remove)$/);
return match ? [{ artifact, number: Number(match[1]), operation: match[2] }] : [];
});
if (requests.length !== 1) {
core.setFailed(`Expected one preview request artifact, found ${requests.length}.`);
return;
}
const request = requests[0];
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: request.number,
});
const associated = run.pull_requests.some(({ number }) => number === request.number);
const sameHead = pullRequest.head.sha === run.head_sha;
const sameRepository = pullRequest.head.repo?.full_name === run.head_repository?.full_name;
if (!associated && (!sameHead || !sameRepository)) {
core.setFailed('Preview request is not associated with the completed workflow run.');
return;
}
const internalHead = `${context.repo.owner}/${context.repo.repo}`;
if (run.head_repository?.full_name !== internalHead ||
pullRequest.head.repo?.full_name !== internalHead) {
core.notice('Hosted previews are limited to trusted repository branches.');
core.setOutput('operation', 'none');
return;
}
const dependabot = pullRequest.user?.login === 'dependabot[bot]';
if (dependabot) {
core.notice('Dependabot previews are not published on the production origin.');
}
if (pullRequest.state === 'open' && !sameHead) {
core.notice('A newer pull request revision exists; ignoring this stale preview request.');
core.setOutput('operation', 'none');
return;
}
let operation = 'remove';
if (pullRequest.state === 'open' && !dependabot) {
const files = await github.paginate(github.rest.pulls.listFiles, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: request.number,
per_page: 100,
});
const exactInputs = new Set([
'.github/workflows/docs.yml',
'.github/workflows/docs-preview.yml',
'.github/workflows/docs-preview-deploy.yml',
'requirements-docs.txt',
'scripts/build-docs.sh',
'scripts/publish-agent-markdown.py',
'scripts/render-dev-notes.py',
'scripts/stage-egress-gate-docs.py',
'tests/test_agent_markdown.py',
'tests/test_docs_404.py',
'tests/test_render_dev_notes.py',
'tests/test_stage_egress_gate_docs.py',
'zensical.toml',
]);
const docsChanged = files.some(
({ filename }) => filename.startsWith('docs/') ||
filename.startsWith('overrides/') ||
filename.startsWith('projects/egress-gate/docs/') ||
exactInputs.has(filename),
);
operation = docsChanged ? 'deploy' : 'remove';
}
if (operation === 'deploy' && request.operation !== 'deploy') {
core.notice('The current pull request needs a newer build; ignoring this request.');
core.setOutput('operation', 'none');
return;
}
if (operation === 'remove') {
let previewStored = true;
try {
await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: `pr-preview/pr-${request.number}`,
ref: 'gh-pages',
});
} catch (error) {
if (error.status === 404) {
previewStored = false;
} else {
throw error;
}
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: request.number,
per_page: 100,
});
const activeComment = comments.some(
(comment) => comment.user?.type === 'Bot' &&
comment.body?.includes('<!-- docs-preview -->') &&
comment.body?.includes('View the deployed preview'),
);
if (!previewStored && !activeComment) {
core.notice('No published preview exists; nothing to remove.');
core.setOutput('operation', 'none');
return;
}
}
core.setOutput('artifact', request.artifact.name);
core.setOutput('number', String(request.number));
core.setOutput('operation', operation);
- name: Checkout deployment tooling context
if: steps.request.outputs.operation != 'none'
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Download preview site
if: steps.request.outputs.operation == 'deploy'
uses: actions/download-artifact@v8
with:
name: ${{ steps.request.outputs.artifact }}
path: preview-site
repository: ${{ github.repository }}
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Create empty removal directory
if: steps.request.outputs.operation == 'remove'
run: mkdir preview-site
- name: Update composite Pages branch
if: steps.request.outputs.operation != 'none'
uses: JamesIves/github-pages-deploy-action@4a3abc783e1a24aeb44c16e869ad83caf6b4cc23 # v4.7.4
with:
branch: gh-pages
folder: preview-site
target-folder: pr-preview/pr-${{ steps.request.outputs.number }}
force: false
attempt-limit: 10
- name: Checkout composite Pages site
if: steps.request.outputs.operation != 'none'
uses: actions/checkout@v7
with:
ref: gh-pages
path: published-site
persist-credentials: false
- name: Upload Pages artifact
if: steps.request.outputs.operation != 'none'
uses: actions/upload-pages-artifact@v5
with:
path: published-site
- name: Deploy to GitHub Pages
if: steps.request.outputs.operation != 'none'
id: pages
uses: actions/deploy-pages@v5
- name: Add or update preview comment
if: steps.request.outputs.operation != 'none'
uses: actions/github-script@v8
env:
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
OPERATION: ${{ steps.request.outputs.operation }}
PAGE_URL: ${{ steps.pages.outputs.page_url }}
PR_NUMBER: ${{ steps.request.outputs.number }}
with:
script: |
const marker = '<!-- docs-preview -->';
const number = Number(process.env.PR_NUMBER);
const rootUrl = process.env.PAGE_URL.replace(/\/?$/, '/');
const previewUrl = `${rootUrl}pr-preview/pr-${number}/`;
const body = process.env.OPERATION === 'deploy'
? `${marker}\n### Documentation preview\n\n[View the deployed preview](${previewUrl})` +
`\n\nBuilt from \`${process.env.HEAD_SHA.slice(0, 7)}\`.`
: `${marker}\n### Documentation preview\n\nThe preview has been removed.`;
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: number,
per_page: 100,
});
const existing = comments.find(
(comment) => comment.user?.type === 'Bot' && comment.body?.includes(marker),
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else if (process.env.OPERATION === 'deploy') {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: number,
body,
});
}
core.setOutput('url', previewUrl);
- name: Mark an existing preview as stale after deployment failure
if: failure() && steps.request.outputs.number != ''
uses: actions/github-script@v8
env:
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
PR_NUMBER: ${{ steps.request.outputs.number }}
with:
script: |
const number = Number(process.env.PR_NUMBER);
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: number,
});
if (pullRequest.state !== 'open' || pullRequest.head.sha !== process.env.HEAD_SHA) {
return;
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: number,
per_page: 100,
});
const existing = comments.find(
(comment) => comment.user?.type === 'Bot' &&
comment.body?.includes('<!-- docs-preview -->'),
);
if (!existing) {
return;
}
const body = existing.body.replace(/\n\n> ⚠️ Latest preview update failed[\s\S]*$/, '') +
`\n\n> ⚠️ Latest preview update failed for ` +
`\`${process.env.HEAD_SHA.slice(0, 7)}\`; the linked preview may be stale. ` +
`[View workflow logs](${context.serverUrl}/${context.repo.owner}/` +
`${context.repo.repo}/actions/runs/${context.runId}).`;
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
mark-build-failure:
name: Mark failed preview build
if: >-
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion != 'success'
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Mark an existing preview as stale
uses: actions/github-script@v8
with:
script: |
const run = context.payload.workflow_run;
const association = run.pull_requests[0];
const internalHead = `${context.repo.owner}/${context.repo.repo}`;
if (!association || run.head_repository?.full_name !== internalHead) {
return;
}
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: association.number,
});
if (pullRequest.user?.login === 'dependabot[bot]' ||
pullRequest.state !== 'open' || pullRequest.head.sha !== run.head_sha) {
return;
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: association.number,
per_page: 100,
});
const existing = comments.find(
(comment) => comment.user?.type === 'Bot' &&
comment.body?.includes('<!-- docs-preview -->'),
);
if (!existing) {
return;
}
const body = existing.body.replace(/\n\n> ⚠️ Latest preview update failed[\s\S]*$/, '') +
`\n\n> ⚠️ Latest preview update failed for ` +
`\`${run.head_sha.slice(0, 7)}\`; the linked preview may be stale. ` +
`[View workflow logs](${run.html_url}).`;
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});