Files
loki/.github/workflows/deploy.yml
CatoandClaude Fable 5.1 f260f0bc52 feat(selfhost): a copy of Loki is a copy — own hostname, own OrangeCat, a complete env contract (#1023)
* feat(selfhost): a copy of Loki is a copy — own hostname, own OrangeCat, a complete env contract

- APP_DOMAIN reads NEXT_PUBLIC_APP_DOMAIN (fallback loki.orangecat.ch), so
  callbacks, emails and copy carry a fork's own name; brand-sync still
  holds the shell mirror to the fallback.
- orangeCatProjectUrl() in config/orangecat.ts replaces the literal
  https://orangecat.ch/projects/… in three components, built on
  NEXT_PUBLIC_ORANGECAT_URL.
- .env.example names every variable src/ reads — 68 were not named, so a
  fork found Google sign-in, embeddings, the Telegram bot or the daemon
  token only by grepping process.env — each with the file that reads it,
  and scripts/test/env-example-covers-src.ts (picked up by test:unit) fails
  when one is missing. A comment that mentions a variable is not a read.
- README: "Run your own copy" — docker compose up, /setup, hostname,
  optional OrangeCat, agents on your own machine, the env contract; the
  name and identity are not part of the licence.

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

* fix(deploy): hand the newly declared NEXT_PUBLIC_* to the build, readers treat "" as unset

public-env-inlined on 070f951b: declaring NEXT_PUBLIC_APP_DOMAIN,
NEXT_PUBLIC_LOKI_BRIDGE_URL and the two ecosystem addresses in
.env.example made them "something an operator may set" — and none reached
the build, so a box that set one would render a different tree than the
browser (the production-only hydration failure that check exists for).

deploy.yml's build step now inlines all four from repo variables. An unset
variable arrives as "", so the readers use || rather than ??: an empty
inlined value falls back to the same default on both sides.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-03 01:09:28 -07:00

152 lines
7.5 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/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: 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: 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