#!/usr/bin/env bash # Build locally, push the static site and the game server to the droplet, # and restart the server. Usage: deploy/deploy.sh set -euo pipefail HOST="${1:?usage: deploy.sh }" # The family list is the kit's; a game deploys with the kit's latest copy when the kit is beside it. KIT_FAMILY="$(dirname "$0")/../../game-kit/family.json" [ -f "$KIT_FAMILY" ] && cp "$KIT_FAMILY" static/family.json npm run check npm run check:server npm test npm run build # Every production ledger must replay identically with the engine about to ship. deploy/verify-ledgers.sh "$HOST" # Old hashed chunks stay a week: a page loaded before this deploy can still # fetch the module it was built against instead of failing mid-game. rsync -az --delete --filter='P _app/immutable/*' \ build/ "root@$HOST:/opt/hnefatafl/build/" # The server runs from source with tsx and carries its own small manifest; # it needs only its code and the engine. Ledgers live outside this tree. rsync -az --delete --exclude='/server/node_modules' \ --include='/server/***' --include='/src/' --include='/src/lib/' --include='/src/lib/game/***' --include='/src/lib/net/' --include='/src/lib/net/view.ts' \ --include='/deploy/' --include='/deploy/hnefatafl.service' --include='/deploy/hnefatafl.cron' --include='/deploy/*.sh' --include='/deploy/hash-tokens.ts' \ --exclude='*' \ ./ "root@$HOST:/opt/hnefatafl/app/" ssh "root@$HOST" ' find /opt/hnefatafl/build/_app/immutable -type f -mtime +7 -delete chown -R root:caddy /opt/hnefatafl/build chmod -R g+rX /opt/hnefatafl/build cd /opt/hnefatafl/app/server && npm install --no-audit --no-fund chown -R hnefatafl:hnefatafl /opt/hnefatafl/app cp /opt/hnefatafl/app/deploy/hnefatafl.service /etc/systemd/system/hnefatafl.service # Cron runs the rollup and the backup from /usr/local/bin: install them on # every deploy so the live copies are always the repo copies. install -m 755 /opt/hnefatafl/app/deploy/visitors.sh /usr/local/bin/hnefatafl-visitors.sh install -m 755 /opt/hnefatafl/app/deploy/pulse.sh /usr/local/bin/hnefatafl-pulse.sh install -m 755 /opt/hnefatafl/app/deploy/hnefatafl-rollup.sh /usr/local/bin/hnefatafl-rollup.sh install -m 755 /opt/hnefatafl/app/deploy/hnefatafl-backup.sh /usr/local/bin/hnefatafl-backup.sh install -m 644 /opt/hnefatafl/app/deploy/hnefatafl.cron /etc/cron.d/hnefatafl mkdir -p /var/log/hnefatafl systemctl daemon-reload systemctl enable --now hnefatafl systemctl restart hnefatafl systemctl reload caddy sleep 1 systemctl --no-pager -l status hnefatafl | head -3 ' echo "deployed."