Repository navigation
Expand file tree
/
Copy pathdeploy
More file actions
executable file
·396 lines (367 loc) · 17.7 KB
/
Copy pathdeploy
File metadata and controls
executable file
·396 lines (367 loc) · 17.7 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
#!/usr/bin/env bash
# Publish the wiki to https://neotokyorebuild.github.io/ntre-wiki/, and manage the versions on
# the gh-pages branch of NeotokyoRebuild/ntre-wiki. The 🚀 Deploy workflow runs the first two.
#
# ./deploy [minor] [TITLE] deploy the next minor version after latest: 0.3 -> 0.4
# ./deploy major [TITLE] deploy the next major version after latest: 0.4 -> 1.0
# ./deploy M.m [TITLE] deploy that exact version, replacing it if it exists
# ./deploy rollback VERSION point `latest` at a deployed version, without a build
# ./deploy list list the deployed versions
# ./deploy delete VERSION... delete versions from the site, after asking
# ./deploy maintenance on put up the maintenance page and hide the wiki
# ./deploy maintenance off take the maintenance page down and bring the wiki back
# ./deploy --dry-run ... any of these but list, without the push
#
# minor and major count from the version `latest` points at (the first deploy is 0.1, or
# 1.0 for major), and skip versions that already exist by applying the same step again:
# with latest at 0.1 and 0.2 deployed, the next minor is 0.3. An exact version must be
# M.m: two whole numbers without leading zeros, such as 0.4 or 1.10.
#
# A deploy only runs from a clean tree whose HEAD is on main in NeotokyoRebuild/ntre-wiki, and
# only after ./check passes. A new version identical to what `latest` serves is not
# deployed. The ⏪ Rollback workflow runs rollback. Delete never touches
# the version `latest` points at. The published address is set here and nowhere else
# (override with NTRE_WIKI_URL).
#
# The 🚧 Maintenance workflow runs maintenance. `on` moves the whole site into offline/ and
# puts wiki/maintenance.html at the root, as index.html and as 404.html, so every address
# into the wiki shows the page. The wiki still works at offline/latest/ for anyone who
# knows the address: it is out of the way, not secret. `off` moves the site back exactly as
# it was. While the page is up, deploy, rollback, list and delete work on the wiki under
# offline/, and the page stays up. A run that finds nothing to change still checks that
# GitHub Pages published the branch, and asks Pages to build it again if it didn't.
set -euo pipefail
cd "$(dirname "$0")"
DRY_RUN=0
if [[ ${1:-} == --dry-run ]]; then DRY_RUN=1; shift; fi
REPO_SLUG=NeotokyoRebuild/ntre-wiki
BRANCH=gh-pages
HIDDEN=offline
export NTRE_WIKI_URL=${NTRE_WIKI_URL:-https://neotokyorebuild.github.io/ntre-wiki/}
export NO_MKDOCS_2_WARNING=1
say() { printf '\033[1m==> %s\033[0m\n' "$*"; }
die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; }
# Only maintenance runs without mike, so its workflow can skip the setup.
[[ ${1:-} == maintenance || -x wiki/.venv/bin/mike ]] || die "no Python environment yet: run ./setup"
export PATH="$PWD/wiki/.venv/bin:$PATH"
# The remote for NeotokyoRebuild/ntre-wiki, found by URL: origin in CI, often upstream locally.
find_remote() {
REMOTE=
for r in $(git remote); do
if [[ $(git remote get-url --push "$r") =~ github\.com[:/]$REPO_SLUG(\.git)?$ ]]; then REMOTE=$r; return; fi
done
die "no remote for github.com/$REPO_SLUG: git remote add upstream git@github.com:$REPO_SLUG.git"
}
# Bring the local gh-pages up to date; fails rather than overwrite a local branch that
# differs. Sets BASE (the remote tip, empty before the first deploy), RESET (how to
# undo a local commit on top of it), SITE (where the wiki is on the branch: the root, or
# $HIDDEN/ while the maintenance page is up) and MIKE (the options every mike command takes,
# which point it at SITE).
fetch_pages() {
say "Fetching $REMOTE/$BRANCH"
if git ls-remote --exit-code --heads "$REMOTE" "$BRANCH" >/dev/null; then
git fetch -q "$REMOTE" "$BRANCH:$BRANCH" \
|| die "local $BRANCH isn't behind $REMOTE/$BRANCH: delete it (git branch -D $BRANCH) and run again"
BASE=$(git rev-parse "$BRANCH")
RESET="git branch -f $BRANCH $BASE"
else
! git rev-parse -q --verify "refs/heads/$BRANCH" >/dev/null \
|| die "$REMOTE has no $BRANCH yet, but a local one exists: delete it (git branch -D $BRANCH) so the first deploy starts empty"
BASE=
RESET="git branch -D $BRANCH"
fi
SITE=
MIKE=(--remote "$REMOTE" --branch "$BRANCH")
if [[ -n $BASE ]] && git rev-parse -q --verify "$BASE:$HIDDEN" >/dev/null; then
SITE=$HIDDEN/
MIKE+=(--deploy-prefix "$HIDDEN")
say "The maintenance page is up: working on the wiki under $SITE"
fi
}
versions_json() { if [[ -n $BASE ]]; then git show "$BRANCH:${SITE}versions.json"; else echo '[]'; fi; }
# A version is M.m: two whole numbers without leading zeros (0.4, 1.10; not 01.2 or 1.2.3).
is_version() { [[ $1 =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; }
# A regex matching any of the given versions exactly, as a path segment: 0.1 -> 0\.1
versions_re() { local out= v; for v in "$@"; do out+=${out:+|}${v//./\\.}; done; printf '%s' "$out"; }
# Guard rails for the local commit: no symlinks (GitHub Pages rejects them), a fast-forward
# of the remote, and every changed path matching $1, if given.
check_result() {
say "Checking the result"
local links outside
links=$(git ls-tree -r "$BRANCH" | awk '$1 == "120000" {print $4}')
[[ -z $links ]] || die "symlinks on $BRANCH, which GitHub Pages rejects: $links (undo with: $RESET)"
[[ -n $BASE ]] || return 0
git merge-base --is-ancestor "$BASE" "$BRANCH" || die "$BRANCH isn't a fast-forward of $REMOTE/$BRANCH (undo with: $RESET)"
[[ -n $1 ]] || return 0
outside=$(git diff --name-only "$BASE" "$BRANCH" | grep -v -E "$1" || true)
[[ -z $outside ]] || die "$2: $outside (undo with: $RESET)"
}
# Push the local commit and wait for GitHub Pages to publish it.
publish() {
if (( DRY_RUN )); then
say "Dry run: not pushing. To discard the local commit: $RESET"
exit 0
fi
say "Pushing to $REMOTE"
git push -q "$REMOTE" "$BRANCH"
if command -v gh >/dev/null && gh auth status >/dev/null 2>&1; then
gh api "repos/$REPO_SLUG/pages" >/dev/null 2>&1 \
|| die "pushed, but GitHub Pages isn't set up for $REPO_SLUG: in Settings → Pages, deploy from the $BRANCH branch, / (root)"
local deployed id state
deployed=$(git rev-parse "$BRANCH")
say "Waiting for GitHub Pages"
for _ in $(seq 1 30); do
id=$(gh api "repos/$REPO_SLUG/deployments?sha=$deployed&per_page=1" --jq '.[0].id // empty' 2>/dev/null || true)
state=${id:+$(gh api "repos/$REPO_SLUG/deployments/$id/statuses" --jq '.[0].state // empty' 2>/dev/null || true)}
case $state in
success) return 0 ;;
failure|error) die "GitHub Pages deploy $state: https://github.com/$REPO_SLUG/deployments" ;;
esac
sleep 10
done
die "timed out waiting for GitHub Pages: https://github.com/$REPO_SLUG/deployments"
fi
}
# Only a clean tree whose HEAD is on main gets published.
check_source() {
say "Checking the source"
[[ -z $(git status --porcelain) ]] || die "uncommitted changes: commit and merge them first"
git fetch -q "$REMOTE" main
git merge-base --is-ancestor HEAD FETCH_HEAD || die "HEAD $(git rev-parse --short HEAD) isn't on $REMOTE/main"
}
# ./deploy [minor|major|M.m] [TITLE]
deploy_version() {
local sha kind version title
kind=${1:-minor}
case $kind in
minor|major) ;;
*) is_version "$kind" || die "not a version: $kind (use minor, major, or M.m, such as 0.4)" ;;
esac
check_source
sha=$(git rev-parse --short HEAD)
./check
fetch_pages
version=$(versions_json | python3 -c '
import json, sys
kind = sys.argv[1]
if kind not in ("minor", "major"):
print(kind)
sys.exit()
deployed, latest = set(), None
for v in json.load(sys.stdin):
try:
number = tuple(int(n) for n in v["version"].split("."))
except ValueError:
continue
deployed.add(number)
if "latest" in v["aliases"]:
latest = number
step = (lambda m, n: (m, n + 1)) if kind == "minor" else (lambda m, n: (m + 1, 0))
target = step(*(latest or max(deployed, default=(0, 0))))
while target in deployed:
target = step(*target)
print("%d.%d" % target)' "$kind")
title=${2:-$version}
say "Deploying $version ($title) from $sha to $BRANCH"
(
cd wiki
mike deploy "${MIKE[@]}" --update-aliases --title "$title" \
-m "Deploy the wiki $version from $sha" "$version" latest
)
# A new version identical to what latest/ already serves would only add a duplicate to
# the version menu: stop instead, and discard the local commit. The sitemaps don't
# count: they carry the build date.
if [[ $kind == minor || $kind == major ]] && [[ -n $BASE ]] \
&& git rev-parse -q --verify "$BASE:${SITE}latest" >/dev/null \
&& git diff --quiet "$BASE:${SITE}latest" "$BRANCH:$SITE$version" -- . ':(exclude)sitemap.xml' ':(exclude)sitemap.xml.gz'; then
git branch -f "$BRANCH" "$BASE"
local current
current=$(versions_json | python3 -c 'import json, sys; print(next((v["version"] for v in json.load(sys.stdin) if "latest" in v["aliases"]), "latest"))')
[[ -z ${GITHUB_ACTIONS:-} ]] || echo "::notice title=Nothing to deploy::The site built from $sha is identical to $current, which latest already serves."
say "Nothing to deploy: the site built from $sha is identical to $current, which latest already serves"
return 0
fi
(
cd wiki
mike set-default "${MIKE[@]}" --template redirect.html \
-m "Point the wiki at $version" latest
)
check_result "^$SITE($(versions_re "$version")|latest)/|^$SITE(versions\.json|index\.html|\.nojekyll)$" \
"the deploy changed files outside $SITE$version/ and ${SITE}latest/"
publish
say "Published ${NTRE_WIKI_URL}${SITE}latest/ ($version from $sha)"
}
# ./deploy rollback VERSION: moves `latest` (a copy of a version's folder) to another
# deployed version. Nothing is built, and main isn't touched: the next deploy publishes
# main again.
rollback_to() {
[[ $# -eq 1 ]] || die "name one version to roll back to, e.g. ./deploy rollback 0.2 (./deploy list shows them)"
local target=$1 current
is_version "$target" || die "not a version: $target (use M.m, such as 0.2)"
fetch_pages
[[ -n $BASE ]] || die "nothing is deployed yet"
current=$(versions_json | python3 -c '
import json, sys
target = sys.argv[1]
versions = json.load(sys.stdin)
current = next((v["version"] for v in versions if "latest" in v["aliases"]), "")
if target not in [v["version"] for v in versions]:
sys.exit("error: " + target + " is not a deployed version (./deploy list shows them)")
if target == current:
sys.exit("error: latest already points at " + target)
print(current)' "$target")
say "Moving latest from ${current:-nothing} to $target"
(
cd wiki
mike alias "${MIKE[@]}" --update-aliases \
-m "Roll the wiki back to $target (latest was ${current:-unset})" "$target" latest
)
check_result "^${SITE}latest/|^${SITE}versions\.json$" "the rollback changed files outside ${SITE}latest/"
publish
say "Published ${NTRE_WIKI_URL}${SITE}latest/ ($target)"
}
# ./deploy list: each version with its aliases, and when and from which commit it was
# deployed (read from mike's commit messages).
list_versions() {
fetch_pages
[[ -n $BASE ]] || { echo "Nothing is deployed yet."; return 0; }
versions_json | python3 -c '
import json, re, subprocess, sys
log = subprocess.run(["git", "log", "--format=%cs %s", sys.argv[1]], capture_output=True, text=True, check=True).stdout
deployed = {}
for line in log.splitlines():
date, _, subject = line.partition(" ")
m = re.fullmatch(r"Deploy the wiki (\S+) from (\S+)", subject)
if m and m.group(1) not in deployed:
deployed[m.group(1)] = (date, m.group(2))
rows = [("VERSION", "TITLE", "ALIASES", "DEPLOYED", "FROM")]
for v in json.load(sys.stdin):
date, sha = deployed.get(v["version"], ("?", "?"))
rows.append((v["version"], v["title"], ", ".join(v["aliases"]) or "-", date, sha))
widths = [max(len(r[i]) for r in rows) for i in range(len(rows[0]))]
for r in rows:
print(" ".join(c.ljust(w) for c, w in zip(r, widths)).rstrip())' "$BRANCH"
}
# ./deploy delete VERSION...: shows what goes and what stays, asks, then deletes.
delete_versions() {
(( $# )) || die "name the versions to delete, e.g. ./deploy delete 0.1 0.2 (./deploy list shows them)"
fetch_pages
[[ -n $BASE ]] || die "nothing is deployed yet"
versions_json | python3 -c '
import json, sys
deployed = {v["version"]: v for v in json.load(sys.stdin)}
asked = sys.argv[1:]
for v in asked:
if v not in deployed:
sys.exit("error: " + v + " is not a deployed version (./deploy list shows them)")
if deployed[v]["aliases"]:
sys.exit("error: " + v + " is " + ", ".join(deployed[v]["aliases"]) + ": deploy or roll back to another version first")
print("Delete:", ", ".join(asked))
print("Keep: ", ", ".join(v + (" (" + ", ".join(d["aliases"]) + ")" if d["aliases"] else "") for v, d in deployed.items() if v not in asked))' "$@"
if (( ! DRY_RUN )); then
[[ -t 0 ]] || die "delete asks before it deletes: run it in a terminal"
local answer
read -r -p "Delete these versions from ${NTRE_WIKI_URL}? [y/N] " answer
[[ $answer == [yY] || $answer == [yY][eE][sS] ]] || die "nothing deleted"
fi
(
cd wiki
mike delete "${MIKE[@]}" -m "Delete the wiki $* from the site" "$@"
)
check_result "^$SITE($(versions_re "$@"))/|^${SITE}versions\.json$" "the delete changed files outside the deleted versions"
[[ -z $(git diff --diff-filter=AM --name-only "$BASE" "$BRANCH" | grep -v "^${SITE}versions\\.json$" || true) ]] \
|| die "the delete added or changed files instead of only removing them (undo with: $RESET)"
publish
say "Deleted $* from ${NTRE_WIKI_URL}$SITE"
}
# Commit tree $1 on top of the remote tip as the local gh-pages, unless it is already the
# tree there. Returns 1 when there is nothing to commit.
commit_pages() {
local tree=$1 message=$2 commit
[[ $tree != $(git rev-parse "$BASE^{tree}") ]] || return 1
commit=$(git commit-tree -p "$BASE" -m "$message" "$tree")
git update-ref "refs/heads/$BRANCH" "$commit" "$BASE"
}
# The last GitHub Pages build: its status, commit and creation time.
pages_build() { gh api -X GET "repos/$REPO_SLUG/pages/builds/latest" --jq '"\(.status) \(.commit) \(.created_at)"'; }
# The branch can be right while the site isn't: GitHub Pages sometimes fails to publish a
# push, and a run that then finds nothing to change pushes nothing. When the last Pages
# build isn't a good build of the branch's tip, ask Pages to build it again, and wait.
republish_if_needed() {
(( ! DRY_RUN )) || return 0
command -v gh >/dev/null && gh auth status >/dev/null 2>&1 || return 0
local head build before
head=$(git rev-parse "$BRANCH")
build=$(pages_build) || die "couldn't read the last GitHub Pages build: https://github.com/$REPO_SLUG/settings/pages"
case ${build% *} in
"built $head") return 0 ;;
"queued $head"|"building $head") say "GitHub Pages is still publishing $BRANCH: waiting" ;;
*)
say "GitHub Pages hasn't published $BRANCH (its last build: ${build% *}): asking it to build again"
before=${build##* }
gh api -X POST "repos/$REPO_SLUG/pages/builds" >/dev/null \
|| die "couldn't ask GitHub Pages to build again: the token needs pages: write"
;;
esac
for _ in $(seq 1 30); do
sleep 10
build=$(pages_build 2>/dev/null) || continue
[[ -z ${before:-} || ${build##* } != "$before" ]] || continue
case ${build% *} in
"built $head") say "GitHub Pages published $BRANCH"; return 0 ;;
errored*) die "GitHub Pages failed to build $BRANCH again: https://github.com/$REPO_SLUG/actions" ;;
esac
done
die "timed out waiting for GitHub Pages: https://github.com/$REPO_SLUG/actions"
}
# Report a run that changed nothing, in the workflow's summary too.
nothing_to_do() {
[[ -z ${GITHUB_ACTIONS:-} ]] || echo "::notice title=Nothing to do::$1"
say "Nothing to do: $1"
}
# ./deploy maintenance on|off: the gh-pages tree is rebuilt by hand, with no checkout. `on`
# also refreshes the page while it is up, after a change to wiki/maintenance.html.
maintenance() {
[[ $# -eq 1 && ( $1 == on || $1 == off ) ]] || die "use ./deploy maintenance on, or ./deploy maintenance off"
[[ $1 == off ]] || check_source
fetch_pages
[[ -n $BASE ]] || die "nothing is deployed yet"
local site page empty tree
site=$(git rev-parse "$BASE:$SITE")
if [[ $1 == off ]]; then
[[ -n $SITE ]] || { nothing_to_do "the maintenance page isn't up"; republish_if_needed; return 0; }
say "Taking down the maintenance page and bringing the wiki back from $SITE"
commit_pages "$site" "Take down the maintenance page and bring the wiki back"
check_result "" ""
[[ $(git rev-parse "$BRANCH^{tree}") == "$site" ]] \
|| die "the wiki at the root isn't what was under $SITE (undo with: $RESET)"
publish
say "Published ${NTRE_WIKI_URL}: the wiki is back"
return 0
fi
page=$(git hash-object -w wiki/maintenance.html)
empty=$(git hash-object -w /dev/null)
tree=$(printf '100644 blob %s %s
' "$empty" .nojekyll "$page" 404.html "$page" index.html \
| { cat; printf '040000 tree %s %s
' "$site" "$HIDDEN"; } | git mktree)
if ! commit_pages "$tree" "Put up the maintenance page and hide the wiki under $HIDDEN/"; then
nothing_to_do "the maintenance page is already up, and unchanged"
republish_if_needed
return 0
fi
say "Putting up the maintenance page and hiding the wiki under $HIDDEN/"
check_result "" ""
[[ $(git rev-parse "$BRANCH:$HIDDEN") == "$site" ]] \
|| die "the wiki under $HIDDEN/ isn't what was deployed (undo with: $RESET)"
publish
say "Published the maintenance page at ${NTRE_WIKI_URL}. The wiki is at ${NTRE_WIKI_URL}$HIDDEN/latest/"
}
find_remote
case ${1:-} in
list) list_versions ;;
rollback) shift; rollback_to "$@" ;;
delete) shift; delete_versions "$@" ;;
maintenance) shift; maintenance "$@" ;;
*) deploy_version "$@" ;;
esac