game-village-politics-frontend/deploy.sh

68 lines
2.3 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# Runs on the server: pulls, builds the static SPA in a container, restarts nginx.
set -e
SELF="$0"
COMPOSE_CMD=(docker compose -f docker-compose.yml)
echo "==> Starting deploy script..."
# ── Self-update check (first run only) ───────────────────────────────
if [ "${1:-}" != "--after-pull" ]; then
echo "==> Checking for updates..."
ORIGINAL_HASH=$(md5sum "$SELF" | cut -d' ' -f1)
git pull --rebase --stat
UPDATED_HASH=$(md5sum "$SELF" | cut -d' ' -f1)
if [ "$ORIGINAL_HASH" != "$UPDATED_HASH" ]; then
echo "==> Script updated — re-running..."
fi
"$SELF" --after-pull
exit $?
fi
echo "==> Running deployment steps..."
# ── Build + deploy ───────────────────────────────────────────────────
REPO_UID=$(stat -c '%u' . 2>/dev/null || id -u)
REPO_GID=$(stat -c '%g' . 2>/dev/null || id -g)
DEPLOY_UID=${DEPLOY_UID:-$REPO_UID}
DEPLOY_GID=${DEPLOY_GID:-$REPO_GID}
if [ ! -f .env ]; then
echo "==> .env missing — creating it from .env.example"
cp .env.example .env
fi
mkdir -p docker-data/node_modules
if [ "$(id -u)" -eq 0 ]; then
chown -R "${DEPLOY_UID}:${DEPLOY_GID}" docker-data .output 2>/dev/null || true
fi
# Remove stale build output so we never serve an old build.
rm -rf .output
# Build/rebuild the builder image (only rebuilds when Dockerfile.build changes).
"${COMPOSE_CMD[@]}" --profile build build
# Install deps + generate, writing .output to the host.
# -T disables pseudo-TTY to prevent interactive prompts during deploy.
"${COMPOSE_CMD[@]}" --profile build run --rm -T -u "${DEPLOY_UID}:${DEPLOY_GID}" build
if [ ! -f .output/public/index.html ] || [ ! -f .output/public/200.html ]; then
echo "ERROR: Build did not produce .output/public/index.html and 200.html — aborting deploy"
exit 1
fi
if find .output -type f -name '*.map' -print -quit | grep -q .; then
echo "ERROR: Sourcemap artifacts still present in .output — aborting deploy"
exit 1
fi
# Recreate so nginx binds the fresh .output/public (the old directory was deleted).
"${COMPOSE_CMD[@]}" up -d --force-recreate frontend
echo "==> Deployment complete!"