Repository navigation
120 lines (105 loc) · 4.18 KB
/
Copy pathpreview.yml
File metadata and controls
120 lines (105 loc) · 4.18 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
name: Preview Documentation
on:
issue_comment:
types:
- created
permissions: {}
jobs:
authorize:
name: Authorize and acknowledge 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: 5
permissions:
contents: read
issues: write
pull-requests: write
outputs:
head_repo: ${{ steps.request.outputs.head_repo }}
head_sha: ${{ steps.request.outputs.head_sha }}
steps:
# issue_comment uses the default branch SHA. This job never checks out PR code.
- name: Checkout trusted authorization code
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
ref: ${{ github.sha }}
persist-credentials: false
- name: Authorize preview request
id: request
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const authorize = require('./.github/scripts/authorize-preview.cjs');
await authorize({ github, context, core });
preview:
name: Build and serve preview
needs: authorize
runs-on: ubuntu-latest
timeout-minutes: 360
permissions:
contents: read
# Only authorized requests reach this job and can replace an existing preview.
concurrency:
group: preview-pr-${{ github.event.issue.number }}
cancel-in-progress: true
env:
CLOUDFLARED_VERSION: "2026.10.4"
CLOUDFLARED_SHA256: "ae5e8add7884b0531d66bbca4570d1de491ff6da51e4ebe7eb160cdf241d9e42"
steps:
- name: Install verified cloudflared
run: |
curl --fail --location --retry 3 \
"https://github.com/cloudflare/cloudflared/releases/download/${CLOUDFLARED_VERSION}/cloudflared-linux-amd64" \
--output "$RUNNER_TEMP/cloudflared"
echo "$CLOUDFLARED_SHA256 $RUNNER_TEMP/cloudflared" | sha256sum --check --strict
chmod +x "$RUNNER_TEMP/cloudflared"
- name: Checkout pull request
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
repository: ${{ needs.authorize.outputs.head_repo }}
ref: ${{ needs.authorize.outputs.head_sha }}
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
with:
node-version-file: ".nvmrc"
# Preview code does not need the default branch's dependency cache.
- name: Install dependencies
run: yarn install --immutable
- name: Build documentation
run: yarn build
- name: Start Docusaurus server
run: |
nohup yarn serve --host 0.0.0.0 --port 3000 > docusaurus-preview.log 2>&1 &
- name: Start preview tunnel
env:
PREVIEW_SHA: ${{ needs.authorize.outputs.head_sha }}
run: |
nohup "$RUNNER_TEMP/cloudflared" tunnel --url http://localhost:3000 --no-autoupdate > cloudflared-preview.log 2>&1 &
for attempt in {1..30}; do
preview_url="$(grep -Eo 'https://[a-z0-9-]+\.trycloudflare\.com' cloudflared-preview.log | head -n 1 || true)"
if [ -n "$preview_url" ]; then
# Job summaries appear after the job ends. Keep the live URL in its log.
echo "Preview URL: $preview_url"
{
echo "## Documentation preview"
printf '\n[Open preview](%s)\n\n' "$preview_url"
printf 'Commit: `%s`\n\n' "$PREVIEW_SHA"
echo "Available for up to six hours. Comment preview on the PR again to replace it."
} >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
sleep 2
done
echo "Failed to start preview tunnel."
cat cloudflared-preview.log
exit 1
# No GitHub write token or privileged action is introduced after PR code runs.
- name: Keep preview live for up to six hours
run: sleep 21600