From f2a9e1762f2b8b254bcfe910030149fbb952f981 Mon Sep 17 00:00:00 2001 From: Mateusz Pstrucha Date: Sun, 4 Oct 2026 12:44:22 +0200 Subject: [PATCH] Deploy tern-web on its own, calling the API on its own origin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The webapp now deploys from its own release as a static-server Swarm stack (deploy/static.yml, tern.mtps.pl on 11902) instead of driving the server's deployment. It's built with VITE_TERN_API — the TERN_API_URL repository variable, defaulting to https://tern-api.mtps.pl — and sends REST calls and the live-update socket there. Empty (development), it stays same-origin behind Vite's proxy. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/release.yml | 40 ++++++++---------------------------- deploy/static.yml | 32 +++++++++++++++++++++++++++++ src/lib/api.ts | 9 +++++++- src/lib/live.ts | 4 ++-- src/vite-env.d.ts | 5 +++++ 5 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 deploy/static.yml diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 76557fc..c632138 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -23,8 +23,12 @@ jobs: - name: Type check run: npm run check + # The API is its own origin when deployed. Overridable with the + # TERN_API_URL repository variable. - name: Build run: npm run build + env: + VITE_TERN_API: ${{ vars.TERN_API_URL || 'https://tern-api.mtps.pl' }} - name: Create release archive run: | @@ -45,39 +49,14 @@ jobs: tern-web-*.tgz tern-web-*.tgz.sha256 - # Deploys the release to tern.mtps.pl (kara, deploy/compose). It lives - # here rather than in tern-server because tags are pushed server-first and - # the single runner works through them in order, so by the time this runs - # both the server image and this webapp release exist. A deploy job in - # tern-server would have to wait for this repo's release while holding - # the only runner. + # Deploys the release just published to tern.mtps.pl (deploy/static on + # the swarm; see deploy/static.yml). deploy: needs: release runs-on: rocky-10 steps: - # The deployment definition is versioned with the server it deploys. - - name: Check out deployment definition + - name: Check out source uses: actions/checkout@v4 - with: - repository: mtps/tern-server - ref: ${{ gitea.ref_name }} - - - name: Check the server image exists - env: - IMAGE: git.app.ygg/mtps/tern-server - TAG: ${{ gitea.ref_name }} - run: | - # Fails fast with a clear message if the server release didn't - # publish this version (failed, or still running on another - # runner), instead of a deploy that can't pull its image. - VERSION="${TAG#v}" - for i in $(seq 1 20); do - docker manifest inspect "$IMAGE:$VERSION" >/dev/null 2>&1 && exit 0 - echo "waiting for $IMAGE:$VERSION ($i/20)" - sleep 30 - done - echo "::error::$IMAGE:$VERSION not found in the registry — did the tern-server release succeed?" - exit 1 - name: Derive version id: version @@ -88,10 +67,9 @@ jobs: - name: Deploy uses: https://git.app.ygg/ygg/actions/deploy@v2 with: - type: compose - dist-path: deploy/tern.yml + type: static + dist-path: deploy/static.yml extra-vars: | version=${{ steps.version.outputs.version }} ssh-key: ${{ secrets.DEPLOY_SSH_KEY }} known-hosts: ${{ secrets.DEPLOY_KNOWN_HOSTS }} - diff --git a/deploy/static.yml b/deploy/static.yml new file mode 100644 index 0000000..6df4ee3 --- /dev/null +++ b/deploy/static.yml @@ -0,0 +1,32 @@ +# tern-web's deployment: a Swarm stack of ygg/static-server serving this +# version's release asset, on tern.mtps.pl (deploy/static in the ygg +# management repo). Run by this repo's release workflow through +# ygg/actions/deploy; `version` arrives as an extra var (the tag without +# its v) and is templated in by Ansible. +# +# The webapp talks to tern-server at tern-api.mtps.pl, baked in at build +# time (VITE_TERN_API in the release workflow). + +static_stack_name: tern-web + +domain: tern.mtps.pl +cert: mtps.pl + +static_service: + name: static + image: git.app.ygg/ygg/static-server:latest + environment: + GITEA_REPOSITORY_URL: https://git.app.ygg/mtps/tern-web + GITEA_RELEASE: "v{{ version }}" + GITEA_ASSET_NAME: "tern-web-{{ version }}.tgz" + SSL_CERT_FILE: /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem + volumes: + - /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem:/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem:ro + ports: + - target: 8080 + published: 11902 + protocol: tcp + mode: ingress + replicas: 2 + restart_policy: + condition: on-failure diff --git a/src/lib/api.ts b/src/lib/api.ts index f04de96..c22f0c2 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -4,6 +4,13 @@ const SESSION_KEY = 'tern-session-token'; +/** + * Where tern-server lives, fixed at build time (VITE_TERN_API). Deployed, + * the webapp and the API are separate origins; in development it's empty + * and Vite's proxy forwards /api on the same origin. + */ +export const API_ORIGIN = (import.meta.env.VITE_TERN_API ?? '').replace(/\/+$/, ''); + export function getSessionToken(): string | null { return localStorage.getItem(SESSION_KEY); } @@ -18,7 +25,7 @@ export function clearSessionToken(): void { async function request(path: string, init?: RequestInit): Promise { const token = getSessionToken(); - const res = await fetch(`/api${path}`, { + const res = await fetch(`${API_ORIGIN}/api${path}`, { ...init, headers: { 'Content-Type': 'application/json', diff --git a/src/lib/live.ts b/src/lib/live.ts index 2a7825b..a67666d 100644 --- a/src/lib/live.ts +++ b/src/lib/live.ts @@ -2,7 +2,7 @@ // polling the same event log while it isn't. Both carry the same cursor, // so switching between them neither drops nor repeats a change. -import { getSessionToken, pollEvents, type LiveEvent } from './api'; +import { API_ORIGIN, getSessionToken, pollEvents, type LiveEvent } from './api'; export type LiveStatus = 'connecting' | 'live' | 'polling'; @@ -55,7 +55,7 @@ export function startLive(handlers: LiveHandlers): () => void { return; } handlers.onStatus(cursor === null ? 'connecting' : 'polling'); - const url = new URL('/api/ws', location.href); + const url = new URL('/api/ws', API_ORIGIN || location.href); url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'; const ws = new WebSocket(url); socket = ws; diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 4078e74..d82bb17 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1,2 +1,7 @@ /// /// + +interface ImportMetaEnv { + /** tern-server's origin, e.g. https://tern-api.mtps.pl. Empty: same origin. */ + readonly VITE_TERN_API?: string; +}