Files
tern-server/deploy
Mateusz PstruchaandClaude Opus 5.5 0f497d88de
CI / test (push) Successful in 3m56s
Deploy tern-server on its own; accept hex keys; fix cross-origin auth
The integrated stack is gone: deploy/compose.yml is just the server and
its Postgres, published on 11901 behind the existing nginx as
tern-api.mtps.pl, and this repo's release deploys it after pushing the
image. The webapp deploys separately from tern-web.

CREDENTIAL_KEY may now be 64 hex digits as well as base64, and a wrong
length says how to generate a right one. The database password reaches
the server as PGPASSWORD rather than inside DATABASE_URL, so it needs
no URL-escaping.

CORS now names Authorization and Content-Type instead of `*`: browsers
don't let the wildcard cover Authorization, so every signed-in request
from the webapp's own origin would have been refused. A route test
checks the preflight.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 12:44:22 +02:00
..

Deployment

compose.yml deploys tern-server to tern-api.mtps.pl. It's one compose project with two services:

  • db: postgres:16-alpine, with its data in the db-data volume.
  • server: git.app.ygg/mtps/tern-server:<version>, with mail and attachments in the blob-data volume.

The server publishes port 11901 on the app host. The existing nginx proxies the domain to it, using the management repo's site role, with websocket.conf included so live updates (/api/ws) pass through. The server mounts the host's CA bundle, so mail and push servers behind the internal CA are trusted.

The webapp deploys separately, from tern-web's own release, to tern.mtps.pl. It calls this API cross-origin. That works because clients authenticate with a bearer token rather than a cookie, and the server allows cross-origin requests.

How a release deploys

tern-server's release workflow has two jobs:

  1. image builds and pushes tern-server:<version> and :latest with ygg/actions/push-image.
  2. deploy sends this file inline through ygg/actions/deploy (type: compose), with version=<version>.

On the app host, docker compose up --wait replaces the containers. The deploy only succeeds once every service is healthy. The image's HEALTHCHECK runs tern-server healthcheck, which passes once the server answers /healthz, after migrations have run. So a server that can't start fails the workflow instead of leaving the API down.

One-time setup

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

    POSTGRES_PASSWORD=<openssl rand -hex 24>
    CREDENTIAL_KEY=<openssl rand -hex 32>
    # optional: PUSH_URL_REWRITE, PUSH_TLS_INSECURE, PUSH_VAPID_SUBJECT,
    # SESSION_TTL_HOURS, TRASH_RETENTION_DAYS, OPEN_REGISTRATION, RUST_LOG
    
    • CREDENTIAL_KEY is 32 bytes, as 64 hex digits or base64.
    • The database password can contain any characters, because it reaches the server as PGPASSWORD, not inside a URL.
    • 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-server repository: DEPLOY_SSH_KEY and DEPLOY_KNOWN_HOSTS. The ygg org's secrets don't reach mtps repositories.

  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-api.mtps.pl/api/auth/register \
      -H 'Content-Type: application/json' -d '{"username":"…","password":"…"}'
    

By hand

The same deploy, run directly on kara with this file copied to the dist directory:

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

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

Escaping in compose.yml

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

  • 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. Write a literal $ as $$.