50 lines
1.6 KiB
Bash
50 lines
1.6 KiB
Bash
#!/usr/bin/env bash
|
|
# Runs ON the server (piped in by deploy.sh): install release $1 (commit $2), optional DB reset ($3 = 1).
|
|
set -euo pipefail
|
|
REL="$1"; SHA="$2"; RESET="${3:-0}"
|
|
APP=/opt/robotclash
|
|
SVC=robotclash
|
|
D="$APP/releases/$REL"
|
|
PORT=$(grep -E '^PORT=' "$APP/shared/.env" | cut -d= -f2); PORT=${PORT:-8080}
|
|
|
|
mkdir -p "$D"
|
|
tar -xzf "/tmp/rc-$REL.tgz" -C "$D"
|
|
rm -f "/tmp/rc-$REL.tgz"
|
|
echo "GIT_COMMIT=$SHA" > "$D/server/.release"
|
|
echo ">> npm ci"
|
|
(cd "$D/server" && npm ci --omit=dev --no-audit --no-fund --loglevel=error)
|
|
chown -R "$SVC:$SVC" "$D"
|
|
|
|
PREV=$(readlink -f "$APP/current" 2>/dev/null || true)
|
|
if [ -f "$APP/shared/data.sqlite" ]; then
|
|
sqlite3 "$APP/shared/data.sqlite" ".backup '$APP/backups/pre-$REL.sqlite'"
|
|
echo ">> db backup: backups/pre-$REL.sqlite"
|
|
fi
|
|
|
|
systemctl stop "$SVC" || true
|
|
if [ "$RESET" = "1" ]; then
|
|
echo ">> db reset (seed accounts)"
|
|
sudo -u "$SVC" bash -c "set -a; . '$APP/shared/.env'; set +a; cd '$D/server' && node scripts/reset-db.js"
|
|
fi
|
|
ln -sfn "$D" "$APP/current"
|
|
systemctl start "$SVC"
|
|
|
|
ok=0
|
|
for i in $(seq 1 20); do
|
|
if curl -fsS "http://127.0.0.1:$PORT/healthz" 2>/dev/null | grep -q "\"commit\":\"$SHA\""; then ok=1; break; fi
|
|
sleep 0.5
|
|
done
|
|
if [ "$ok" != "1" ]; then
|
|
echo "!! health check failed for $REL"
|
|
journalctl -u "$SVC" -n 20 --no-pager || true
|
|
if [ -n "$PREV" ] && [ -d "$PREV" ]; then
|
|
echo "!! rolling back to $(basename "$PREV")"
|
|
ln -sfn "$PREV" "$APP/current"; systemctl restart "$SVC"
|
|
fi
|
|
exit 1
|
|
fi
|
|
echo ">> live: $(curl -fsS "http://127.0.0.1:$PORT/healthz")"
|
|
|
|
# keep the last 5 releases
|
|
ls -1dt "$APP"/releases/* 2>/dev/null | tail -n +6 | xargs -r rm -rf
|