pediatric-ai-scribe-v3/scripts/build-image.sh
Daniel 739286b53b
Some checks failed
Forgejo Android APK / Root app tests (push) Successful in 47s
Forgejo Docker Build / Root app tests (push) Successful in 46s
Forgejo Android APK / Build signed APK (push) Successful in 2m12s
Forgejo Docker Build / Build Docker image (push) Successful in 17s
Forgejo Docker Build / Deploy to the host (push) Failing after 0s
fix: warn when the built image is not the one a deploy will start
`docker compose up` starts whatever PED_AI_IMAGE in .env names, and
build-image.sh does not move that pin. A pin left from an earlier deploy
therefore starts the older image while every signal reports success: the build
completes, `up` says the container started, and /api/health returns {ok:true}
from the wrong revision.

This is not hypothetical. The pin here had been sitting on a revision from two
hours before the Modify card was added, so a rebuild-and-restart rolled My
Resources back 31 commits and removed the feature. The missing card was then
reported as a new bug, and three deploys in this session had in fact deployed
nothing.

build-image.sh now compares the pin to the revision it just built and, when they
differ, prints the pin, says that `up` will start it instead, and gives the
command to move it. It does not correct the pin: naming a revision is also how a
deliberate rollback is done, so this is said rather than silently overridden.

Both deployment docs now check /api/build against `git rev-parse HEAD` after
starting, because /api/health passing only proves a container is up, not that it
is the one you built.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dv6sqaY6Vq3ChZHMem3cnU
2026-09-12 05:58:37 +02:00

69 lines
2.8 KiB
Bash
Executable file

#!/bin/sh
# Build only; this never starts services.
#
# The revision is validated and baked into the image (BUILD_ID + the OCI
# revision label), so the result can always be traced back to a commit and
# /api/build can report it at runtime. Plain `docker compose build` does not set
# GIT_REVISION and produces an image labelled "unknown" — which is why this is
# the supported way to build.
#
# The image is tagged twice: ped-ai-local:<revision> is immutable and is what a
# deploy should name, ped-ai-local:latest is the convenience pointer.
set -eu
cd "$(dirname "$0")/.."
if [ -e .git ]; then
GIT_REVISION=$(env -i PATH="$PATH" HOME="${HOME:-}" git rev-parse --verify 'HEAD^{commit}')
printf '%s\n' "$GIT_REVISION" | grep -Eq '^[0-9a-f]{40}$' || {
echo 'Expected a full lowercase Git SHA' >&2
exit 1
}
else
GIT_REVISION=unknown
echo 'Unversioned development build: revision unknown' >&2
fi
export GIT_REVISION
# A local build always writes the local tag, even when .env pins PED_AI_IMAGE to
# a deployed registry image — otherwise building here would quietly overwrite
# the tag a deploy is pinned to.
PED_AI_IMAGE=ped-ai-local:latest
export PED_AI_IMAGE
docker compose build "$@" pediatric-scribe
if [ "$GIT_REVISION" != unknown ]; then
docker tag ped-ai-local:latest "ped-ai-local:$GIT_REVISION"
echo "Built ped-ai-local:$GIT_REVISION (also tagged :latest)" >&2
else
echo 'Built ped-ai-local:latest with no recorded revision' >&2
fi
# A deploy runs `docker compose up`, which reads PED_AI_IMAGE from .env — not
# the tag just built. A pin left behind from an earlier deploy therefore starts
# that older image, and `up` reports success either way: the build looks done,
# the health check passes, and the running app is silently an older revision.
# That has already cost a session — it rolled the app back far enough to remove
# a feature, and the missing feature was read as a new bug.
#
# Not corrected automatically: the pin is how a deploy names a revision on
# purpose, including a deliberate rollback. Said loudly instead.
if [ "$GIT_REVISION" != unknown ] && [ -f .env ]; then
PINNED=$(sed -n 's/^PED_AI_IMAGE=//p' .env | tail -1)
case "$PINNED" in
'') ;;
*"$GIT_REVISION") ;;
*)
echo >&2
echo "WARNING: .env pins PED_AI_IMAGE=$PINNED" >&2
echo " which is NOT the image just built." >&2
echo " 'docker compose up -d --no-build' will start the pinned image," >&2
echo " not this build. To deploy what was just built:" >&2
echo >&2
echo " sed -i 's|^PED_AI_IMAGE=.*|PED_AI_IMAGE=ped-ai-local:$GIT_REVISION|' .env" >&2
echo >&2
echo " Then confirm after starting it:" >&2
echo " curl -fsS http://127.0.0.1:3552/api/build" >&2
echo >&2
;;
esac
fi