Files
tern-server/deploy
Mateusz PstruchaandClaude Opus 5.5 22c55854f3
CI / test (push) Successful in 4m39s
Add the tern.mtps.pl deployment, a registration gate and a healthcheck
deploy/tern.yml is the whole stack as one compose project for
ygg/actions/deploy (deploy/compose on kara): Postgres, the server at the
released version, ygg/static-server serving the matching tern-web
release, and an nginx proxy putting webapp and API (incl. /api/ws) on
one origin on port 11901, with sign-in rate-limited. Kara's nginx
fronts it as tern.mtps.pl. tern-web's release triggers the deploy.

Only the first user may now register unless OPEN_REGISTRATION=true,
so a public deployment isn't open sign-up; an advisory lock keeps two
racing first registrations from both succeeding.

`tern-server healthcheck` backs a Dockerfile HEALTHCHECK, so
`docker compose up --wait` waits for the server to serve — and fails a
deploy whose server can't start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 20:47:32 +02:00
..

Deployment

tern.yml deploys the whole Tern stack to tern.mtps.pl as one compose project (deploy/compose in the ygg management repo). The deploy runs automatically at the end of every tern-web release, through the shared ygg/actions/deploy action.

What runs

service image role
db postgres:16-alpine Tern's database, in the db-data volume
server git.app.ygg/mtps/tern-server:<version> the API and sync engine; mail and attachments live in the blob-data volume
web git.app.ygg/ygg/static-server serves the matching tern-web release (tern-web-<version>.tgz)
proxy nginx:1.29-alpine puts the webapp and its API on one origin, published on port 11901
  • Routing: the proxy sends /api (including the /api/ws WebSocket) to the server and everything else to the webapp. The webapp needs both on one origin.
  • Rate limit: sign-in and sign-up are rate-limited per client.
  • Kara's nginx: the site role proxies tern.mtps.pl to port 11901, with websocket.conf included for live updates.
  • Trust store: the server and webapp mount the host's CA bundle, so IMAP/SMTP servers, push servers and git.app.ygg behind the internal CA are trusted.

How a release deploys

  1. Tags go out server-first (see ../docs/release-process.md). tern-server's release pushes tern-server:<version> with ygg/actions/push-image.
  2. tern-web's release publishes tern-web-<version>.tgz. Then its deploy job does three things:
    • checks out this file at the same tag;
    • checks that the server image for that version exists;
    • runs deploy/compose with version=<version>.
  3. On kara, docker compose up --wait replaces the running containers. A deploy only succeeds once every service is healthy: the server is healthy when it answers /healthz, which is after migrations have run. So a server that can't start fails the workflow instead of quietly going down.

The deploy lives in tern-web, not tern-server, because of the single runner. Tags are queued server-first, so by the time tern-web's release runs, both artifacts exist. A deploy job in tern-server would have to wait for tern-web while holding the only runner.

One-time setup

  1. Secrets on kara: /opt/ygg/secrets/env/tern.env. It's copied next to the compose file as .env.

    POSTGRES_PASSWORD=<openssl rand -hex 24>
    CREDENTIAL_KEY=<openssl rand -base64 32>
    # optional: PUSH_URL_REWRITE, PUSH_TLS_INSECURE, PUSH_VAPID_SUBJECT,
    # SESSION_TTL_HOURS, TRASH_RETENTION_DAYS, OPEN_REGISTRATION, RUST_LOG
    

    Back up CREDENTIAL_KEY somewhere other than kara. Without it, the stored IMAP/SMTP passwords and the VAPID key can't be decrypted.

  2. Secrets on the mtps/tern-web repository: DEPLOY_SSH_KEY and DEPLOY_KNOWN_HOSTS, the same values as the ygg org's secrets. The tern repositories belong to mtps, so the org-level secrets don't reach them.

  3. PyYAML on the runner (dnf install python3-pyyaml). The deploy action converts this YAML file to JSON before sending it.

  4. Create your user. Only the first user can register unless OPEN_REGISTRATION=true is set. Sign up once in the Android app (Create a new user on this server) or with curl -X POST https://tern.mtps.pl/api/auth/register -H 'Content-Type: application/json' -d '{"username":"…","password":"…"}'. After that, sign-up is closed.

By hand

The same deploy, run directly on kara, with tern.yml copied to the dist directory:

/opt/ygg/play.sh deploy/compose -e @/opt/ygg/dist/compose/tern.yml -e version=0.9.0

To take it down, add -e compose_state=destroy. That stops the project and removes the nginx site. The db-data and blob-data volumes are kept.

Escaping in tern.yml

The file is processed twice, so some characters need escaping:

  • Ansible templates {{ … }}. That's how {{ version }} gets filled in. Wrap anything else that looks like Jinja in {% raw %}.
  • Docker compose interpolates $. That's how ${POSTGRES_PASSWORD} comes from .env. The proxy's nginx variables are therefore written $$host, $$http_upgrade, and so on.