sokko-g/scripts/deploy-sokko-g.sh
Shinuwa 067a4a8fd6
Some checks failed
Validate and deploy Sokko G / validate (push) Failing after 19m40s
Validate and deploy Sokko G / deploy (push) Has been skipped
Fix the audit's concurrency, stale state and delivery findings
Share uploads now reserve their slot, bytes and per-IP count before the
disk check, so simultaneous uploads can no longer exceed the limits.
Writes to a share's metadata run one after another, and a revocation can
no longer be undone by a pending read count.

The TCG page ignores a state reload that started before or during a
mutation, the last mutation wins, and returning to the page triggers a
single reload. The card cache confirms writes only once the IndexedDB
transaction completes and shows only the current player's copy. The
administration panel loads on demand, which shrinks the TCG chunk from
311.6 to 191.8 kB.

A new /api/health route answers 503 when the cards or shares API failed
to start. The deploy script ships the commit's production dependencies
with the build, waits up to 30 s for a healthy server instead of trusting
systemctl is-active, and restores node_modules on rollback. Both Forgejo
workflows check the route on the public site. The updated script applies
once reinstalled on the server.

Unused helpers, the ignored join role and duplicate button selectors are
removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 21:38:20 +01:00

187 lines
6.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# Rôle : construit et active en production le commit Sokko G explicitement demandé.
set -Eeuo pipefail
readonly APP_ROOT="/data/webapps/sites/sokko-g"
readonly APP_USER="shinuwa"
readonly NODE_HOME="/home/shinuwa/.nvm/versions/node/v24.11.1"
readonly NODE_BIN="$NODE_HOME/bin/node"
readonly NPM_BIN="$NODE_HOME/bin/npm"
readonly SERVICE_NAME="sokko-g"
readonly LOCK_FILE="/run/lock/sokko-g-deploy.lock"
readonly STATE_DIR="/var/lib/sokko-g"
readonly SERVICE_ENV_FILE="/etc/sokko-g/cards.env"
# Délai laissé au service pour répondre sur /api/health après son redémarrage.
readonly HEALTH_TIMEOUT_SECONDS=30
build_root=""
worktree=""
backup_dist=""
backup_node_modules=""
usage() {
echo "Usage: deploy-sokko-g --commit <full-sha>" >&2
exit 2
}
cleanup() {
local status=$?
trap - EXIT
if [[ -n "$worktree" && -d "$worktree" ]]; then
sudo -u "$APP_USER" git -C "$APP_ROOT" worktree remove --force "$worktree" >/dev/null 2>&1 || true
fi
if [[ -n "$build_root" && -d "$build_root" ]]; then
rm -rf -- "$build_root"
fi
exit "$status"
}
trap cleanup EXIT
as_app_user() {
sudo -u "$APP_USER" "$@"
}
restore_previous_release() {
local previous_commit=$1
as_app_user rm -rf -- "$APP_ROOT/website/dist"
if [[ -n "$backup_dist" && -d "$backup_dist" ]]; then
as_app_user mv "$backup_dist" "$APP_ROOT/website/dist"
fi
# Sans copie précédente (première livraison de ce type), les nouvelles dépendances restent : l'ancien code n'a rien d'autre.
if [[ -n "$backup_node_modules" && -d "$backup_node_modules" ]]; then
as_app_user rm -rf -- "$APP_ROOT/node_modules"
as_app_user mv "$backup_node_modules" "$APP_ROOT/node_modules"
fi
as_app_user git -C "$APP_ROOT" checkout --detach "$previous_commit"
systemctl restart "$SERVICE_NAME" || true
}
# Valeur d'une variable telle que le service la voit : l'environnement systemd l'emporte sur le .env du site.
service_env_value() {
local name=$1
local file value=""
for file in "$APP_ROOT/.env" "$SERVICE_ENV_FILE"; do
[[ -r "$file" ]] || continue
value="$(sed -n "s/^[[:space:]]*$name[[:space:]]*=[[:space:]]*//p" "$file" | tail -n 1 | sed -e 's/^["'"'"']//' -e 's/["'"'"'][[:space:]]*$//' -e 's/[[:space:]]*$//')" || true
done
printf '%s' "$value"
}
# `systemctl is-active` répond dès le lancement du processus : seul /api/health prouve que le serveur a chargé son code
# et ses dépendances, et que chaque API demandée a démarré.
wait_until_healthy() {
local host port url deadline body
host="$(service_env_value HOST)"
port="$(service_env_value PORT)"
[[ -z "$host" || "$host" == "0.0.0.0" || "$host" == "::" ]] && host="127.0.0.1"
url="http://$host:${port:-5173}/api/health"
deadline=$((SECONDS + HEALTH_TIMEOUT_SECONDS))
while (( SECONDS < deadline )); do
# Le contenu compte : sans cette route, le serveur répondrait par la page de l'application.
if body="$(curl --fail --silent --max-time 3 "$url" 2>/dev/null)" && [[ $body == *'"ok":true'* ]]; then
systemctl is-active --quiet "$SERVICE_NAME"
return
fi
sleep 1
done
echo "$url did not answer successfully within ${HEALTH_TIMEOUT_SECONDS}s." >&2
curl --silent --max-time 3 "$url" >&2 || true
return 1
}
write_state() {
local name=$1
local value=$2
local temporary
temporary="$(mktemp "$STATE_DIR/.${name}.XXXXXX")"
printf '%s\n' "$value" > "$temporary"
chmod 0644 "$temporary"
mv "$temporary" "$STATE_DIR/$name"
}
[[ $EUID -eq 0 ]] || {
echo "deploy-sokko-g must run as root." >&2
exit 1
}
[[ $# -eq 2 && $1 == "--commit" ]] || usage
readonly requested_commit=$2
[[ $requested_commit =~ ^[0-9a-f]{40}$ ]] || usage
command -v flock >/dev/null
command -v curl >/dev/null
[[ -x "$NODE_BIN" && -x "$NPM_BIN" ]] || {
echo "Node 24.11.1 and npm 11.17.0 must be installed under $NODE_HOME." >&2
exit 1
}
# Ces fichiers appartiennent au compte du service : les lancer en root lui donnerait les droits root.
[[ "$(as_app_user "$NODE_BIN" --version)" == "v24.11.1" ]]
[[ "$(as_app_user env PATH="$NODE_HOME/bin:/usr/bin:/bin" "$NPM_BIN" --version)" == "11.17.0" ]]
systemctl show --property=ExecStart --value "$SERVICE_NAME" | grep -Fq "$NPM_BIN"
exec 9>"$LOCK_FILE"
flock --wait 900 9 || {
echo "Another Sokko G deployment still holds $LOCK_FILE." >&2
exit 1
}
as_app_user git -C "$APP_ROOT" fetch --prune origin \
+refs/heads/main:refs/remotes/origin/main
as_app_user git -C "$APP_ROOT" cat-file -e "$requested_commit^{commit}"
as_app_user git -C "$APP_ROOT" merge-base --is-ancestor \
"$requested_commit" refs/remotes/origin/main
if [[ -n "$(as_app_user git -C "$APP_ROOT" status --porcelain --untracked-files=no)" ]]; then
echo "The production checkout contains tracked changes; deployment aborted." >&2
exit 1
fi
readonly previous_commit="$(as_app_user git -C "$APP_ROOT" rev-parse HEAD)"
build_root="$(mktemp -d "$(dirname "$APP_ROOT")/.sokko-g-build.XXXXXX")"
chown "$APP_USER:$APP_USER" "$build_root"
worktree="$build_root/source"
as_app_user git -C "$APP_ROOT" worktree add --detach "$worktree" "$requested_commit"
as_app_user env \
PATH="$NODE_HOME/bin:/usr/bin:/bin" \
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 \
"$NPM_BIN" --prefix "$worktree" ci
as_app_user env \
PATH="$NODE_HOME/bin:/usr/bin:/bin" \
"$NPM_BIN" --prefix "$worktree" run build
[[ -f "$worktree/website/dist/index.html" ]]
# Le service utilisera exactement les dépendances installées par npm ci pour ce commit, sans les outils de développement.
as_app_user env \
PATH="$NODE_HOME/bin:/usr/bin:/bin" \
"$NPM_BIN" --prefix "$worktree" prune --omit=dev
[[ -d "$worktree/node_modules" ]]
# Le worktree est sur le même système de fichiers que le site : chaque remplacement est un simple renommage.
as_app_user git -C "$APP_ROOT" checkout --detach "$requested_commit"
backup_dist="$build_root/previous-dist"
if [[ -d "$APP_ROOT/website/dist" ]]; then
as_app_user mv "$APP_ROOT/website/dist" "$backup_dist"
fi
as_app_user mv "$worktree/website/dist" "$APP_ROOT/website/dist"
backup_node_modules="$build_root/previous-node_modules"
if [[ -d "$APP_ROOT/node_modules" ]]; then
as_app_user mv "$APP_ROOT/node_modules" "$backup_node_modules"
fi
as_app_user mv "$worktree/node_modules" "$APP_ROOT/node_modules"
if ! systemctl restart "$SERVICE_NAME" || ! wait_until_healthy; then
echo "The new release did not start; restoring $previous_commit." >&2
restore_previous_release "$previous_commit"
exit 1
fi
install -d -o root -g root -m 0755 "$STATE_DIR"
write_state previous-commit "$previous_commit"
write_state current-commit "$requested_commit"
printf 'Sokko G deployed at %s (previous: %s).\n' "$requested_commit" "$previous_commit"