- npm audit fix (multer 2.5.0, body-parser, qs, proxy-addr): 0 vulnerabilities - engines node >=22.9 and .nvmrc (prod runs Node 26) - GET /healthz for deployment checks - workflow: check job (npm ci, npm test, npm audit) on every push; deploy only on main after checks, with pinned host key, BatchMode and exact commit SHA - ops/deploy-gameservers-control.sh: lock, refuse dirty tree, fast-forward to the commit, npm ci --omit=dev, wait for /healthz, roll back on failure - stop tracking storage/servers.json (prod storage now lives outside the repo) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
2.9 KiB
Bash
Executable file
94 lines
2.9 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Rôle : déploie un commit de main en production sur la VM webapps.
|
|
# Installé dans /usr/local/bin/deploy-gameservers-control (root:root, 755) et lancé par la CI
|
|
# via `sudo /usr/local/bin/deploy-gameservers-control --commit <sha>` (cf. ops/README.md).
|
|
#
|
|
# Étapes : verrou -> contrôle du dépôt -> avance rapide jusqu'au commit -> npm ci -> redémarrage
|
|
# -> attente de /healthz. Si /healthz ne répond pas, retour automatique au commit précédent.
|
|
set -euo pipefail
|
|
|
|
APP_DIR=/data/webapps/apps/gameservers-control
|
|
APP_USER=shinuwa
|
|
SERVICE=gameservers-control
|
|
NODE_BIN=/home/shinuwa/.nvm/versions/node/v26.3.0/bin
|
|
LOCK_FILE=/run/lock/deploy-gameservers-control.lock
|
|
HEALTH_TRIES=30
|
|
|
|
usage() {
|
|
echo "Usage: $0 [--commit <sha complet de 40 caractères>]" >&2
|
|
exit 2
|
|
}
|
|
|
|
COMMIT=""
|
|
if [[ $# -gt 0 ]]; then
|
|
[[ $# -eq 2 && "$1" == "--commit" && "$2" =~ ^[0-9a-f]{40}$ ]] || usage
|
|
COMMIT="$2"
|
|
fi
|
|
|
|
# Un seul déploiement à la fois : un second attend la fin du premier.
|
|
exec 9>"$LOCK_FILE"
|
|
flock 9
|
|
|
|
as_app() {
|
|
sudo -u "$APP_USER" env PATH="$NODE_BIN:/usr/bin:/bin" HOME="/home/$APP_USER" "$@"
|
|
}
|
|
|
|
cd "$APP_DIR"
|
|
|
|
# Refuse de déployer par-dessus des modifications locales de fichiers suivis par git :
|
|
# un correctif fait à la main en prod serait écrasé ou bloquerait l'avance rapide.
|
|
if [[ -n "$(as_app git status --porcelain --untracked-files=no)" ]]; then
|
|
echo "Modifications locales dans $APP_DIR, déploiement annulé :" >&2
|
|
as_app git status --short --untracked-files=no >&2
|
|
exit 1
|
|
fi
|
|
|
|
as_app git fetch --prune origin main
|
|
TARGET="${COMMIT:-$(as_app git rev-parse origin/main)}"
|
|
|
|
if ! as_app git merge-base --is-ancestor "$TARGET" origin/main; then
|
|
echo "Le commit $TARGET n'est pas sur origin/main, déploiement annulé." >&2
|
|
exit 1
|
|
fi
|
|
|
|
PREVIOUS="$(as_app git rev-parse HEAD)"
|
|
# Un commit plus ancien que celui en place (CI en retard) ne fait pas reculer la prod.
|
|
as_app git merge --ff-only "$TARGET"
|
|
DEPLOYED="$(as_app git rev-parse HEAD)"
|
|
echo "Commit en place : $DEPLOYED (précédent : $PREVIOUS)"
|
|
|
|
PORT="$(sed -n 's/^PORT=//p' .env 2>/dev/null | tail -n 1)"
|
|
HEALTH_URL="http://127.0.0.1:${PORT:-3100}/healthz"
|
|
|
|
install_and_restart() {
|
|
as_app npm ci --omit=dev --no-audit --no-fund
|
|
systemctl restart "$SERVICE"
|
|
}
|
|
|
|
wait_healthy() {
|
|
local i
|
|
for ((i = 1; i <= HEALTH_TRIES; i++)); do
|
|
if curl -fsS --max-time 2 "$HEALTH_URL" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
sleep 1
|
|
done
|
|
return 1
|
|
}
|
|
|
|
install_and_restart
|
|
if wait_healthy; then
|
|
echo "Déploiement OK : $DEPLOYED répond sur $HEALTH_URL"
|
|
exit 0
|
|
fi
|
|
|
|
echo "$HEALTH_URL ne répond pas après ${HEALTH_TRIES}s, retour à $PREVIOUS." >&2
|
|
journalctl -u "$SERVICE" -n 30 --no-pager >&2 || true
|
|
as_app git reset --hard "$PREVIOUS"
|
|
install_and_restart
|
|
if wait_healthy; then
|
|
echo "Retour arrière OK : $PREVIOUS est de nouveau en place." >&2
|
|
else
|
|
echo "Le retour arrière ne répond pas non plus : intervention manuelle nécessaire." >&2
|
|
fi
|
|
exit 1
|