Files
loki/docs/infrastructure/self-host-cd.md
CatoandClaude Fable 5.1 944ed15747 feat(cloud): private builders — an allowlisted agent environment, a runner without the database, one sealed unit per account (#1346)
* feat(runner): agents get an allowlisted environment; the cloud builder asks Loki what to clone

Track 1, PRs 2 and 3 of docs/architecture/private-builders.md, together.

- src/lib/agent-execution/agent-env.ts: with LOKI_AGENT_ENV=allowlist (set
  by the box units) an agent PTY is born with a shell's environment, the
  engine keys the unit holds and what its own spec passes — never
  DATABASE_URL, never the org GITHUB_TOKEN. Off the box nothing changes.
- GET /api/control/workspace-source?tab=: a runner asks with its own token
  and gets its own account's repository and a token that account may write
  with (github-org-token.ts). box-workspace.ts reads no database any more;
  the runner passes the run's token to the one agent that needs it
  (GITHUB_TOKEN/GH_TOKEN in the spec env).
- mint-box-runner-token.ts --user <id>: a token per tenant.
- Records: changelog {#agent-env} {#runner-no-db}; roadmap milestone ticked.

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

* feat(cloud): a private builder per account — the flag, the sealed unit, reconciled on the box

Track 1, PRs 4 and 5 of docs/architecture/private-builders.md.

- users.private_builder (migration 0122) is the one switch, set from
  System → Plans (/api/system/private-builder). It grants cloud BUILDING
  only: isCloudBuilderAllowed (founder or flag) for routing, Sessions and
  the kickoff path; isFleetInfrastructureAllowed is the founder alone and
  gates the org GitHub token, studio CD, own domains and the server's own
  PTYs. LOKI_CLOUD_BUILDER_USER_IDS is retired.
- scripts/hetzner/loki-box-runner@.service: one unit per tenant as
  lk-<short>, ProtectSystem=strict, ProtectHome=tmpfs with its own home
  bound, the app and the runner env invisible, MemoryMax/CPUQuota/TasksMax,
  its own dev root, the agent env allowlist, no database password.
- scripts/hetzner/new-tenant.sh: user, clean shell, dev root, a runner
  token minted for THAT user, the agent contract, claude in its own home,
  an env file for its engine keys, the unit. reconcile-tenants.sh runs
  every minute from loki-tenants.timer (install-tenant-reconcile.sh, run by
  the deploy) and brings units into step with the flag. The deploy ships
  the scripts and template and drains and restarts every unit, each in its
  own transient unit.
- A tenant's project path is written under its own root
  (tenantDevRoot), which counts as box-rooted for routing.
- Records: changelog {#new-tenant} {#private-builder-flag}; roadmap.

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

* chore(runner): Fleet Runner 0.8.50 — the allowlisted environment and the API-backed clone

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-20 12:05:27 +02:00

8.1 KiB

Continuous deployment for self-hosted apps

Every app on bitbaum deploys through one pipeline: .github/workflows/selfhost-deploy.yml in this repo, called by a small shim in each app repo.

Why it exists

Before this, scripts/hetzner/install-push-deploy.sh installed a local pre-push hook — so a deploy only happened when someone pushed from the laptop that had the hook. Anything merged on GitHub (which is how PRs land) never reached the box. That is how aoz-begleitung sat with merged fixes and a five-month-old production build: CI was green, main was correct, and nothing had ever shipped it.

The hook is still useful for iterating from the laptop. It is no longer the only path to production.

The pipeline

CI completes green on the default branch
  └─ workflow_run deploy handoff carries exact CI SHA
     └─ confirm/supersede check (no waiting loop)    ci-gate.sh
        └─ pull the app's runtime .env from the box  box stays the env SSOT
        └─ install (npm/pnpm, per lockfile) + build
           + rsync + atomic swap                    deploy.sh
           └─ localhost health check                deploy.sh (auto-rollback)
              └─ public https check                 through Caddy/TLS/DNS

Two properties worth keeping:

  • The box owns prod env. The runner pulls /opt/<app>/shared/.env (or the legacy /opt/<app>/app/.env) over SSH at deploy time. Prod secrets are not copied into a dozen GitHub secret stores, and changing one is a single box-side edit.
  • A missing deploy key fails the job. It does not "skip with a notice" — a deploy that quietly does nothing while reporting green is the failure this pipeline was built to end.

Adding an app

  1. Make sure the app has a row in scripts/hetzner/apps.conf — that file is the SSOT for its port, domain, app directory and database.

  2. Add the deploy key to the app repo (one secret, nothing else):

    gh secret set HETZNER_SSH_PRIVATE_KEY -R bitbaum/<repo> < ~/.ssh/loki_ci_deploy
    
  3. Commit this shim to the app repo as .github/workflows/deploy.yml. Prefer workflow_run after the repo's CI workflow succeeds, pass the originating head_sha as git-ref, and keep workflow_dispatch for manual recovery. The shared workflow accepts git-ref so the build and deploy use that exact CI commit. See the Heidi and AOZ app shims for examples. For a repo without CI, a direct push trigger remains supported but the reusable workflow must gate it.

    Example for a repo whose CI workflow is named CI:

    name: Deploy
    
    on:
      workflow_dispatch: {}
      workflow_run:
        workflows: [CI]
        types: [completed]
        branches: [main]      # use master where that is the default branch
    
    jobs:
      deploy:
        if: github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event != 'pull_request')
        uses: bitbaum/loki/.github/workflows/selfhost-deploy.yml@main
        with:
          app: <apps.conf key>
          git-ref: ${{ github.event.workflow_run.head_sha }}
        secrets:
          HETZNER_SSH_PRIVATE_KEY: ${{ secrets.HETZNER_SSH_PRIVATE_KEY }}
    

    Use an explicit secret mapping (not secrets: inherit). Inherit only works inside the same GitHub org; kickoff repos under a personal account (e.g. catomean/…) calling bitbaum/loki would otherwise fail with "Secret HETZNER_SSH_PRIVATE_KEY is required, but not provided while calling."

    Optional inputs: git-ref (exact app commit; used by workflow_run callers), node-version (fallback 24 when the repo has no .nvmrc), install-flags (e.g. --legacy-peer-deps), and package-manager to override lockfile detection (npm | pnpm | yarn).

The app repo's name and the apps.conf key often differ — revamp-info on the box is served from the hirnli repo, datacat-web from datacat. The shim's app: value is always the apps.conf key.

Giving a site its own domain

A registered site can move from <slug>.orangecat.ch to a domain its owner bought, with the free address forwarding there. See custom-domains.md.

Sites that sleep when idle

Sites that are not live yet start on their first request and stop after 15 idle minutes. See sleep-when-idle.md.

When a deploy fails

Symptom Meaning
CI is not green for <sha> — deploy blocked Working as designed. Fix CI; the next push deploys.
commit superseded (job succeeds, nothing deployed) A newer push is already deploying. Expected.
no runtime .env found on the box The app has never been deployed, or /opt/<app> was renamed.
DEPLOY UNHEALTHY — rolling back The new release failed its health check; prod is back on the previous release. Read journalctl -u <app>-app -n 50.
https://<domain> returned <code> after the deploy The service is up but the public path is not — look at Caddy, not the app.

Loki kickoff (existing repo → CD)

scripts/hetzner/new-site.sh scaffolds a new site from scripts/site-template, which is bitbaum/fleet templates/app (the fleet starter) pinned to a fleet commit — change it in fleet, then scripts/templates/sync-starter.sh; CI proves the copy is exact. Loki Make it happen with the Next.js starter seeds the SAME app (minus its workflows) into a new repo via /api/projects/[id]/provision. Both paths give the site its database and auth/seal secrets on the box (scripts/hetzner/provision-db-on-box.sh). To put a kickoff repo on the same CD path:

  1. Kickoff calls POST /api/projects/[id]/register-cd after provision.

  2. That seeds .github/workflows/deploy.yml (when missing) and, on the studio box (register script + deploy key present, eligible account), runs:

    bash scripts/hetzner/register-site.sh <slug> --repo <owner>/<name> --title '…'
    
  3. On success it writes user_projects.live_url and the production_url attr. When auto-register cannot run, the API returns that same command as the single next step — never a fake "site ready" with no URL.

register-site.sh is the CD half of new-site.sh without scaffolding: apps.conf row, deploy secret, sync-infra. Prefer it over a parallel host.

Auto-register on the studio box

POST /api/projects/[id]/register-cd (also called from Make it happen after provision) runs scripts/hetzner/register-site.sh in-process when all of:

  1. Eligible account — users.is_default (isFleetInfrastructureAllowed; studio CD is the fleet's, not any private builder's).
  2. Script present — resolved in order: LOKI_REGISTER_SITE_SCRIPT, $LOKI_REPO_ROOT/scripts/hetzner/register-site.sh, $LOKI_BOX_DEV_ROOT/loki/... (default durable root /home/ubuntu/dev on the box), process.cwd()/scripts/..., then /opt/loki/app/scripts/hetzner/register-site.sh.
  3. Deploy key readable — DEPLOY_KEY_PATH or /home/ubuntu/.ssh/loki_ci_deploy (the same key new-site.sh pipes into gh secret set HETZNER_SSH_PRIVATE_KEY). The production app runs as User=ubuntu; a key that only exists on a laptop will make auto-register return command-only with an explicit missing-key reason.
  4. Not disabled — unset LOKI_SITE_CD_AUTO (or anything other than 0).

When any gate fails, the API returns command and reason / gate — the kickoff UI must show the reason (not only the bash line). Use Register site on the project header to retry (register-cd is idempotent once liveUrl is set).

One-time box setup (Cato / loki.orangecat.ch)

# On the studio box, as ubuntu — durable checkout + deploy key
test -f /home/ubuntu/dev/loki/scripts/hetzner/apps.conf
install -m 600 /path/to/loki_ci_deploy /home/ubuntu/.ssh/loki_ci_deploy

# Optional explicit env in /opt/loki/app/.env (EnvironmentFile):
# LOKI_REPO_ROOT=/home/ubuntu/dev/loki
# LOKI_BOX_DEV_ROOT=/home/ubuntu/dev
# DEPLOY_KEY_PATH=/home/ubuntu/.ssh/loki_ci_deploy

Keep /home/ubuntu/dev/loki on main so apps.conf edits survive the next /opt/loki/app release swap. liveUrl is written only after register-site.sh exits 0 — never faked.