Files
loki/scripts/hetzner/deploy.sh
T
65dea6f303 chore: pnpm — the fleet has one package manager (#478)
* chore: pnpm — the fleet has one package manager

- packageManager pnpm@11.25.0; lockfile via pnpm import, package-lock.json
  removed; npm "workspaces" replaced by pnpm-workspace.yaml packages
  (packages/agent), and bridge/ joins the workspace — one install, one
  lockfile, its own package-lock.json removed
- npm "overrides" (fast-uri pin) moved to pnpm-workspace.yaml (pnpm ignores
  the npm-only field)
- allowBuilds: better-sqlite3, node-pty, @sentry/cli, native toolchain, and
  the two git-hosted deps (@fleet/ai-forms, limitkit — pnpm wants the exact
  resolved spec, keys move with the pinned tags); temporary
  minimumReleaseAgeExclude for our own @bitbaum/ai-kit@0.6.2
- phantom deps declared: @auth/core (src/auth.ts augments @auth/core/jwt —
  TS2664 under pnpm's strict layout), dotenv, sharp (scripts)
- CI/audit/deploy workflows: pnpm/action-setup@v6, store-dir pinned outside
  the action's bin dir, cache: pnpm, frozen-lockfile; deploy's separate
  `npm --prefix bridge ci` is covered by the workspace install; Next cache
  key hashes pnpm-lock.yaml; npm audit → pnpm audit
- desktop-release: root install → pnpm; desktop/ itself STAYS an npm package
  (own package-lock.json, electron toolchain untouched)
- husky: pre-push staleness gate now compares pnpm-lock.yaml against
  node_modules/.modules.yaml; hooks + scripts (deploy-hetzner, hetzner/deploy,
  install-app, doctor, smoke) converted to pnpm
- check-deploy-ready fleet gate now recognizes pnpm verify/test lines too

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WqKqMnHQHSmkGFfc5t7Rxn

* fix: prettier-format pnpm-workspace.yaml — format:check gates verify

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WqKqMnHQHSmkGFfc5t7Rxn

---------

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-04 10:35:27 +00:00

168 lines
8.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build a Next.js app (standalone) and deploy it to /opt/<name>/app on the box.
# Generalizes the orangecat/revampit cutover flow from the runbook:
# build → stage standalone + static + public → rsync → restart → health check.
# Runtime env: <repo>/<app_dir>/.env.selfhost.local is uploaded as /opt/<name>/app/.env
# ONLY if missing on the box (box env is never overwritten — fix it there or pass --env).
# Usage: deploy.sh <app> [--env] (--env force-uploads .env.selfhost.local)
# Absolute script dir, resolved BEFORE the cd into the app repo below — a
# relative invocation (`bash deploy.sh app` from this dir) otherwise resolves
# sibling scripts as ./apply-schema.sh inside the APP repo and aborts the
# deploy. The push-deploy hook always passed absolute paths, which is why this
# never fired until a manual relative run (2026-07-17).
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$HERE/lib.sh"
app_lookup "${1:?usage: deploy.sh <app> [--env]}"
FORCE_ENV="${2:-}"
# apps.conf records the LAPTOP checkout path. A CI runner has the same repo at a
# different path, so REPO_OVERRIDE re-points just that field — name/port/domains/
# app_dir/db still come from the manifest, which stays the single source of truth.
REPO="${REPO_OVERRIDE:-$REPO}"
SRC="$REPO/$APP_DIR"
[ -d "$SRC" ] || { echo "ERROR: $SRC missing"; exit 1; }
cd "$SRC"
# Build with the runtime env: NEXT_PUBLIC_* is baked at build time, and SSG
# pages may query the DB — tunnel build-time DB access to the box's Postgres.
[ -f "$SRC/.env.selfhost.local" ] || { echo "ERROR: $SRC/.env.selfhost.local missing — run gen-env.sh $NAME"; exit 1; }
set -a; source "$SRC/.env.selfhost.local"; set +a
STAGE=""
TUNNEL_SOCK=$(mktemp -u /tmp/deploy-tunnel.XXXXXX)
ssh -o BatchMode=yes -f -N -M -S "$TUNNEL_SOCK" -L 15432:localhost:5432 "$BOX"
# `ssh -f` returns as soon as it backgrounds itself, which is BEFORE the
# forwarded port is necessarily accepting connections. prisma then connects to
# nothing and the deploy aborts at the schema step with a bare P1001 that reads
# like "the box's Postgres is down" — it is not, the tunnel just was not up yet.
# Intermittent by nature: it depends on how fast the box answers, so it passes
# on a quiet box and fails on a loaded one. Wait for the port to actually accept.
tunnel_up=false
for _ in $(seq 1 40); do
if (exec 3<>/dev/tcp/127.0.0.1/15432) 2>/dev/null; then
exec 3<&- 3>&-; tunnel_up=true; break
fi
sleep 0.25
done
if ! $tunnel_up; then
echo "ERROR: DB tunnel to $BOX never started listening on 127.0.0.1:15432 (waited 10s)."
echo " Check SSH access to $BOX; the box's Postgres itself may be fine."
ssh -S "$TUNNEL_SOCK" -O exit "$BOX" 2>/dev/null || true
exit 1
fi
trap 'ssh -S "$TUNNEL_SOCK" -O exit "$BOX" 2>/dev/null; [ -n "$STAGE" ] && rm -rf "$STAGE"' EXIT
[ -n "${DATABASE_URL:-}" ] && export DATABASE_URL="${DATABASE_URL/@localhost:5432/@127.0.0.1:15432}"
[ -n "${DIRECT_URL:-}" ] && export DIRECT_URL="${DIRECT_URL/@localhost:5432/@127.0.0.1:15432}"
# Reconcile DB schema BEFORE building — SSG may query columns/tables that the new
# code introduces. Guarded: applies only pending, additive migrations and refuses
# any destructive diff (aborts without shipping). See apply-schema.sh.
if [ "$DB" != "-" ]; then
"$HERE/apply-schema.sh" "$NAME" "$REPO" "$DB" "$APP_DIR" \
|| { echo "ERROR: schema step failed — deploy aborted (no code shipped)"; exit 1; }
fi
echo "=== build $NAME ($SRC) ==="
SELF_HOST=1 pnpm run build
ST="$SRC/.next/standalone"
[ -d "$ST" ] || { echo "ERROR: no standalone output — set output:'standalone' in next.config"; exit 1; }
STAGE=$(mktemp -d)
cp -r "$ST"/. "$STAGE/"
# static + public live next to the (possibly monorepo-nested) server.js
NEST=$(cd "$STAGE" && find . -maxdepth 4 -name server.js -not -path '*node_modules*' | head -1 | xargs dirname)
cp -r "$SRC/.next/static" "$STAGE/$NEST/.next/static"
[ -d "$SRC/public" ] && cp -r "$SRC/public" "$STAGE/$NEST/public"
# ── Releases layout (rollback-able deploys) ─────────────────────────────────
# /opt/<name>/shared/{.env,launch.sh} persistent box-side files
# /opt/<name>/releases/<ts>-<sha>/ one dir per deploy
# /opt/<name>/app -> releases/<ts>-<sha> ATOMIC symlink; systemd units keep
# pointing at /opt/<name>/app unchanged.
# A failed health check flips the symlink back to the previous release
# (auto-rollback); rollback.sh <app> does the same on demand. Legacy layout
# (app as a real dir) is migrated in place on the first release-deploy.
REL="$(date +%Y%m%d-%H%M%S)-$(git -C "$REPO" rev-parse --short HEAD 2>/dev/null || echo nosha)"
RELDIR="/opt/$NAME/releases/$REL"
# One-time shared/ bootstrap: COPY (not move — the running service still uses
# them) .env + launch.sh out of a legacy app dir. Idempotent.
box "set -e
mkdir -p /opt/$NAME/releases /opt/$NAME/shared
if [ -d /opt/$NAME/app ] && [ ! -L /opt/$NAME/app ]; then
[ -f /opt/$NAME/shared/.env ] || cp -p /opt/$NAME/app/.env /opt/$NAME/shared/.env 2>/dev/null || true
[ -f /opt/$NAME/shared/launch.sh ] || cp -p /opt/$NAME/app/launch.sh /opt/$NAME/shared/launch.sh 2>/dev/null || true
fi
# launch.sh must resolve its dir PHYSICALLY: /opt/<name>/app is a symlink in
# the releases layout, and its server.js search does not descend a logical
# symlink path — vitareba crash-looped on the first release-deploy until
# pwd -P (2026-07-17). NB: no backticks in this box \"...\" string — they run
# as command substitution on the box (that is what emitted find\\: not found).
[ -f /opt/$NAME/shared/launch.sh ] && sed -i 's|&& pwd)\"|\&\& pwd -P)\"|' /opt/$NAME/shared/launch.sh"
if [ "$FORCE_ENV" = "--env" ] || ! box "test -f /opt/$NAME/shared/.env"; then
[ -f "$SRC/.env.selfhost.local" ] || { echo "ERROR: $SRC/.env.selfhost.local missing"; exit 1; }
scp -o BatchMode=yes "$SRC/.env.selfhost.local" "$BOX:/opt/$NAME/shared/.env"
# Ownership, not just mode. The app sources this file as its unit's User=;
# a .env it cannot read is a total outage (twice on 2026-08-28), and the
# correct owner is never in doubt. Assert it here as well as in
# host-check's repair loop, so the invariant is stated where the file is
# written and not only where it is later found broken.
box "sudo chown ubuntu:ubuntu /opt/$NAME/shared/.env && chmod 600 /opt/$NAME/shared/.env"
echo "uploaded .env → shared/"
fi
echo "=== rsync → $RELDIR ==="
for attempt in 1 2 3; do
rsync -az --partial \
-e "ssh -o BatchMode=yes -o ServerAliveInterval=15 -o ServerAliveCountMax=4" \
--exclude '.env' --exclude 'launch.sh' \
"$STAGE"/ "$BOX:$RELDIR/" && break
echo "rsync attempt $attempt failed, retrying in 5s..."; sleep 5
[ "$attempt" = 3 ] && { echo "ERROR: rsync failed after 3 attempts"; exit 1; }
done
# Wire shared files into the release, remember the current target for
# rollback, then swap ATOMICALLY (ln to temp name + rename over) and restart.
PREV=$(box "readlink /opt/$NAME/app 2>/dev/null || true")
box "set -e
ln -sfn /opt/$NAME/shared/.env '$RELDIR/.env'
ln -sfn /opt/$NAME/shared/launch.sh '$RELDIR/launch.sh'
if [ -d /opt/$NAME/app ] && [ ! -L /opt/$NAME/app ]; then
mv /opt/$NAME/app /opt/$NAME/releases/legacy-\$(date +%s)
fi
ln -sfn '$RELDIR' /opt/$NAME/app.new && mv -T /opt/$NAME/app.new /opt/$NAME/app
sudo systemctl restart $NAME-app"
# Poll for health up to 60s — a Next standalone cold boot routinely needs >4s;
# a single early sample read 'activating/000' and (correctly) failed a healthy
# deploy. Break on the first good sample; keep the last sample for the verdict.
status=inactive; code=000
for _ in $(seq 1 15); do
sleep 4
status=$(box "systemctl is-active $NAME-app 2>/dev/null" || true)
code=$(box "curl -s -o /dev/null -w '%{http_code}' --max-time 10 http://localhost:$PORT/ 2>/dev/null" || true)
code="${code:-000}"
[ "$status" = "active" ] && [ "$code" -ge 200 ] 2>/dev/null && [ "$code" -lt 400 ] && break
[ "$status" = "failed" ] && break # crash-loop: no point waiting the full window
done
echo "RESULT $NAME: systemd=$status http=$code release=$REL"
if [ "$status" = "active" ] && [ "$code" -ge 200 ] && [ "$code" -lt 400 ]; then
# Healthy — prune old releases, keep the newest 3 (current is among them).
box "cd /opt/$NAME/releases && ls -1t | tail -n +4 | xargs -r rm -rf"
exit 0
fi
# Unhealthy — auto-rollback to the previous release if one exists.
if [ -n "$PREV" ]; then
echo "DEPLOY UNHEALTHY — rolling back to $PREV"
box "ln -sfn '$PREV' /opt/$NAME/app.new && mv -T /opt/$NAME/app.new /opt/$NAME/app && sudo systemctl restart $NAME-app"
sleep 4
rb=$(box "systemctl is-active $NAME-app || true")
echo "ROLLBACK $NAME: systemd=$rb → $PREV"
else
echo "DEPLOY UNHEALTHY — no previous release to roll back to (first release-deploy)"
fi
exit 1