-
Notifications
You must be signed in to change notification settings - Fork 17
106 lines (94 loc) · 3.43 KB
/
docs-preview.yml
File metadata and controls
106 lines (94 loc) · 3.43 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
name: Documentation Preview
on:
pull_request:
paths:
- 'docs-site/**'
- 'docs/**'
- '*.md'
- '.github/workflows/docs-preview.yml'
permissions:
contents: read
pull-requests: write
concurrency:
group: docs-preview-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
build-preview:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4
- name: Setup Node.js
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: docs-site/package-lock.json
- name: Install dependencies
run: |
cd docs-site
npm ci
- name: Build documentation
id: build
continue-on-error: true
run: |
cd docs-site
npm run build
- name: Upload preview artifact
if: steps.build.outcome == 'success'
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: docs-preview-pr-${{ github.event.pull_request.number }}
path: docs-site/dist
retention-days: 7
- name: Comment on PR
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v7
with:
script: |
const artifactUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const buildOutcome = '${{ steps.build.outcome }}';
const body = buildOutcome === 'success'
? [
'## Documentation Preview',
'',
`Documentation has been built for this PR.`,
'',
`**[Download preview artifact](${artifactUrl})**`,
'',
'To view locally:',
'1. Download the `docs-preview-pr-${{ github.event.pull_request.number }}` artifact from the workflow run',
'2. Unzip and open `index.html` in your browser',
'',
`_Built from commit ${context.sha.substring(0, 7)}_`,
].join('\n')
: [
'## Documentation Preview',
'',
`Documentation build failed for this PR. [View logs](${artifactUrl}).`,
'',
`_Built from commit ${context.sha.substring(0, 7)}_`,
].join('\n');
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('Documentation Preview')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}