diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c6c1d23 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +# The build image copies nothing; the source is bind-mounted at run time. +* diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..cc1a98f --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +# Read at build time: `pnpm generate` bakes runtime config into the static files. +NUXT_PUBLIC_SERVER_URL=https://server-village.blax.at +NUXT_PUBLIC_WEBS_URL=ws-server-village.blax.at +NUXT_PUBLIC_WS_PROTOCOL=wss +NUXT_PUBLIC_PUSHER_APP_KEY=websocket +# true until the backend serves the WS API (see CLAUDE.md, "Mock server"). +NUXT_PUBLIC_MOCK_SERVER=true diff --git a/.gitignore b/.gitignore index 1af2f45..b318879 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,9 @@ i18n/.messages .env.* !.env.example +# Docker volumes +docker-data/ + # IDE .vscode .idea diff --git a/Dockerfile.build b/Dockerfile.build new file mode 100644 index 0000000..c1537f8 --- /dev/null +++ b/Dockerfile.build @@ -0,0 +1,14 @@ +# Reusable build environment. The source is bind-mounted, not copied. +FROM node:26-slim + +WORKDIR /app +ENV PNPM_HOME="/pnpm" +ENV PATH="$PNPM_HOME:$PATH" +ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0 +# Lets pnpm purge a stale bind-mounted node_modules without a TTY prompt. +ENV CI=true + +RUN npm install -g --force corepack && corepack enable + +# Static SPA build (ssr: false): writes .output/public, the same files a Steam wrapper ships. +CMD ["sh", "-c", "pnpm install --frozen-lockfile && pnpm run generate && find .output -type f -name '*.map' -delete && find .output -type f \\( -name '*.js' -o -name '*.mjs' -o -name '*.css' \\) -exec sed -i -e '/^[[:space:]]*\\/\\/# sourceMappingURL=.*$/d' -e '/^[[:space:]]*\\/\\*# sourceMappingURL=.*\\*\\/[[:space:]]*$/d' {} +"] diff --git a/deploy.sh b/deploy.sh new file mode 100755 index 0000000..611dfad --- /dev/null +++ b/deploy.sh @@ -0,0 +1,67 @@ +#!/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!" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2f95c0d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,41 @@ +services: + # One-shot build container. Bind-mounts the repo, runs pnpm install + generate, + # writes .output to the host, then exits. + # Usage: docker compose --profile build run --rm build + build: + image: "village-politics-frontend-build:latest" + build: + context: . + dockerfile: Dockerfile.build + volumes: + - .:/app + - ./docker-data/node_modules:/app/node_modules + env_file: + - .env + profiles: + - build + + # Long-running static server for the generated SPA in .output/public. + frontend: + image: "nginx:stable-alpine" + container_name: village-politics-frontend + restart: unless-stopped + volumes: + - ./.output/public:/usr/share/nginx/html:ro + - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf:ro + labels: + traefik.enable: "true" + traefik.docker.network: "web" + # nginx on the host terminates TLS with the *.blax.at wildcard cert and + # forwards plain HTTP to traefik's `web` entrypoint. This router beats + # blax-nuxt-website's *.blax.at fallback router (priority -100). + traefik.http.routers.village-frontend.rule: "Host(`village.blax.at`) || Host(`village.localhost.at`)" + traefik.http.routers.village-frontend.entrypoints: "web" + traefik.http.routers.village-frontend.service: "village-frontend-http" + traefik.http.services.village-frontend-http.loadbalancer.server.port: "80" + +networks: + default: + name: web + external: true + driver: bridge diff --git a/docker/nginx.conf b/docker/nginx.conf new file mode 100644 index 0000000..24b5a9b --- /dev/null +++ b/docker/nginx.conf @@ -0,0 +1,16 @@ +server { + listen 80; + root /usr/share/nginx/html; + + # Hashed build assets never change. + location /_nuxt/ { + add_header Cache-Control "public, max-age=31536000, immutable"; + try_files $uri =404; + } + + # Prerendered pages, else the SPA shell for client-side routes like /village/12. + location / { + add_header Cache-Control "no-cache"; + try_files $uri $uri/index.html /200.html; + } +} diff --git a/scripts/deploy-edgerealm.sh b/scripts/deploy-edgerealm.sh new file mode 100755 index 0000000..a1812c3 --- /dev/null +++ b/scripts/deploy-edgerealm.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# Deploys the pushed master to village.blax.at (edgerealm) and checks it is live. +# Touches only the game-village-politics-frontend checkout and container on that shared host. +# On Windows, put the Windows OpenSSH client first: PATH="/c/Windows/System32/OpenSSH:$PATH". +set -euo pipefail + +HOST=root@static.64.40.140.128.clients.your-server.de +DIR=/mnt/HC_Volume_102040804/srv/game-village-politics-frontend +REPO=ssh://git@git.blax.at:222/blax-software/game-village-politics-frontend.git +URL=https://village.blax.at + +cd "$(dirname "$0")/.." +if [ -n "$(git status --porcelain)" ]; then + echo "Uncommitted changes: commit and push first." >&2 + exit 1 +fi +git fetch -q origin +LOCAL=$(git rev-parse HEAD) +REMOTE=$(git rev-parse origin/master) +if [ "$LOCAL" != "$REMOTE" ]; then + echo "HEAD ${LOCAL:0:7} is not origin/master ${REMOTE:0:7}: push (or pull) first." >&2 + exit 1 +fi + +echo "==> Deploying ${LOCAL:0:7} to edgerealm" +ssh -o BatchMode=yes "$HOST" "[ -d $DIR ] || git clone -q $REPO $DIR" +ssh -o BatchMode=yes "$HOST" "cd $DIR && ./deploy.sh" | { grep -E '==> Deployment complete|ERROR|Container village-politics-frontend Started' || true; } + +DEPLOYED=$(ssh -o BatchMode=yes "$HOST" "cd $DIR && git rev-parse HEAD") +if [ "$DEPLOYED" != "$LOCAL" ]; then + echo "Server is on ${DEPLOYED:0:7}, expected ${LOCAL:0:7}." >&2 + exit 1 +fi + +# The container needs a moment to start serving. /village/1 checks the SPA fallback. +for _ in $(seq 1 20); do + PAGE=$(curl -s -o /dev/null -w '%{http_code}' "$URL/" || true) + ROUTE=$(curl -s -o /dev/null -w '%{http_code}' "$URL/village/1" || true) + if [ "$PAGE" = 200 ] && [ "$ROUTE" = 200 ]; then + echo "Live: $URL at ${LOCAL:0:7}" + exit 0 + fi + sleep 2 +done +echo "Deployed ${LOCAL:0:7}, but $URL answers page=$PAGE route=$ROUTE." >&2 +exit 1