sokko-g/scripts/deploy-sokko-g.sh
Shinuwa 7046515d20
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 20m8s
Validate and deploy Sokko G / deploy (push) Successful in 10s
Close the security gaps found by the application audit
Imported toolboxes could run scripts on the site origin through image data
URLs, so only bitmap images are now accepted at import, display and opening.
The Cards game and its fake admin login no longer turn on when NODE_ENV is
missing, and the Node server now sends a CSP and the other security headers.

The deployment script no longer runs files owned by the service account as
root, the services cannot gain privileges through sudo, and the deployment
key stays in a single workflow step with actions pinned by commit. The script
and systemd units must still be reinstalled on the server after delivery.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 10:55:35 +01:00

131 lines
4 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"
build_root=""
worktree=""
backup_dist=""
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
as_app_user git -C "$APP_ROOT" checkout --detach "$previous_commit"
systemctl restart "$SERVICE_NAME" || true
}
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
[[ -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" ]]
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"
if ! systemctl restart "$SERVICE_NAME" || ! systemctl is-active --quiet "$SERVICE_NAME"; 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"