-
Notifications
You must be signed in to change notification settings - Fork 8
154 lines (132 loc) · 4.61 KB
/
Copy pathpreview.yml
File metadata and controls
154 lines (132 loc) · 4.61 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
name: Preview Documentation
on:
issue_comment:
types:
- created
permissions:
contents: read
issues: write
pull-requests: write
concurrency:
group: preview-pr-${{ github.event.issue.number }}
cancel-in-progress: true
jobs:
preview:
name: Build and serve preview
if: >-
github.event.issue.pull_request &&
(
github.event.comment.body == 'preview' ||
github.event.comment.body == '/preview'
) &&
contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.comment.author_association)
runs-on: ubuntu-latest
timeout-minutes: 360
steps:
- name: Authorize preview request
uses: actions/github-script@v7
with:
script: |
const actor = context.payload.comment.user.login;
const { owner, repo } = context.repo;
let permission = 'none';
try {
const { data } = await github.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username: actor,
});
permission = data.permission;
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
const allowed = new Set(['admin', 'maintain', 'write']);
if (!allowed.has(permission)) {
core.setFailed(
`Preview builds are limited to repository members with write access. ` +
`${actor} has ${permission} access.`
);
}
- name: Get pull request
id: pr
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const pull_number = context.payload.issue.number;
const { data: pull } = await github.rest.pulls.get({
owner,
repo,
pull_number,
});
core.setOutput('head_repo', pull.head.repo.full_name);
core.setOutput('head_ref', pull.head.ref);
core.setOutput('head_sha', pull.head.sha);
- name: React to preview request
uses: actions/github-script@v7
continue-on-error: true
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket',
});
- name: Checkout pull request
uses: actions/checkout@v4
with:
repository: ${{ steps.pr.outputs.head_repo }}
ref: ${{ steps.pr.outputs.head_sha }}
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "yarn"
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Build documentation
run: yarn build
- name: Install cloudflared
run: |
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb
sudo dpkg -i cloudflared.deb
- name: Start Docusaurus server
run: |
nohup yarn serve --host 0.0.0.0 --port 3000 > docusaurus-preview.log 2>&1 &
- name: Start preview tunnel
id: tunnel
run: |
nohup cloudflared tunnel --url http://localhost:3000 --no-autoupdate > cloudflared-preview.log 2>&1 &
for attempt in {1..30}; do
preview_url="$(grep -o 'https://[^ ]*trycloudflare.com' cloudflared-preview.log | head -n 1 || true)"
if [ -n "$preview_url" ]; then
echo "url=$preview_url" >> "$GITHUB_OUTPUT"
exit 0
fi
sleep 2
done
echo "Failed to start preview tunnel."
cat cloudflared-preview.log
exit 1
- name: Comment preview URL
uses: actions/github-script@v7
env:
PREVIEW_URL: ${{ steps.tunnel.outputs.url }}
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: [
`Preview: ${process.env.PREVIEW_URL}`,
'',
'This preview will stay live for up to 6 hours. Comment `preview` again to replace it with a fresh build.',
].join('\n'),
});
- name: Keep preview live for up to six hours
run: sleep 21600