ci: run tests and audit on every push, deploy exact commit with health check
All checks were successful
Deploy Gameservers Control / check (push) Successful in 6s
Deploy Gameservers Control / deploy (push) Has been skipped

- 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>
This commit is contained in:
Shinuwa 2026-12-02 12:24:15 +01:00
parent 655cf655da
commit 0a7bfc183d
10 changed files with 249 additions and 129 deletions

41
ops/README.md Normal file
View file

@ -0,0 +1,41 @@
# Exploitation
## Déploiement
La CI se connecte en SSH à la VM webapps (`shinuwa@192.168.0.100:2911`) avec la clé
`forgejo-runner@webapps`, puis lance :
```
sudo /usr/local/bin/deploy-gameservers-control --commit '<sha>'
```
Côté serveur, cette clé est limitée par `/usr/local/sbin/deploy-ssh-gate` (commande forcée dans
`~/.ssh/authorized_keys`) : toute nouvelle commande SSH dans le workflow doit y être ajoutée.
### Secrets Forgejo du dépôt
| Secret | Contenu |
|---|---|
| `DEPLOY_SSH_KEY` | Clé privée `forgejo-runner@webapps` |
| `DEPLOY_KNOWN_HOSTS` | Sortie de `ssh-keyscan -p 2911 192.168.0.100` |
### Installer ou mettre à jour le script de déploiement
Sur la VM webapps, depuis le dépôt à jour :
```bash
sudo install -o root -g root -m 755 ops/deploy-gameservers-control.sh /usr/local/bin/deploy-gameservers-control
```
Le script :
1. prend un verrou, pour qu'un seul déploiement tourne à la fois ;
2. refuse de déployer si des fichiers suivis par git ont été modifiés à la main en prod ;
3. avance `main` jusqu'au commit demandé (avance rapide seulement, jamais de recul) ;
4. lance `npm ci --omit=dev`, redémarre le service, puis attend `/healthz` pendant 30 s ;
5. si `/healthz` ne répond pas, revient au commit précédent et termine en erreur.
## Données
`STORAGE_DIR` pointe hors du dépôt (`/data/webapps/data/gameservers-control`, droits `700`,
fichiers `600`).

View file

@ -0,0 +1,94 @@
#!/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