-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdeploy_web_pages.sh
More file actions
executable file
·76 lines (64 loc) · 2.7 KB
/
Copy pathdeploy_web_pages.sh
File metadata and controls
executable file
·76 lines (64 loc) · 2.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
#!/bin/bash
# Build the MicroPythonOS web export and publish it to a GitHub Pages branch.
#
# This builds web/ (unless --no-build) and force-pushes its contents as a single
# commit to the `gh-pages` branch of the chosen remote. GitHub Pages then serves
# it at https://<owner>.github.io/<repo>/.
#
# One-time setup (in the repo's GitHub settings):
# Settings → Pages → Build and deployment → Source: "Deploy from a branch",
# Branch: gh-pages / (root).
#
# Usage:
# scripts/deploy_web_pages.sh # build, then deploy to `origin` remote's `gh-pages` branch
# scripts/deploy_web_pages.sh --no-build # deploy the existing web/ as-is
# REMOTE=other-origin scripts/deploy_web_pages.sh # deploy to a different remote
# BRANCH=gh-pages scripts/deploy_web_pages.sh # use a different Pages branch
set -euo pipefail
mydir=$(cd "$(dirname "$0")" && pwd -P)
codebasedir=$(cd "$mydir/.." && pwd -P)
remote="${REMOTE:-origin}"
branch="${BRANCH:-gh-pages}"
webdir="$codebasedir/web"
# 1. Build unless told otherwise.
if [ "${1:-}" != "--no-build" ]; then
"$mydir/build_mpos_web.sh"
fi
if [ ! -f "$webdir/index.html" ]; then
echo "ERROR: $webdir/index.html not found. Build first (omit --no-build)."
exit 1
fi
# 2. Validate the remote exists.
if ! git -C "$codebasedir" remote get-url "$remote" >/dev/null 2>&1; then
echo "ERROR: git remote '$remote' not found. Add it or set REMOTE=<name>."
echo "Available remotes:"
git -C "$codebasedir" remote -v
exit 1
fi
remote_url=$(git -C "$codebasedir" remote get-url "$remote")
# 3. Stage the built site in a temporary worktree and publish as a single
# squashed commit (history is not needed for a generated artifact).
tmpdir=$(mktemp -d)
cleanup() { rm -rf "$tmpdir"; }
trap cleanup EXIT
cp -R "$webdir"/. "$tmpdir"/
# Jekyll would otherwise ignore files/dirs starting with "_" (e.g. the
# preloaded filesystem). Disable Jekyll processing on Pages.
touch "$tmpdir/.nojekyll"
short_sha=$(git -C "$codebasedir" rev-parse --short HEAD 2>/dev/null || echo "unknown")
git -C "$tmpdir" init -q
git -C "$tmpdir" checkout -q -b "$branch"
git -C "$tmpdir" add -A
git -C "$tmpdir" -c user.name="web-deploy" -c user.email="web-deploy@local" \
commit -q -m "Deploy web export ($short_sha)"
echo "Force-pushing web export to $remote/$branch ($remote_url)..."
git -C "$tmpdir" push -f "$remote_url" "$branch":"$branch"
# 4. Print the resulting Pages URL when the remote is a GitHub repo.
if [[ "$remote_url" =~ github.com[:/]+([^/]+)/([^/.]+) ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
echo
echo "Pushed. Once Pages is enabled (Settings → Pages → branch: $branch / root),"
echo "the site will be live at:"
echo " https://${owner}.github.io/${repo}/"
fi