The deploy path's api container now keeps itself on the clone's revision
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s

gym.dploy.lol runs deploy/compose-build-at-deploy.yml, and that stack has an asymmetry
that has now cost two deploys: `web` serves the frontend build out of a shared volume, so
every deploy refreshes the app by itself, while `api` copies the clone INTO its container
at start-up. Compose recreates a service only when its configuration or image changed, and
the clone is a volume, so neither does — the api kept running the previous revision while
the frontend moved on.

That is invisible from the outside except as a route that answers 404 although the screen
that calls it is already deployed and visibly new, which is exactly what "Crear clave de
ingesta → not found" was: POST /api/vitals/key hitting last week's server.

The api's command is now a small loop instead of a one-liner: it syncs /app from the
clone whenever the clone's revision changes, restarts the server on it, and starts it
again if it ever exits (the old one-liner left that to the container restart policy, which
also meant a full re-copy every time). Dependencies are reinstalled only when
package.json itself changed, so a deploy costs a couple of seconds of downtime rather
than a minute of npm install.

It is careful about the window a deploy opens: `source` does `rm -rf /work/repo` before it
re-clones, and the loop tests for api/server.js — a file a fresh clone writes and that
rm removes — so a deploy caught mid-clone is waited out instead of half-copied.

Two things this file has to get right, both verified rather than assumed. Every `$` in the
container's script is written `$$`, because compose interpolates `$VAR` in block scalars
too and an unescaped `$src` would have arrived empty (this is the same reason the web
service writes `$$host`). And the loop's control flow was simulated before pushing: first
start, a new clone, a package.json change, a server that dies, and a clone that disappears
mid-deploy and comes back — five paths, all as intended.
This commit is contained in:
dsantillan committed 2026-09-23 10:44:49 -04:00
1 parent c150101232
commit 5eb0a5298a
1 file changed
+46 -1
+46 -1
View File
@@ -111,7 +111,52 @@ services:
command:
- /bin/sh
- -c
- "rm -rf /app/* && cp -a /source/repo/api/. /app/ && npm install --omit=dev && exec node server.js"
- |
# Keep this container on the revision the clone holds.
#
# `web` needs nothing like this: it serves the frontend build out of a shared volume, so
# every deploy refreshes the app by itself. The api's code is copied INTO this container
# instead, and compose recreates a service only when its configuration or image changed —
# the clone is a volume, so neither does. Without the loop below the api silently keeps
# serving the previous revision after every deploy, which from the outside looks exactly
# like a route that does not exist yet (404 on an endpoint the frontend already calls).
#
# It is also a supervisor: if the server exits, the loop starts it again, where the old
# one-liner left that to the container restart policy (and a full re-copy with it).
src=/source/repo/api
pid=''
last=''
sync_code() {
# Everything except node_modules: the copy IS the code, while wiping the installed
# dependencies would mean a full `npm install` — a minute of downtime — on every deploy.
for f in /app/*; do
case "$$f" in */node_modules) ;; *) rm -rf "$$f" ;; esac
done
cp -a "$$src/." /app/
}
start_api() { cd /app; node server.js & pid=$$!; }
trap 'kill "$$pid" 2>/dev/null; exit 0' TERM INT
while :; do
# server.js is written last by a fresh clone and removed first by `rm -rf /work/repo`, so
# this one test covers both "the clone is not ready yet" and "a deploy is in flight".
if [ -f "$$src/server.js" ]; then
stamp=$$(stat -c %Y "$$src/server.js" 2>/dev/null)
if [ "$$stamp" != "$$last" ]; then
last="$$stamp"
if [ -n "$$pid" ]; then kill "$$pid" 2>/dev/null; wait "$$pid" 2>/dev/null; fi
reinstall=''
if ! cmp -s "$$src/package.json" /app/package.json; then reinstall=1; fi
sync_code
if [ -n "$$reinstall" ]; then npm install --omit=dev; fi
echo "api: synced to the clone at $$(date -u +%H:%M:%SZ)"
start_api
elif [ -n "$$pid" ] && ! kill -0 "$$pid" 2>/dev/null; then
echo 'api: the server exited — starting it again'
start_api
fi
fi
sleep 15
done
restart: unless-stopped
web-build: