-
Notifications
You must be signed in to change notification settings - Fork 22
413 lines (413 loc) · 16.9 KB
/
pr-preview.yml
File metadata and controls
413 lines (413 loc) · 16.9 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
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
name: PR Preview
on:
schedule:
- cron: 0 0 */6 * *
issue_comment:
types: [created]
pull_request_target:
types: [labeled, synchronize, closed]
concurrency:
group: pr-preview
cancel-in-progress: false
env:
GITHUB_PR_NUMBER: ${{ github.event_name == 'issue_comment' && github.event.issue.number || github.event.pull_request.number }}
JEKYLL_ENV: production
jobs:
cache-refresh:
if: ${{ !github.event.repository.fork && github.event_name == 'schedule' && github.event.schedule == '0 0 */6 * *' }}
runs-on: ubuntu-latest
steps:
- name: Cache Refresh
uses: actions/cache/restore@v4
with:
key: pages-${{ github.run_id }}
path: /home/runner/gh-pages
restore-keys: pages-
preview-remove:
if:
${{
!github.event.repository.fork && (
( github.event_name == 'pull_request_target' && (
github.event.action == 'closed' || (
github.event.action == 'labeled' &&
github.event.label.name == 'preview/remove'
)
)
) || (
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
github.event.comment.body == '/preview remove' &&
contains('OWNER,MEMBER', github.event.comment.author_association)
)
)
}}
runs-on: ubuntu-latest
permissions:
actions: write
contents: write
pull-requests: write
steps:
- id: restore-pages
name: Restore Pages
uses: actions/cache/restore@v4
with:
key: pages-${{ github.run_id }}
path: /home/runner/gh-pages
restore-keys: pages-
- name: Prepare Pages
run: |
mkdir -p /home/runner/gh-pages/data/{files,refs}
rm -rf /home/runner/gh-pages/PR${{ env.GITHUB_PR_NUMBER }}
grep -rlw 'PR${{ env.GITHUB_PR_NUMBER }}' /home/runner/gh-pages/data/refs | xargs -r sed -i '/\<PR${{ env.GITHUB_PR_NUMBER }}\>/d'
for ref in /home/runner/gh-pages/data/refs/*; do
if [ ! -s "$ref" ]; then
base=$(basename "$ref")
rm -f "/home/runner/gh-pages/data/files/$base"
rm -f "$ref"
fi
done
echo "[$(date)] Remove Preview PR${{ env.GITHUB_PR_NUMBER }}" > /home/runner/gh-pages/index.html
- name: Save Pages
uses: actions/cache/save@v4
with:
key: pages-${{ github.run_id }}
path: /home/runner/gh-pages
- if: ${{ github.event_name == 'issue_comment' }}
id: comment-message
name: Comment Message (issue_comment)
uses: actions/github-script@v8
with:
script: |
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
issue_number: context.issue.number,
body: "[Preview] Preview has been removed."
});
- if: ${{ (github.event_name == 'pull_request_target' && github.event.action != 'closed') || steps.comment-message.outcome == 'failure' }}
name: Comment Message
uses: actions/github-script@v8
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: "[Preview] Preview has been removed."
});
- if: ${{ steps.restore-pages.outputs.cache-matched-key }}
name: Remove History Cache
uses: actions/github-script@v8
with:
script: |
github.rest.actions.deleteActionsCacheByKey({
owner: context.repo.owner,
repo: context.repo.repo,
key: "${{ steps.restore-pages.outputs.cache-matched-key }}"
});
- if: ${{ contains(github.event.pull_request.labels.*.name, 'preview/remove') || contains(github.event.issue.labels.*.name, 'preview/remove') }}
name: Remove Label (preview/remove)
continue-on-error: true
uses: actions/github-script@v8
with:
script: |
github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
name: "preview/remove"
});
- if: ${{ contains(github.event.pull_request.labels.*.name, 'preview/watch') || contains(github.event.issue.labels.*.name, 'preview/watch') }}
name: Remove Label (preview/watch)
continue-on-error: true
uses: actions/github-script@v8
with:
script: |
github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
name: "preview/watch"
});
preview-create-init:
if:
${{
!github.event.repository.fork && (
( github.event_name == 'pull_request_target' && (
( github.event.action == 'labeled' && contains(fromJSON('["preview/create", "preview/watch"]'), github.event.label.name) ) ||
( github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'preview/watch') )
)
) || (
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(fromJSON('["/preview create", "/preview watch"]'), github.event.comment.body) &&
contains('OWNER,MEMBER', github.event.comment.author_association)
)
)
}}
runs-on: ubuntu-latest
permissions:
checks: write
pull-requests: write
outputs:
sha: ${{ steps.init.outputs.sha }}
domain: ${{ steps.init.outputs.domain }}
pathname: ${{ steps.init.outputs.pathname }}
repository: ${{ steps.init.outputs.repository }}
check-id: ${{ steps.init.outputs.check-id }}
steps:
- id: init
name: Init
uses: actions/github-script@v8
with:
script: |
const { owner, repo } = context.repo;
const pages = (await octokit.rest.repos.getPages({ owner, repo })).data;
const domain = pages.cname || `${owner.toLowerCase()}.github.io`;
core.setOutput('domain', domain);
core.setOutput('pathname', (pages.cname || repo.toLowerCase() === domain) ? '' : `/${repo}`);
const pr = context.payload.pull_request || (await github.rest.pulls.get({
owner, repo,
pull_number: context.issue.number
})).data;
core.setOutput('sha', pr.head.sha);
core.setOutput('repository', pr.head.repo.full_name);
const { data: check } = await github.rest.checks.create({
owner, repo,
name: "Create preview",
head_sha: pr.head.sha,
status: "in_progress",
started_at: new Date().toISOString(),
details_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{ github.run_id }}`,
output: {
title: "Create preview by command",
summary: "Preview is being created..."
}
});
core.setOutput('check-id', check.id);
- if: ${{ github.event_name == 'issue_comment' && github.event.comment.body == '/preview watch' }}
name: Add Label (preview/watch)
continue-on-error: true
uses: actions/github-script@v8
with:
script: |
github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ["preview/watch"]
});
preview-create-build:
needs: preview-create-init
runs-on: ubuntu-latest
env:
PREVIEW_WATCH:
${{
contains(github.event.pull_request.labels.*.name, 'preview/watch') ||
contains(github.event.issue.labels.*.name, 'preview/watch') || (
github.event_name == 'issue_comment' &&
github.event.comment.body == '/preview watch'
)
}}
outputs:
artifact-id: ${{ steps.upload-site.outputs.artifact-id }}
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Merge PR
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git fetch https://github.com/${{ needs.preview-create-init.outputs.repository }} ${{ needs.preview-create-init.outputs.sha }}
git merge --squash ${{ needs.preview-create-init.outputs.sha }} --no-edit
git commit -m "Merge #${{ env.GITHUB_PR_NUMBER }} for preview build"
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.4"
bundler-cache: ${{ env.PREVIEW_WATCH }}
cache-version: PR-${{ env.GITHUB_PR_NUMBER }}
- name: Jekyll Build
run: |
echo "url: https://${{ needs.preview-create-init.outputs.domain }}" > _action.yml
echo "baseurl: ${{ needs.preview-create-init.outputs.pathname }}/PR${{ env.GITHUB_PR_NUMBER }}" >> _action.yml
echo "source: ${{ github.workspace }}" >> _action.yml
echo "destination: /home/runner/site" >> _action.yml
echo "preview:" >> _action.yml
echo " pr-number: ${{ env.GITHUB_PR_NUMBER }}" >> _action.yml
${{ env.PREVIEW_WATCH }} || bundle install --jobs 4
bundle exec jekyll build --trace --config _config.yml,_action.yml
- id: upload-site
name: Upload Site
uses: actions/upload-artifact@v4
with:
name: site
path: /home/runner/site
preview-create-deploy:
needs: [preview-create-init, preview-create-build]
runs-on: ubuntu-latest
environment:
name: github-pages
url: https://${{ needs.preview-create-init.outputs.domain }}${{ needs.preview-create-init.outputs.pathname }}/PR${{ env.GITHUB_PR_NUMBER }}
permissions:
actions: write
pages: write
id-token: write
contents: write
steps:
- id: restore-pages
name: Restore Pages
uses: actions/cache/restore@v4
with:
key: pages-${{ github.run_id }}
path: /home/runner/gh-pages
restore-keys: pages-
- name: Merge Site Before
run: |
mkdir -p /home/runner/gh-pages/data/{files,refs}
rm -rf /home/runner/gh-pages/PR${{ env.GITHUB_PR_NUMBER }}
grep -rlw 'PR${{ env.GITHUB_PR_NUMBER }}' /home/runner/gh-pages/data/refs | xargs -r sed -i '/\<PR${{ env.GITHUB_PR_NUMBER }}\>/d'
- name: Merge Site
uses: actions/download-artifact@v5
with:
name: site
path: /home/runner/gh-pages/PR${{ env.GITHUB_PR_NUMBER }}
- name: Merge Site After
run: |
find /home/runner/gh-pages/PR${{ env.GITHUB_PR_NUMBER }} -type f | while read file; do
hash=$(sha256sum "$file" | cut -d' ' -f1)
size=$(stat -c%s "$file")
key="${hash}-${size}"
target="/home/runner/gh-pages/data/files/$key"
if [ ! -f "$target" ]; then
mv "$file" "$target"
else
rm -f "$file"
fi
ln -s "$(realpath --relative-to="$(dirname "$file")" "$target")" "$file"
echo "PR${{ env.GITHUB_PR_NUMBER }}" >> "/home/runner/gh-pages/data/refs/$key"
done
for ref in /home/runner/gh-pages/data/refs/*; do
if [ ! -s "$ref" ]; then
base=$(basename "$ref")
rm -f "/home/runner/gh-pages/data/files/$base"
rm -f "$ref"
fi
done
echo "[$(date)] Preview PR${{ env.GITHUB_PR_NUMBER }}" > /home/runner/gh-pages/index.html
- id: upload-pages
name: Upload Pages
uses: actions/upload-pages-artifact@v4
with:
name: github-pages
path: /home/runner/gh-pages
- name: Deploy Pages
uses: actions/deploy-pages@v4
with:
artifact_name: github-pages
- name: Save Pages
uses: actions/cache/save@v4
with:
key: pages-${{ github.run_id }}
path: /home/runner/gh-pages
- if: ${{ steps.restore-pages.outputs.cache-matched-key }}
name: Remove History Cache
uses: actions/github-script@v8
with:
script: |
github.rest.actions.deleteActionsCacheByKey({
owner: context.repo.owner,
repo: context.repo.repo,
key: "${{ steps.restore-pages.outputs.cache-matched-key }}"
});
- if: ${{ steps.upload-pages.outputs.artifact_id }}
name: Remove Pages Artifact
uses: actions/github-script@v8
with:
script: |
github.rest.actions.deleteArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: "${{ steps.upload-pages.outputs.artifact_id }}",
});
- if: ${{ needs.preview-create-build.outputs.artifact-id }}
name: Remove Temp Artifact
uses: actions/github-script@v8
with:
script: |
github.rest.actions.deleteArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: "${{ needs.preview-create-build.outputs.artifact-id }}"
});
preview-create-finally:
if: ${{ always() && needs.preview-create-init.result == 'success' }}
needs: [preview-create-init, preview-create-deploy]
runs-on: ubuntu-latest
permissions:
checks: write
pull-requests: write
steps:
- if: ${{ github.event_name == 'issue_comment' }}
id: comment-message
name: Comment Message (issue_comment)
env:
PREVIEW_URL: https://${{ needs.preview-create-init.outputs.domain }}${{ needs.preview-create-init.outputs.pathname }}/PR${{ env.GITHUB_PR_NUMBER }}
continue-on-error: true
uses: actions/github-script@v8
with:
script: |
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
issue_number: context.issue.number,
body: "${{ needs.preview-create-deploy.result == 'success' && format('[Preview] {0}', env.PREVIEW_URL) || '[Preview] Failed, see logs for more information.' }}"
});
- if: ${{ steps.comment-message.outcome == 'failure' }}
name: Comment Message
env:
PREVIEW_URL: https://${{ needs.preview-create-init.outputs.domain }}${{ needs.preview-create-init.outputs.pathname }}/PR${{ env.GITHUB_PR_NUMBER }}
uses: actions/github-script@v8
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: "${{ needs.preview-create-deploy.result == 'success' && format('[Preview] {0}', env.PREVIEW_URL) || '[Preview] Failed, see logs for more information.' }}"
});
- if: ${{ contains(github.event.pull_request.labels.*.name, 'preview/create') || contains(github.event.issue.labels.*.name, 'preview/create') }}
name: Remove Label (preview/create)
continue-on-error: true
uses: actions/github-script@v8
with:
script: |
github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
name: "preview/create"
});
- name: Check Over
env:
PREVIEW_URL: https://${{ needs.preview-create-init.outputs.domain }}${{ needs.preview-create-init.outputs.pathname }}/PR${{ env.GITHUB_PR_NUMBER }}
uses: actions/github-script@v8
with:
script: |
github.rest.checks.update({
owner: context.repo.owner,
repo: context.repo.repo,
check_run_id: ${{ needs.preview-create-init.outputs.check-id }},
status: "completed",
conclusion: "${{ needs.preview-create-deploy.result == 'success' && 'success' || 'failure' }}",
completed_at: new Date().toISOString(),
details_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/runs/${{ github.run_id }}`,
output: {
title: "Create preview ${{ needs.preview-create-deploy.result == 'success' && 'success' || 'failure' }}",
summary: "${{ needs.preview-create-deploy.result == 'success' && format('[Preview] {0}', env.PREVIEW_URL) || '[Preview] Failed, see logs for more information.' }}"
}
});