Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·132 lines (113 loc) · 5.46 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·132 lines (113 loc) · 5.46 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
#!/usr/bin/env bash
#
# Build GamesLab and ship it to the server.
#
# ./deploy/deploy.sh # backend and frontend
# ./deploy/deploy.sh backend # the Go binary, the specs, restart
# ./deploy/deploy.sh frontend # the built site only
#
# Settings come from deploy/deploy.env — copy deploy.env.example and fill in the
# host. Everything is overridable from the environment for one-off targets:
#
# SSH_HOST=pi@10.0.0.9 ./deploy/deploy.sh backend
#
# Checks run before anything is uploaded. SKIP_CHECKS=1 to skip them, which is
# for when you already ran them in this tree and not for when they are failing.
set -euo pipefail
repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$repo"
# shellcheck disable=SC1091
[ -f deploy/deploy.env ] && . deploy/deploy.env
SSH_HOST="${SSH_HOST:-}"
# Empty by default so a host that is an alias in ~/.ssh/config keeps whatever
# port, user and key that entry sets. Passing -p 22 unconditionally would
# override a non-standard port and turn `ssh server2` into a connection refused.
SSH_PORT="${SSH_PORT:-}"
# The Orange Pi 5 Plus is an RK3588 — arm64. The binary is static and pure Go,
# so there is nothing to install on the other end.
GOOS_TARGET="${GOOS_TARGET:-linux}"
GOARCH_TARGET="${GOARCH_TARGET:-arm64}"
BASE="${BASE:-/var/www/production/henhen1227}"
FRONTEND_DIR="${FRONTEND_DIR:-$BASE/gameslab.henhen1227.com}"
BACKEND_DIR="${BACKEND_DIR:-$BASE/gameslab-backend}"
SOCKET="${SOCKET:-$BASE/gameslab-henhen1227-backend.sock}"
SERVICE="${SERVICE:-gameslab}"
# Set SUDO= (empty) if you deploy as root. Whatever this runs must not prompt for
# a password: the ssh session is not interactive and a prompt hangs the deploy.
# The usual fix is one sudoers line —
# www-data ALL=(root) NOPASSWD: /bin/systemctl restart gameslab
SUDO="${SUDO-sudo}"
target="${1:-all}"
say() { printf '\n\033[1m== %s\033[0m\n' "$*"; }
die() { printf '\033[31merror:\033[0m %s\n' "$*" >&2; exit 1; }
[ -n "$SSH_HOST" ] || die "SSH_HOST is not set. Copy deploy/deploy.env.example to deploy/deploy.env and fill it in."
command -v rsync >/dev/null || die "rsync is not installed locally"
# A string rather than an array: macOS ships bash 3.2, where expanding an empty
# array under `set -u` is an unbound-variable error rather than nothing. Left
# unquoted at the call sites on purpose, so an empty value contributes no
# argument at all — safe here because a port has no spaces in it.
SSH_OPTS=""
[ -n "$SSH_PORT" ] && SSH_OPTS="-p $SSH_PORT"
# shellcheck disable=SC2086
ssh_run() { ssh $SSH_OPTS "$SSH_HOST" "$@"; }
push() { rsync -az --delete-after -e "ssh $SSH_OPTS" "$@"; }
if [ "${SKIP_CHECKS:-0}" != "1" ]; then
say "Checks"
npm run check
else
echo "skipping checks (SKIP_CHECKS=1)"
fi
# --------------------------------------------------------------------- backend
if [ "$target" = "all" ] || [ "$target" = "backend" ]; then
say "Building gameslab for $GOOS_TARGET/$GOARCH_TARGET"
out="$(mktemp -d)/gameslab"
( cd backend && CGO_ENABLED=0 GOOS="$GOOS_TARGET" GOARCH="$GOARCH_TARGET" \
go build -trimpath -ldflags="-s -w" -o "$out" ./cmd/gameslab )
file "$out" 2>/dev/null | sed 's/^/ /' || true
printf ' %s\n' "$(du -h "$out" | cut -f1) binary"
say "Uploading to $SSH_HOST:$BACKEND_DIR"
ssh_run "mkdir -p '$BACKEND_DIR/data'"
# Uploaded beside the running binary and renamed into place, because you
# cannot write to a busy executable on Linux — the kernel answers ETXTBSY —
# and because rename(2) is atomic, so there is no instant where the path
# holds half a binary.
push "$out" "$SSH_HOST:$BACKEND_DIR/gameslab.incoming"
push --exclude='*.txt~' specs/ "$SSH_HOST:$BACKEND_DIR/specs/"
say "Restarting $SERVICE"
ssh_run "set -e
chmod +x '$BACKEND_DIR/gameslab.incoming'
mv -f '$BACKEND_DIR/gameslab.incoming' '$BACKEND_DIR/gameslab'
$SUDO systemctl restart '$SERVICE'"
say "Health"
# Asked over the socket rather than over the domain, so this answers about
# the process that was just restarted and not about nginx's cache, DNS, or a
# certificate. Retried because the service seeds the library on start.
ssh_run "SERVICE_NAME='$SERVICE'
for i in 1 2 3 4 5 6 7 8 9 10; do
if out=\$(curl -sf --unix-socket '$SOCKET' http://localhost/api/health 2>/dev/null); then
echo \"\$out\"; exit 0
fi
sleep 1
done
echo 'no answer from the socket after 10s' >&2
$SUDO systemctl --no-pager --lines=20 status '\$SERVICE_NAME' >&2 || true
exit 1"
fi
# -------------------------------------------------------------------- frontend
if [ "$target" = "all" ] || [ "$target" = "frontend" ]; then
say "Building the site"
# No VITE_GAMESLAB_SERVER: nginx puts /api and /ws on the same origin as the
# page, which is what the frontend assumes when nothing is set. Setting it
# here would make every request cross-origin and need CORS.
npm run build
say "Uploading to $SSH_HOST:$FRONTEND_DIR"
ssh_run "mkdir -p '$FRONTEND_DIR'"
# Trailing slashes matter: contents into the directory, not the directory
# into the directory. --delete-after so the old hashed assets stay reachable
# until the new index.html is in place.
# .well-known is excluded from the delete: certbot writes its ACME challenge
# there, and a renewal that lands mid-deploy would otherwise be swept away.
push --exclude='.well-known' frontend/dist/ "$SSH_HOST:$FRONTEND_DIR/"
fi
say "Done"
echo " https://gameslab.henhen1227.com"