Files
evig/.github/workflows/deploy-selfhost.yml

230 lines
11 KiB
YAML

name: Deploy production app
# Push to main → build + rsync to production app evig.orangecat.ch.
# One-time setup (Settings → Secrets → Actions):
# HETZNER_SSH_PRIVATE_KEY — private key for ubuntu@<box>; the box IP itself is
# the org Actions variable HETZNER_IP, not a literal here
# SELFHOST_ENV — full contents of .env.selfhost.local (multiline OK)
# AUTH_TEST_USER_PASSWORD — password for the non-admin E2E account. The
# address is deliberately not recorded here: pass it
# in AUTH_TEST_USER_EMAIL (see scripts/e2e-inventory-prod.sh).
# AUTH_TEST_ADMIN_PASSWORD — password for the staff (admin) E2E account; its
# address comes from AUTH_TEST_ADMIN_EMAIL the same way.
# Both are real personal mailboxes, so their addresses are configuration, not
# source: src/config/e2e-test-accounts.ts reads E2E_TEST_ACCOUNT_EMAILS for the
# same reason, and this repo is public.
# Without deploy secrets the job logs a notice and exits cleanly.
# Without E2E passwords the post-deploy inventory step is skipped.
on:
workflow_dispatch: {}
workflow_run:
workflows: [CI Pipeline]
types: [completed]
branches: [main]
# Queue deploys; do NOT cancel them.
#
# Measured 2026-09-07: with `cancel-in-progress: true` and a ~13-minute deploy,
# 4 of this repo's last 10 deploys were CANCELLED — 40%, against 0% for every
# other repo in the fleet. Three in a row cancelled each other on one afternoon
# and a merged fix took three attempts to reach production. Cancelling is safe
# only if the newer run is guaranteed to finish, and at 13 minutes it is not.
#
# Queuing plus the supersede check in the CI gate below gives the intended
# behaviour without the loss: a superseded commit exits early and cheaply,
# and the newest commit always deploys.
concurrency:
group: deploy-selfhost-${{ github.ref }}
cancel-in-progress: false
jobs:
deploy:
if: github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event != 'pull_request')
name: evig.orangecat.ch
runs-on: ubuntu-latest
timeout-minutes: 45
outputs:
deployed: ${{ steps.mark-deployed.outputs.deployed }}
env:
HETZNER_SSH_PRIVATE_KEY: ${{ secrets.HETZNER_SSH_PRIVATE_KEY }}
SELFHOST_ENV: ${{ secrets.SELFHOST_ENV }}
# The box IP has ONE definition for the whole org: the `HETZNER_IP`
# Actions variable (visibility: all). It was duplicated here as a literal
# and again in scripts/selfhost-deploy-evig.sh, so re-IPing the box meant
# finding every copy. Both now read this.
HETZNER_IP: ${{ vars.HETZNER_IP }}
steps:
- name: Check secrets configured
id: secrets
run: |
if [ -z "$HETZNER_SSH_PRIVATE_KEY" ] || [ -z "$SELFHOST_ENV" ]; then
echo "::notice::HETZNER_SSH_PRIVATE_KEY and/or SELFHOST_ENV not set — skipping self-host deploy. Push from a machine with .env.selfhost.local still deploys via the pre-push hook."
echo "configured=false" >> "$GITHUB_OUTPUT"
else
echo "configured=true" >> "$GITHUB_OUTPUT"
fi
- name: Checkout code
if: steps.secrets.outputs.configured == 'true'
uses: actions/checkout@v7
with:
ref: ${{ github.event.workflow_run.head_sha || github.sha }}
# No version input: pnpm/action-setup reads packageManager from package.json.
- uses: pnpm/action-setup@v6
if: steps.secrets.outputs.configured == 'true'
# The action's default store nests inside its own bin dir; a git-dep
# prepare build then dies with TS2742. Pin the store somewhere sane.
- name: Set pnpm store dir
if: steps.secrets.outputs.configured == 'true'
run: pnpm config set store-dir ~/.pnpm-store --global
- name: Setup Node.js
if: steps.secrets.outputs.configured == 'true'
uses: actions/setup-node@v7
with:
node-version: '24'
cache: pnpm
- name: Install dependencies
if: steps.secrets.outputs.configured == 'true'
run: pnpm install --frozen-lockfile
# Persist Next.js's incremental compiler cache across runs. The build runs
# on this ephemeral runner, so without this every build is COLD (full
# recompile). restore-keys falls back to the latest cache for the same
# lockfile, making subsequent builds incremental (warm).
- name: Cache Next.js build
if: steps.secrets.outputs.configured == 'true'
uses: actions/cache@v6
with:
path: .next/cache
key: nextjs-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}-${{ github.sha }}
restore-keys: |
nextjs-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}-
# ONE definition of "verified" — but REQUIRED, not re-run.
#
# This step used to be `pnpm run verify`, which is lint + umlauts + chrome
# + typecheck + test + build. CI Pipeline runs exactly that bundle on this
# same commit, and the two ran CONCURRENTLY — so the commit was verified
# twice in parallel and built four times in total per merge (CI verify,
# CI's E2E "Build app", this one, and the build inside the deploy script
# that actually ships). Only the last produces the artifact.
#
# Requiring CI's green is not a weaker gate than re-running it: the same
# bundle must pass, it simply has to pass ONCE. What made an earlier
# version of this step weaker was that it re-inlined a SUBSET (no
# lint:chrome, i18n only) — the fix for that is to depend on the full
# bundle, not to run a second copy of it.
#
# Exit paths: green -> deploy; red -> fail loudly; superseded (main has
# moved past this commit) -> skip, because the newer commit's deploy
# ships this change too. Fail-closed on timeout.
- name: Require CI green on this commit
id: gate
if: steps.secrets.outputs.configured == 'true'
env:
GH_TOKEN: ${{ github.token }}
TARGET_SHA: ${{ github.event.workflow_run.head_sha || github.sha }}
EVENT_NAME: ${{ github.event_name }}
CI_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
CI_EVENT: ${{ github.event.workflow_run.event }}
run: |
set +e
# workflow_run already means the named CI workflow completed. Validate
# the success and source event, then avoid starting a polling loop.
if [ "$EVENT_NAME" = workflow_run ]; then
if [ "$CI_CONCLUSION" != success ] || [ "$CI_EVENT" = pull_request ] || [ -z "$TARGET_SHA" ]; then
echo "::error::Refusing deploy for CI conclusion=$CI_CONCLUSION event=$CI_EVENT sha=$TARGET_SHA"
exit 1
fi
head=$(gh api "repos/$GITHUB_REPOSITORY/commits/main" --jq .sha 2>/dev/null)
if [ -n "$head" ] && [ "$head" != "$TARGET_SHA" ]; then
echo "proceed=false" >> "$GITHUB_OUTPUT"
echo "::notice::superseded by ${head:0:8} — its deploy ships this change"
exit 0
fi
echo "proceed=true" >> "$GITHUB_OUTPUT"
exit 0
fi
for _ in $(seq 1 90); do
head=$(gh api "repos/$GITHUB_REPOSITORY/commits/main" --jq .sha 2>/dev/null)
if [ -n "$head" ] && [ "$head" != "$TARGET_SHA" ]; then
echo "proceed=false" >> "$GITHUB_OUTPUT"
echo "::notice::superseded by ${head:0:8} — its deploy ships this change"
exit 0
fi
state=$(gh api \
"repos/$GITHUB_REPOSITORY/actions/runs?head_sha=$TARGET_SHA&per_page=50" \
--jq '[.workflow_runs[] | select(.name=="CI Pipeline")] | first
| "\(.status) \(.conclusion)"' 2>/dev/null)
case "$state" in
"completed success")
echo "proceed=true" >> "$GITHUB_OUTPUT"; exit 0 ;;
"completed "*)
echo "::error::CI Pipeline is $state for $TARGET_SHA — deploy blocked"
exit 1 ;;
esac
sleep 20
done
echo "::error::timed out waiting for CI Pipeline on $TARGET_SHA"
exit 1
- name: Write selfhost env
if: steps.secrets.outputs.configured == 'true' && steps.gate.outputs.proceed == 'true'
run: |
printf '%s' "$SELFHOST_ENV" > .env.selfhost.local
chmod 600 .env.selfhost.local
- name: Setup SSH
if: steps.secrets.outputs.configured == 'true' && steps.gate.outputs.proceed == 'true'
run: |
mkdir -p ~/.ssh
printf '%s\n' "$HETZNER_SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
# Fail loudly rather than keyscanning an empty host: `ssh-keyscan -H ""`
# writes nothing, exits 0, and the deploy then dies much later with an
# unhelpful host-key error. An unset org variable is a configuration
# bug, not a reason to continue.
if [ -z "$HETZNER_IP" ]; then
echo "::error::HETZNER_IP is empty — set the org Actions variable HETZNER_IP (visibility: all) to the box IP."
exit 1
fi
ssh-keyscan -H "$HETZNER_IP" >> ~/.ssh/known_hosts
- name: Deploy
if: steps.secrets.outputs.configured == 'true' && steps.gate.outputs.proceed == 'true'
# Inherits HETZNER_IP from the job env above; the deploy script builds
# BOX from it and refuses to run without it.
run: bash scripts/selfhost-deploy-evig.sh
# Must carry the gate too: this output is what starts the prod smoke job,
# and a superseded run never deployed anything. Marking it deployed would
# smoke-test a release this run did not make — and when that smoke fails
# it reports against THIS commit, blaming a change that never shipped.
- name: Mark deployed
id: mark-deployed
if: steps.secrets.outputs.configured == 'true' && steps.gate.outputs.proceed == 'true'
run: echo "deployed=true" >> "$GITHUB_OUTPUT"
post-deploy-smoke:
name: Read-only prod smoke
runs-on: ubuntu-latest
needs: deploy
if: needs.deploy.outputs.deployed == 'true'
timeout-minutes: 5
steps:
- name: Checkout code
uses: actions/checkout@v7
with:
ref: ${{ github.event.workflow_run.head_sha || github.sha }}
# Read-only: public pages render + DB-backed public APIs return success.
# No login, no prod mutation. The heavier authenticated dual-persona
# journeys (which mutate prod) are manual: pnpm run test:e2e:inventory:prod.
- name: Run read-only smoke (public pages + APIs)
run: bash scripts/post-deploy-smoke.sh