Files
loki/.github/workflows/deploy.yml
T
cf3d9ed52e fix(deploy): hand the VAPID public key to the build, so push can work at all (#490)
#488 stopped the hydration failure by rendering nothing until past the server
render. It could not restore the feature: the browser still received an empty
NEXT_PUBLIC_VAPID_PUBLIC_KEY, so subscribe() had no application server key and
push notifications were dead in production regardless.

The missing half was never in the code. deploy.yml's build step declared no
`env:` at all, so every NEXT_PUBLIC_* was inlined empty while the box ran with
this one set. Fixed by creating the repo variable and passing it to the build.

A variable, not a secret: the value is compiled into the client bundle and
handed to every browser that subscribes. Hiding it would be theatre, and a
secret's log masking would only obscure the verification.

Verified before trusting the value, because a wrong key fails silently at send
time and this one gets baked into every future bundle:

  - decodes to 65 bytes, first byte 0x04 — a valid uncompressed P-256 point
  - the public key is the MATE of VAPID_PRIVATE_KEY on the box (derived the
    point from the private scalar and compared; no secret printed)
  - VAPID_SUBJECT is a valid mailto:/https: scheme

One trap on the way: the box stores the value quote-wrapped (len 89 = 87 + 2).
dotenv strips those when the server loads them, so runtime was fine — but
copying it verbatim into the variable would have inlined literal quotes and
produced a broken key that looks correct in every listing.

The gate's KNOWN_GAPS entry is deleted rather than edited, so removing either
the variable or the env: line now fails the check instead of silently killing
push again. Mutation-proven both ways: line removed -> 4 passed, 1 failed
naming the var; restored -> 5 passed, 0 failed.


Claude-Session: https://claude.ai/code/session_01UvjGNAS9CMfEGNW26tUR4P

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-05 08:27:25 +00:00

145 lines
7.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. (optional) repo variable HETZNER_IP (defaults to 167.233.22.31). │
# │ 3. DISABLE the local hook or you'll deploy twice: comment out the │
# │ `>>> fleetcrown 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/dotfiles (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: fleetcrown
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/fleetcrown
HETZNER_IP: ${{ vars.HETZNER_IP || '167.233.22.31' }}
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 }}
run: |
pnpm run build
pnpm -C bridge run build
- 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