#!/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: 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 # Building is not deploying. `docker compose up` takes its image from # PED_AI_IMAGE in .env, which this script does not move, so a pin left behind by # an earlier deploy starts that 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. # # scripts/deploy.sh is the answer to this and has been since it landed: it moves # the pin, waits for health, then asks /api/build which revision is actually # serving and rolls back if it is not the one requested. Running `up` by hand # skips all three. This points at it rather than repeating half of it. 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 "NOTE: .env still pins PED_AI_IMAGE=$PINNED" >&2 echo " so 'docker compose up' would start that image, not this build." >&2 echo >&2 echo " Deploy this build with:" >&2 echo " scripts/deploy.sh ped-ai-local:$GIT_REVISION $GIT_REVISION" >&2 echo >&2 echo " which moves the pin and then verifies /api/build, rolling back" >&2 echo " if the container comes up on a different revision." >&2 echo >&2 ;; esac fi