From 6fd81aae15608342b5cbcd5a33643da571ec0a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 15 Sep 2026 19:38:44 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=A7=B9=20one=20source=20for?= =?UTF-8?q?=20the=20deployment=20defaults=20-=20the=20defaults=20(PORT,=20?= =?UTF-8?q?HOST,=20BACKEND=5FORIGIN)=20are=20written=20once,=20in=20the=20?= =?UTF-8?q?image=20ENV=20=20=20block=20in=20Containerfile;=20server.ts=20n?= =?UTF-8?q?ow=20refuses=20to=20boot=20without=20them=20instead=20=20=20of?= =?UTF-8?q?=20carrying=20its=20own=20copies,=20so=20a=20stripped=20image?= =?UTF-8?q?=20fails=20loudly=20-=20compose=20healthcheck=20asks=20the=20co?= =?UTF-8?q?ntainer=20for=20PORT;=20the=20deploy=20gate=20and=20the=20=20?= =?UTF-8?q?=20verify=20step=20read=20the=20running=20container=20for=20the?= =?UTF-8?q?=20port=20instead=20of=20parsing=20a=20=20=20second=20copy=20ou?= =?UTF-8?q?t=20of=20the=20operator=20file=20-=20drop=20deploy/Containerfil?= =?UTF-8?q?e.runtime:=20CI=20now=20builds=20the=20repo=20Containerfile,=20?= =?UTF-8?q?so=20=20=20there=20is=20no=20CI-only=20twin=20whose=20base=20im?= =?UTF-8?q?age=20or=20ENV=20can=20drift=20-=20docs=20follow:=20knobs=20par?= =?UTF-8?q?agraph,=20env=20example=20and=20unit=20header=20describe=20over?= =?UTF-8?q?rides=20=20=20of=20the=20image=20defaults=20rather=20than=20fal?= =?UTF-8?q?lbacks=20scattered=20across=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 65 ++++++++++++------------ Containerfile | 12 ++++- README.md | 4 +- compose.yaml | 31 +++++------ deploy/Containerfile.runtime | 21 -------- deploy/hezarfen_frontend.env.example | 12 +++-- deploy/hezarfen_frontend_compose.service | 7 +-- server.ts | 30 +++++++---- 8 files changed, 88 insertions(+), 94 deletions(-) delete mode 100644 deploy/Containerfile.runtime diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b9ce553..6e3360c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,9 +2,10 @@ # # Typecheck — tsc --noEmit on its own runner. No browser, no build. # Build and test — the unit suite (vitest: node + jsdom projects), the -# production build, then packs dist/ + server.ts into the -# runtime image (deploy/Containerfile.runtime) and uploads -# the image tarball + compose files as the run's artifact. +# production build, then the repo Containerfile's image +# (its ENV block is the single source of the deployment +# defaults) and uploads the image tarball + compose files +# as the run's artifact. # Deploy — SSH: podman load the image, point the deploy-owned # stack.env's HEZARFEN_TAG at it, install compose.yaml + # the hezarfen_frontend_compose user unit, restart the @@ -141,34 +142,23 @@ jobs: test -f dist/index.html du -sh dist - - name: Stage the runtime context + # One Containerfile for laptop and CI, so there is no CI-only twin to + # drift: whatever its ENV block says is what ships. + - name: Build the image run: | set -euo pipefail - mkdir -p stage - # dist/ and server.ts are exactly what deploy/Containerfile.runtime - # COPYs; nothing else from the repo goes into the shipped image. - cp -r dist stage/dist - cp server.ts stage/server.ts - ls -l stage + docker build -f Containerfile -t localhost/hezarfen_frontend:$GITHUB_SHA . - - name: Build the runtime image + - name: Stage the release bundle run: | set -euo pipefail - # Pack the already-built SPA into the slim runtime image. Never - # build from source here: the bun/vite build ran above (same - # division of labour as the backend's deploy). - docker build -f deploy/Containerfile.runtime -t localhost/hezarfen_frontend:$GITHUB_SHA stage/ + mkdir -p stage docker save localhost/hezarfen_frontend:$GITHUB_SHA | gzip > stage/hezarfen_frontend.tar.gz - - - name: Stage the compose bundle - run: | - set -euo pipefail cp compose.yaml deploy/hezarfen_frontend_compose.service stage/ echo -n "$GITHUB_SHA" > stage/tag # Everything the deploy installs on the server must be here; a # missing piece fails here, on the runner, not mid-deploy on the - # VPS. (stage/ also still holds dist/ and server.ts — docker build - # context, not deploy payload.) + # VPS. test -f stage/hezarfen_frontend.tar.gz test -f stage/compose.yaml test -f stage/hezarfen_frontend_compose.service @@ -331,13 +321,9 @@ jobs: } done - # PORT is optional there (compose falls back to 5173), but the health - # gate below has to probe the port the stack actually published, so - # read the same value compose will — same file, no second place to - # keep in sync. Plain KEY=value, as deploy/hezarfen_frontend.env.example - # documents. - port=$(sed -n 's/^PORT=\(.*\)$/\1/p' "$env_file" | tail -1) - port=${port:-5173} + # The probe port is not repeated here: ask the container that gets + # (re)created below what it carries (image ENV, overridden by the + # operator's file), instead of parsing a second copy of the number. # Rootless podman plus a compose provider; fail with the fix, not # a bare command-not-found three steps in. @@ -408,6 +394,17 @@ jobs: systemctl --user enable -- "$service" systemctl --user restart "$service" || true + # Ask the container that was just (re)created which port to probe + # (image ENV, overridden by the operator's file) — the number lives in + # one place and this reads it, rather than keeping a second copy that + # can drift. No container reachable = nothing came up. + port=$(podman inspect -f '{{range .Config.Env}}{{println .}}{{end}}' hezarfen_frontend 2>/dev/null | sed -n 's/^PORT=//p') || true + if [ -z "$port" ]; then + echo 'the frontend container reports no PORT; rolling back' >&2 + rollback + exit 1 + fi + # The oneshot returns before the container has bound its port. Wait # for the SPA shell rather than a clock: a 200 whose body carries # the index.html title means the new build is really being served. @@ -455,11 +452,13 @@ jobs: deploy_path=$HOME/$1 export XDG_RUNTIME_DIR=/run/user/$(id -u) cd -- "$deploy_path" - # Same derivation as the deploy step: the gate must probe the port - # the operator's file published, not a remembered default. - env_file=$deploy_path/hezarfen_frontend.env - port=$(sed -n 's/^PORT=\(.*\)$/\1/p' "$env_file" | tail -1) - port=${port:-5173} + # Same source as the deploy step: the container's own PORT, not a + # second copy of the number in this script. + port=$(podman inspect -f '{{range .Config.Env}}{{println .}}{{end}}' hezarfen_frontend | sed -n 's/^PORT=//p') + if [ -z "$port" ]; then + echo 'the frontend container reports no PORT' >&2 + exit 1 + fi tag=$(cat -- "$deploy_path/current_tag") image=$(podman inspect -f '{{.Config.Image}}' hezarfen_frontend) case "$image" in diff --git a/Containerfile b/Containerfile index 0a3ea2f..ab7492f 100644 --- a/Containerfile +++ b/Containerfile @@ -17,8 +17,16 @@ WORKDIR /app COPY --from=build /app/dist ./dist COPY server.ts ./server.ts -ENV PORT=5173 -EXPOSE 5173 +# THE deployment defaults, and the only place they are written. server.ts reads +# all three from the environment and refuses to boot without them, compose's +# healthcheck asks the container for PORT instead of repeating a number, and the +# deploy pipeline reads the port back out of the running container. Override any +# of them in hezarfen_frontend.env — env_file wins over these image values. +# Nothing is EXPOSEd: the service runs with host networking, so it binds the +# host's interface directly and no port mapping exists. +ENV PORT=5173 \ + HOST=0.0.0.0 \ + BACKEND_ORIGIN=http://127.0.0.1:7656 USER bun # Healthcheck lives in compose.yaml (podman's OCI image format ignores a diff --git a/README.md b/README.md index c849ab2..8b4fd44 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ podman compose down # stop The `Containerfile` is a two-stage build: `bun install --frozen-lockfile` plus `bun run build` produce `dist/`, and `oven/bun:1-slim` serves it with `server.ts` — which also reverse-proxies `/api/*` (plain HTTP and the exam-room WebSocket) to `BACKEND_ORIGIN`. No nginx, no node_modules at runtime, no state: one container and no volumes. It runs with `network_mode: host`, so it shares the host's loopback and reaches a backend published on `127.0.0.1:7656` exactly as a host process would (a bridge network would resolve `127.0.0.1` to the container itself). Host networking means no port mapping: the app binds the host's interface directly, so set `HOST=127.0.0.1` and let the reverse proxy on the same loopback be the only door. -**Knobs live in two places and are never mixed.** On a laptop nothing is required: with no env file the app falls back to its own defaults (`BACKEND_ORIGIN=http://127.0.0.1:7656`, `HOST=0.0.0.0`, `PORT=5173`), so the `up` above works with zero extra files. On a server the operator copies `deploy/hezarfen_frontend.env.example` to `$HOME/hezarfen_frontend/hezarfen_frontend.env`, `chmod 0600` it and edits it — nothing automated creates, overwrites or uploads that file. The service declares it as `env_file:`, so **every key it carries reaches the container with no extra flag**; nothing else is interpolated from it, and the only compose-level variable is `HEZARFEN_TAG`, which the deploy owns in `stack.env`. +**Knobs live in two places and are never mixed.** The defaults are written once, in the image's `ENV` block (`Containerfile`) — `BACKEND_ORIGIN`, `HOST`, `PORT` — and nothing else in the repo repeats them: `server.ts` reads all three and refuses to boot without them, the compose healthcheck asks the container for `PORT`, and the deploy reads the port back out of the running container. A laptop needs no extra files. On a server the operator copies `deploy/hezarfen_frontend.env.example` to `$HOME/hezarfen_frontend/hezarfen_frontend.env`, `chmod 0600` it and edits it — nothing automated creates, overwrites or uploads that file. The service declares it as `env_file:`, so **every key it carries overrides the image default with no extra flag**; nothing is interpolated from it, and the only compose-level variable is `HEZARFEN_TAG`, which the deploy owns in `stack.env`. Environment values are read when the container **starts**: after editing the file, recreate (`podman compose up -d`, or `systemctl --user restart hezarfen_frontend_compose`). `podman restart` keeps the old environment. @@ -78,7 +78,7 @@ podman compose --env-file ~/hezarfen_frontend/hezarfen_frontend.env \ `--env-file` feeds both compose's interpolation and — through `env_file:` — the container; the second file is compose-owned and carries `HEZARFEN_TAG` (a later `--env-file` wins for duplicate keys). Because the unit passes `--no-build`, the image must already exist on the host (`podman compose build`, or `podman load` of a tarball shipped from elsewhere). After an env change: `systemctl --user restart hezarfen_frontend_compose`. -Deploying is GitHub Actions' job, not the server's. `.github/workflows/main.yml` typechecks (`tsc --noEmit`, its own runner, parallel with the rest), runs the vitest suite, builds the SPA, packs `dist/` + `server.ts` into the runtime image (`deploy/Containerfile.runtime`), and ships the image tarball together with `compose.yaml`, the `hezarfen_frontend_compose.service` unit and a `tag` file as the run's artifact. The deploy job loads that image on the server and starts the stack with `podman compose up -d --no-build`; the tag travels in a deploy-owned `stack.env`, so a release never rewrites the operator's knobs file. A health gate protects the swap: the new container must serve the SPA shell and must be *this* build's image, otherwise the deploy rolls back to `previous_tag` (and on a first deploy stops the stack instead). The operator's only manual step is writing the env file once (above). +Deploying is GitHub Actions' job, not the server's. `.github/workflows/main.yml` typechecks (`tsc --noEmit`, its own runner, parallel with the rest), runs the vitest suite, builds the SPA, builds the repo `Containerfile`'s image (the same one a laptop builds — its `ENV` block is the single source of the deployment defaults), and ships the image tarball together with `compose.yaml`, the `hezarfen_frontend_compose.service` unit and a `tag` file as the run's artifact. The deploy job loads that image on the server and starts the stack with `podman compose up -d --no-build`; the tag travels in a deploy-owned `stack.env`, so a release never rewrites the operator's knobs file. A health gate protects the swap: the new container must serve the SPA shell and must be *this* build's image, otherwise the deploy rolls back to `previous_tag` (and on a first deploy stops the stack instead). The operator's only manual step is writing the env file once (above). It needs three repository secrets — `SSH_PRIVATE_KEY`, `SSH_HOST`, `SSH_USER`, the same three names the backend deploy uses — plus `loginctl enable-linger` for the deploy user. `workflow_dispatch` (Actions → Run workflow, or `gh workflow run main.yml --ref main`) redeploys the newest green build without re-running the suite, which is the door to use when only the server side changed. diff --git a/compose.yaml b/compose.yaml index 5316b93..52616f0 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,23 +1,17 @@ # The frontend: one container behind podman compose — Bun serves the built SPA # and reverse-proxies /api/* to the backend. # -# Knobs live in two places and are never mixed (see README): -# laptop the ${VAR:-default} interpolations below — `podman compose up` -# works with zero extra files -# server ~/hezarfen_frontend/hezarfen_frontend.env (0600, written by hand -# from deploy/hezarfen_frontend.env.example) +# Every default (BACKEND_ORIGIN, HOST, PORT) is written once, in the image's +# ENV block (Containerfile); this file declares no `environment:` at all, so +# nothing here can shadow a value. A laptop runs it as-is; a server overlays +# ~/hezarfen_frontend/hezarfen_frontend.env (0600, written by hand from +# deploy/hezarfen_frontend.env.example) through `env_file:`. # -# Interpolation reads the --env-file arguments (hezarfen_frontend.env first, -# deploy-owned stack.env second); the service's `env_file:` passes that same -# file into the container. BACKEND_ORIGIN is deliberately NOT listed under -# `environment:` — a listed key wins over `env_file:`, which would shadow the -# file with this file's default whenever compose runs without --env-file. -# server.ts keeps the same default built in, so nothing is lost on a laptop and -# the operator's file is authoritative on a server. PORT stays listed: the -# publish above is interpolated from it, and one source has to move the -# listener and the publish together. +# `--env-file` only matters for `HEZARFEN_TAG` (the deploy passes the +# deploy-owned stack.env second, so it wins for that one key); the container's +# knobs arrive through `env_file:`, which needs no flag. # -# The value is read when the container STARTS: editing the file needs a +# Those values are read when the container STARTS: editing the file needs a # recreate (`podman compose up -d`, or `systemctl --user restart # hezarfen_frontend_compose`), not a `podman restart`. @@ -47,9 +41,10 @@ services: required: false restart: unless-stopped healthcheck: - # Bun-based: the slim runtime image ships no curl. Reads the container's - # own PORT, so a moved port does not silently wedge the healthcheck. - test: ["CMD", "bun", "-e", "fetch('http://127.0.0.1:'+(Bun.env.PORT??5173)+'/').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"] + # Bun-based: the slim runtime image ships no curl. PORT comes from the + # container's own environment — the image ENV block is where the number + # lives, so this probe holds no second copy of it. + test: ["CMD", "bun", "-e", "fetch('http://127.0.0.1:'+Bun.env.PORT+'/').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"] interval: 30s timeout: 5s retries: 3 diff --git a/deploy/Containerfile.runtime b/deploy/Containerfile.runtime deleted file mode 100644 index 7b52690..0000000 --- a/deploy/Containerfile.runtime +++ /dev/null @@ -1,21 +0,0 @@ -# CI-only runtime image: the build CONTEXT is `stage/`, which holds the -# already-built `dist/` and `server.ts` — never the repo (see -# .github/workflows/main.yml). The laptop path is the repo-root Containerfile, -# which installs dependencies and runs `vite build`; this one only packs that -# build's output, so the shipped image carries no source, no node_modules and -# no toolchain. -# -# Keep the runtime stage in sync with the repo Containerfile's second stage. - -FROM docker.io/oven/bun:1-slim -WORKDIR /app -COPY dist ./dist -COPY server.ts ./server.ts - -ENV PORT=5173 -EXPOSE 5173 -USER bun - -# Healthcheck lives in compose.yaml (podman's OCI image format ignores a -# Containerfile HEALTHCHECK). -CMD ["bun", "run", "server.ts"] diff --git a/deploy/hezarfen_frontend.env.example b/deploy/hezarfen_frontend.env.example index 35344a2..ef5ee64 100644 --- a/deploy/hezarfen_frontend.env.example +++ b/deploy/hezarfen_frontend.env.example @@ -11,11 +11,13 @@ # into the frontend container). Plain KEY=value lines: no quotes, no `export`, # no trailing comment. # -# The service declares this file as `env_file:`, so every key below reaches the -# container with no extra flag and nothing is shadowed by a compose default. -# Values are read when the container STARTS: after editing, recreate -# (`podman compose up -d`, or `systemctl --user restart -# hezarfen_frontend_compose`). `podman restart` keeps the old environment. +# The service declares this file as `env_file:`, so every key below overrides +# the image's own ENV defaults (see the ENV block in Containerfile — that block +# is the only place the default values are written) with no extra flag, and +# nothing is shadowed by a compose default. Values are read when the container +# STARTS: after editing, recreate (`podman compose up -d`, or +# `systemctl --user restart hezarfen_frontend_compose`). `podman restart` keeps +# the old environment. # # The app reads three keys today (server.ts): BACKEND_ORIGIN, PORT, HOST. diff --git a/deploy/hezarfen_frontend_compose.service b/deploy/hezarfen_frontend_compose.service index 0cf8ec8..62571b4 100644 --- a/deploy/hezarfen_frontend_compose.service +++ b/deploy/hezarfen_frontend_compose.service @@ -11,14 +11,15 @@ # podman load -i image.tar # or load one shipped from elsewhere # # Keys read from hezarfen_frontend.env (full docs in -# deploy/hezarfen_frontend.env.example): +# deploy/hezarfen_frontend.env.example). All three have defaults in the image's +# ENV block (Containerfile) — this file overrides them: # BACKEND_ORIGIN the origin /api/* is proxied to; on this # host that is http://127.0.0.1:7656, which # the service can reach because compose runs # it with host networking -# HOST bind address; 127.0.0.1 keeps the app on +# HOST bind address; 127.0.0.1 keeps the app on # loopback behind the reverse proxy -# PORT listen port, default 5173 +# PORT listen port # # Env-only restart (re-runs `compose up -d`, recreating the container when its # environment changed): diff --git a/server.ts b/server.ts index 3e3f277..baf293a 100644 --- a/server.ts +++ b/server.ts @@ -6,16 +6,26 @@ // Local dev uses Vite's own proxy (vite.config.ts); this file is only the // container's runtime after `vite build`. -const PORT = Number(Bun.env.PORT ?? 5173); -// Interface to bind. 0.0.0.0 is what a published container port needs; under -// host networking the process sits directly on the host, so a server sets -// HOST=127.0.0.1 and only the reverse proxy beside it can reach the app. -const HOST = Bun.env.HOST ?? "0.0.0.0"; -// Where the backend lives. The default is the co-located backend a server -// publishes on its own loopback (the compose stack maps 127.0.0.1:7656), which -// is what the frontend container reaches under host networking. Override with -// BACKEND_ORIGIN for anything else. -const BACKEND_ORIGIN = Bun.env.BACKEND_ORIGIN ?? "http://127.0.0.1:7656"; +// Every knob comes from the environment; the values themselves live in exactly +// one place — the image's ENV block (see Containerfile), overridden per +// deployment by hezarfen_frontend.env. Boot refuses rather than guessing: a +// missing value here means the image was stripped or the process was started +// outside it, and silently binding :5000-style defaults hides that. +function required(name: string): string { + const value = Bun.env[name]; + if (!value) { + console.error( + `${name} is not set. The image supplies the defaults (see the ENV block in Containerfile); ` + + `set ${name} in hezarfen_frontend.env to override them.`, + ); + process.exit(1); + } + return value; +} + +const PORT = Number(required("PORT")); +const HOST = required("HOST"); +const BACKEND_ORIGIN = required("BACKEND_ORIGIN"); const BACKEND_HTTP = BACKEND_ORIGIN.replace(/\/+$/, ""); const BACKEND_WS = BACKEND_HTTP.replace(/^http/, "ws"); const BACKEND_HOST = new URL(BACKEND_HTTP).host;