Files
loki/.github/workflows/deploy.yml

179 lines
9.0 KiB
YAML

# CI-gated production deploy (DevOps remediation #2).
#
# Today the Hetzner deploy fires from the LOCAL pre-push hook, so a red CI still
# ships and `--no-verify` skips every gate. This workflow makes CI the gate:
# it runs ONLY after the CI workflow finishes successfully on main, then builds
# and ships. A failing CI → no deploy.
#
# ┌─ BEFORE YOU ENABLE THIS (it is inert until on `main` + secrets exist) ─────┐
# │ 1. Add repo secret HETZNER_SSH_KEY = a private key whose public half is in │
# │ the box's root authorized_keys (a deploy-only key, ideally). │
# │ 2. org variable HETZNER_IP — the box address, set once for the org. │
# │ 3. DISABLE the local hook or you'll deploy twice: comment out the │
# │ `>>> loki push-deploy >>>` block in .husky/pre-push. │
# │ 4. Merge this file to main. First run: watch the "Build" step — it builds │
# │ against a schema-only Postgres (no seed); if a page pre-renders off real │
# │ data and fails, add a minimal seed step here. │
# └────────────────────────────────────────────────────────────────────────────┘
name: Deploy
on:
workflow_run:
workflows: ["CI"] # the job in ci.yml
types: [completed]
branches:
[main] # a PR's CI is not a ship signal — filter it out
# here rather than creating a run that only skips
# Ships whatever is on main right now. Required, not a convenience: a CI run
# that GITHUB_TOKEN started (the auto-merge re-arm) emits NO workflow_run
# event, so the chain above never fires for an auto-merged PR — observed
# 2026-08-05 with three merges live on main and none of them deployed. The
# sweep dispatches this directly instead — it compares main's tip against the
# last successful run here and closes the gap. The sweep now lives in
# bitbaum/fleet (scripts/ci/auto-merge-sweep.sh); this repo enables the
# reconciler by passing `deploy_workflow: deploy.yml` in auto-merge.yml.
# Removing that input makes merges land and never ship.
workflow_dispatch: {}
# One deploy at a time; never cancel a ship mid-flight.
concurrency:
group: deploy-prod
cancel-in-progress: false
jobs:
deploy:
# Chained: only a GREEN CI run that was on main. Dispatched: the caller is
# responsible for the gate (the sweep checks main's CI before dispatching).
# Kill switch: dormant until you set repo variable DEPLOY_VIA_CI=true. This
# lets the workflow live on main (green PR, reviewed) WITHOUT deploying until
# you've added HETZNER_SSH_KEY and disabled the local push-deploy hook. Unset
# variable → '' → never runs.
if: >
vars.DEPLOY_VIA_CI == 'true' &&
(github.event_name == 'workflow_dispatch' ||
(github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_branch == 'main'))
runs-on: ubuntu-latest
# Build does DB queries during static pre-render, so give it a schema-only
# Postgres (drizzle-kit push below). No real data — see the seed caveat above.
services:
postgres:
image: postgres:17
env:
POSTGRES_PASSWORD: ci
POSTGRES_DB: loki
ports: ["5432:5432"]
options: >-
--health-cmd pg_isready --health-interval 5s
--health-timeout 5s --health-retries 10
env:
DATABASE_URL: postgres://postgres:ci@localhost:5432/loki
HETZNER_IP: ${{ vars.HETZNER_IP }}
steps:
- uses: actions/checkout@v7
with:
# Chained: the exact CI-green commit. Dispatched: the tip of the ref
# it was dispatched on. Never an empty string — that silently checks
# out the default branch and would ship something nobody verified.
ref: ${{ github.event.workflow_run.head_sha || github.sha }}
- uses: pnpm/action-setup@v6
# setup-pnpm's default store nests inside its own bin dir, which breaks
# any prepare-building dep (portable-type TS2742) — pin it outside.
- run: pnpm config set store-dir ~/.pnpm-store --global
- uses: actions/setup-node@v7
with:
node-version: 22
cache: pnpm
# Root deps AND the bridge subpackage — bridge/ is its own npm package with
# its own lockfile (pg, @types/pg). The build step below runs `tsc` in it,
# so without this the bridge build fails with "Cannot find module 'pg'".
# One workspace install covers root AND bridge/ (both in
# pnpm-workspace.yaml) — the separate `npm --prefix bridge ci` is gone.
- run: pnpm install --frozen-lockfile
- name: Push schema to CI Postgres (build needs it)
run: pnpm exec drizzle-kit push
# Next's incremental compiler cache. Every deploy compiled the whole app
# from cold because the runner is ephemeral — the one part of the pipeline
# that is genuinely WORK rather than waiting, so it is worth making
# smaller. Keyed on the lockfile plus the sources, with a lockfile-only
# fallback so a normal source change still starts from a warm cache.
# `.next/cache` is compiler state, not output: what ships is
# `.next/standalone`, rebuilt every run either way.
- name: Cache Next.js build cache
uses: actions/cache@v6
with:
path: .next/cache
key: ${{ runner.os }}-next-${{ hashFiles('pnpm-lock.yaml') }}-${{ hashFiles('src/**/*.ts', 'src/**/*.tsx') }}
restore-keys: |
${{ runner.os }}-next-${{ hashFiles('pnpm-lock.yaml') }}-
${{ runner.os }}-next-
# A NEXT_PUBLIC_* value has two origins: the server reads it at runtime,
# the browser gets what was inlined HERE, at build time. Anything the box
# sets at runtime must therefore also be handed to this step, or the two
# disagree — which is what rendered a notifications bell on the server and
# nothing on the client, throwing away the tree on every authenticated
# page (React #418, #488). scripts/test/public-env-inlined.ts enforces it.
- name: Build (app + bridge)
env:
# Public by design — it is compiled into the client bundle and handed
# to every browser that subscribes. A repo variable, not a secret.
NEXT_PUBLIC_VAPID_PUBLIC_KEY: ${{ vars.NEXT_PUBLIC_VAPID_PUBLIC_KEY }}
# Declared in .env.example (the env contract), so an operator may set
# them; inlined here from repo variables so the browser and the box
# agree. Empty when unset — every reader treats "" as unset (||).
NEXT_PUBLIC_APP_DOMAIN: ${{ vars.NEXT_PUBLIC_APP_DOMAIN }}
NEXT_PUBLIC_LOKI_BRIDGE_URL: ${{ vars.NEXT_PUBLIC_LOKI_BRIDGE_URL }}
NEXT_PUBLIC_ECOSYSTEM_BITCOIN_ADDRESS: ${{ vars.NEXT_PUBLIC_ECOSYSTEM_BITCOIN_ADDRESS }}
NEXT_PUBLIC_ECOSYSTEM_LIGHTNING_ADDRESS: ${{ vars.NEXT_PUBLIC_ECOSYSTEM_LIGHTNING_ADDRESS }}
run: |
pnpm run build
pnpm -C bridge run build
- name: Resolve release receipt visibility
id: receipt_scope
env:
GH_TOKEN: ${{ github.token }}
run: bash scripts/hetzner/receipt-visibility.sh auto "$GITHUB_REPOSITORY"
- name: Stage verified public package receipt
run: |
set -euo pipefail
# The resolver accepts only GitHub's matching, typed repository metadata.
test "${{ steps.receipt_scope.outputs.public }}" = true
producer="$RUNNER_TEMP/fleet-receipt-producer"
git init --quiet "$producer"
git -C "$producer" remote add origin https://github.com/bitbaum/fleet.git
git -C "$producer" fetch --quiet --depth 1 origin 5da9886a5211dc74dc6b7c8f5eade60c460cea6c
git -C "$producer" checkout --quiet --detach FETCH_HEAD
FLEET_RECEIPT_PUBLIC=true FLEET_RECEIPT_ROOT="$producer" \
FLEET_RECEIPT_REPO="$GITHUB_REPOSITORY" \
bash scripts/hetzner/stage-release-receipt.sh "$GITHUB_WORKSPACE" "$GITHUB_WORKSPACE" "$GITHUB_WORKSPACE/.next/standalone"
test -s .next/standalone/public/.well-known/fleet-release.json
- name: SSH setup
run: |
mkdir -p ~/.ssh
printf '%s\n' "${{ secrets.HETZNER_SSH_KEY }}" > ~/.ssh/id_deploy
chmod 600 ~/.ssh/id_deploy
ssh-keyscan -H "$HETZNER_IP" >> ~/.ssh/known_hosts 2>/dev/null
printf 'Host %s\n IdentityFile ~/.ssh/id_deploy\n IdentitiesOnly yes\n' "$HETZNER_IP" >> ~/.ssh/config
- name: Ship to box (reuses deploy-hetzner.sh — flock, rollback, verify)
run: bash scripts/deploy-hetzner.sh --no-build
- name: Verify the served package receipt
run: |
node "$RUNNER_TEMP/fleet-receipt-producer/scripts/ci/verify-live-release-receipt.mjs" \
--url https://loki.orangecat.ch/.well-known/fleet-release.json \
--expected .next/standalone/public/.well-known/fleet-release.json