-
Notifications
You must be signed in to change notification settings - Fork 0
336 lines (286 loc) · 9.83 KB
/
Copy pathdeploy.yml
File metadata and controls
336 lines (286 loc) · 9.83 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
name: Build, Deploy, and On-Demand Preview
"on":
push:
branches:
- main
pull_request:
branches:
- main
issue_comment:
types:
- created
env:
PREVIEW_ROOT: pr-preview
jobs:
build-pr:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Install uv
uses: astral-sh/setup-uv@v2
- name: Install dependencies
run: uv sync
- name: Build site
run: uv run mkdocs build --strict
deploy-main:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: write
concurrency:
group: gh-pages
cancel-in-progress: false
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Install uv
uses: astral-sh/setup-uv@v2
- name: Install dependencies
run: uv sync
- name: Build site
run: uv run mkdocs build --strict
- name: Checkout gh-pages
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
git init gh-pages
cd gh-pages
git remote add origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
if git ls-remote --exit-code --heads origin gh-pages > /dev/null; then
git fetch --depth=1 origin gh-pages
git checkout -B gh-pages FETCH_HEAD
else
git checkout --orphan gh-pages
fi
- name: Deploy main site to gh-pages
run: |
set -euo pipefail
find gh-pages -mindepth 1 -maxdepth 1 \
! -name .git \
! -name "${PREVIEW_ROOT}" \
-exec rm -rf {} +
cp -a site/. gh-pages/
cd gh-pages
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add --all
if git diff --cached --quiet; then
echo "No gh-pages changes to deploy."
else
git commit -m "Deploy main site"
git push origin HEAD:gh-pages
fi
preview-command:
if: >-
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.comment.author_association) &&
(
startsWith(github.event.comment.body, '/preview') ||
startsWith(github.event.comment.body, '/unpreview')
)
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
action: ${{ steps.preview.outputs.action }}
head_sha: ${{ steps.preview.outputs.head_sha }}
pr_number: ${{ steps.preview.outputs.pr_number }}
preview_url: ${{ steps.preview.outputs.preview_url }}
steps:
- name: Read command and PR metadata
id: preview
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.issue.number }}
run: |
set -euo pipefail
body="$(jq -r '.comment.body' "$GITHUB_EVENT_PATH" | tr -d '\r')"
body="$(printf '%s' "$body" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
case "$body" in
/preview)
action=deploy
;;
/unpreview)
action=remove
;;
*)
action=ignore
;;
esac
repo_name="${GITHUB_REPOSITORY#*/}"
if [ "$repo_name" = "${GITHUB_REPOSITORY_OWNER}.github.io" ]; then
preview_url="https://${repo_name}/${PREVIEW_ROOT}/pr-${PR_NUMBER}/"
else
preview_url="https://${GITHUB_REPOSITORY_OWNER}.github.io/${repo_name}/${PREVIEW_ROOT}/pr-${PR_NUMBER}/"
fi
{
echo "action=${action}"
echo "pr_number=${PR_NUMBER}"
echo "preview_url=${preview_url}"
} >> "$GITHUB_OUTPUT"
if [ "$action" != "deploy" ]; then
exit 0
fi
pr_json="$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}")"
head_repo="$(printf '%s' "$pr_json" | jq -r '.head.repo.full_name')"
base_repo="$(printf '%s' "$pr_json" | jq -r '.base.repo.full_name')"
head_sha="$(printf '%s' "$pr_json" | jq -r '.head.sha')"
if [ "$head_repo" != "$base_repo" ]; then
echo "::error::PR previews are only supported for branches in this repository."
exit 1
fi
if [ -z "$head_sha" ] || [ "$head_sha" = "null" ]; then
echo "::error::Could not determine the PR head SHA."
exit 1
fi
echo "head_sha=${head_sha}" >> "$GITHUB_OUTPUT"
build-preview:
if: needs.preview-command.outputs.action == 'deploy'
needs: preview-command
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout PR head
uses: actions/checkout@v4
with:
ref: ${{ needs.preview-command.outputs.head_sha }}
persist-credentials: false
- name: Install uv
uses: astral-sh/setup-uv@v2
- name: Install dependencies
run: uv sync
- name: Build site
run: uv run mkdocs build --strict
- name: Upload preview artifact
uses: actions/upload-artifact@v4
with:
name: pr-preview-${{ needs.preview-command.outputs.pr_number }}
path: site
if-no-files-found: error
deploy-preview:
if: needs.preview-command.outputs.action == 'deploy'
needs:
- preview-command
- build-preview
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
pull-requests: write
concurrency:
group: gh-pages
cancel-in-progress: false
steps:
- name: Checkout gh-pages
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
git init gh-pages
cd gh-pages
git remote add origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
if git ls-remote --exit-code --heads origin gh-pages > /dev/null; then
git fetch --depth=1 origin gh-pages
git checkout -B gh-pages FETCH_HEAD
else
git checkout --orphan gh-pages
fi
- name: Download preview artifact
uses: actions/download-artifact@v4
with:
name: pr-preview-${{ needs.preview-command.outputs.pr_number }}
path: preview-site
- name: Deploy PR preview
env:
PR_NUMBER: ${{ needs.preview-command.outputs.pr_number }}
run: |
set -euo pipefail
preview_dir="gh-pages/${PREVIEW_ROOT}/pr-${PR_NUMBER}"
rm -rf "$preview_dir"
mkdir -p "$preview_dir"
cp -a preview-site/. "$preview_dir"/
cd gh-pages
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add --all "${PREVIEW_ROOT}"
if git diff --cached --quiet; then
echo "No preview changes to deploy."
else
git commit -m "Deploy PR ${PR_NUMBER} preview"
git push origin HEAD:gh-pages
fi
- name: Comment with preview URL
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ needs.preview-command.outputs.pr_number }}
PREVIEW_URL: ${{ needs.preview-command.outputs.preview_url }}
run: |
set -euo pipefail
marker="<!-- pr-preview:${PR_NUMBER} -->"
body="$(printf '%s\n%s\n' "$marker" "Preview published: ${PREVIEW_URL}")"
gh pr comment "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --body "$body"
remove-preview:
if: needs.preview-command.outputs.action == 'remove'
needs: preview-command
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
pull-requests: write
concurrency:
group: gh-pages
cancel-in-progress: false
steps:
- name: Checkout gh-pages
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
git init gh-pages
cd gh-pages
git remote add origin "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
if git ls-remote --exit-code --heads origin gh-pages > /dev/null; then
git fetch --depth=1 origin gh-pages
git checkout -B gh-pages FETCH_HEAD
else
git checkout --orphan gh-pages
fi
- name: Remove PR preview
env:
PR_NUMBER: ${{ needs.preview-command.outputs.pr_number }}
run: |
set -euo pipefail
preview_dir="gh-pages/${PREVIEW_ROOT}/pr-${PR_NUMBER}"
rm -rf "$preview_dir"
mkdir -p "gh-pages/${PREVIEW_ROOT}"
cd gh-pages
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add --all "${PREVIEW_ROOT}"
if git diff --cached --quiet; then
echo "No preview changes to remove."
else
git commit -m "Remove PR ${PR_NUMBER} preview"
git push origin HEAD:gh-pages
fi
- name: Comment that preview was removed
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ needs.preview-command.outputs.pr_number }}
run: |
set -euo pipefail
marker="<!-- pr-preview:${PR_NUMBER} -->"
body="$(printf '%s\n%s\n' "$marker" "Preview removed for PR #${PR_NUMBER}.")"
gh pr comment "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --body "$body"