Files
dsantillan affac4a2a0 Add scripts/build-images.sh: build the two images and publish them
The images docker-compose.yml pulls are normally built by
.gitea/workflows/docker-publish.yml, which needs a registered Actions runner
and a PACKAGES_TOKEN secret. Neither exists on this instance's forge yet, so a
host that pulls rather than builds has nothing to pull — and the two builds are
easy to get wrong by hand: api builds from ./api, web from the repository root,
because the web image compiles frontend/ inside its first stage. A wrong context
produces an image that builds fine and then fails at runtime.

One command now does both and pushes them:

    GITEA_TOKEN=<token with write:package> ./scripts/build-images.sh

TAG, REGISTRY, OWNER, PLATFORM, PUSH and API_TARGET (default vs coach) are all
overridable, and the tags default to exactly what docker-compose.yml asks for.
2026-09-21 14:33:30 -04:00

96 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Builds the two container images this project runs on — api and web — and (by default) pushes
# them to the registry `docker-compose.yml` pulls from, so a Dokploy/Portainer/compose deploy can
# pick them up without changing anything.
#
# ./scripts/build-images.sh # build + push :latest
# TAG=v1.4.0 ./scripts/build-images.sh # build + push a release tag
# PUSH=0 ./scripts/build-images.sh # build only (images stay local)
# API_TARGET=coach ./scripts/build-images.sh # the AI-Coach api image instead of the default
#
# Needs: docker with buildx available (any recent Docker), and for a push either a prior
# `docker login` or GITEA_TOKEN set to a token with the `write:package` scope.
#
# GITEA_TOKEN=<token> ./scripts/build-images.sh
#
# Not to be confused with website/build-images.sh, which is unrelated: that one runs ffmpeg over
# the README screenshots.
#
# Why a script at all: the two images have different build contexts (api builds from ./api, web
# from the repository root because it compiles frontend/), and getting a context wrong produces an
# image that builds fine and then fails at runtime. `.gitea/workflows/docker-publish.yml` does the
# same two builds in CI — this is the same thing for the machine that is actually serving the app,
# for an instance whose forge has no Actions runner registered.
set -euo pipefail
REGISTRY="${REGISTRY:-git.dploy.lol}"
OWNER="${OWNER:-dsantillan}"
TAG="${TAG:-latest}"
API_TARGET="${API_TARGET:-default}"
PUSH="${PUSH:-1}"
PLATFORM="${PLATFORM:-}"
IMAGE_API="${REGISTRY}/${OWNER}/kuentagym-api"
IMAGE_WEB="${REGISTRY}/${OWNER}/kuentagym-web"
# Run from the repository root whatever directory the script was called from.
cd "$(dirname "$0")/.."
for tool in docker; do
command -v "$tool" >/dev/null || { echo "✗ $tool is not installed" >&2; exit 1; }
done
[ -f docker-compose.yml ] || { echo "✗ run this from a checkout of the repository" >&2; exit 1; }
echo "✓ docker $(docker version --format '{{.Server.Version}}' 2>/dev/null || echo '(server unreachable)')"
# A push needs credentials. Logging in here rather than assuming saves the confusing
# "denied: requested access to the resource is denied" that a stale login produces.
if [ "$PUSH" = "1" ] && [ -n "${GITEA_TOKEN:-}" ]; then
echo "→ logging in to ${REGISTRY} as ${OWNER}"
printf '%s' "$GITEA_TOKEN" | docker login "$REGISTRY" -u "$OWNER" --password-stdin
fi
# Shared by both builds. --platform is only passed when asked for: on a normal single-arch machine
# letting Docker build for the host is faster and cannot fail on a missing qemu binfmt.
build() {
local name="$1" context="$2" dockerfile="$3" image="$4"; shift 4
echo
echo "→ building ${image}:${TAG} (context: ${context})"
# shellcheck disable=SC2086
docker build \
${PLATFORM:+--platform "$PLATFORM"} \
--file "$dockerfile" \
--tag "${image}:${TAG}" \
"$@" \
"$context"
if [ "$PUSH" = "1" ]; then
echo "→ pushing ${image}:${TAG}"
docker push "${image}:${TAG}"
fi
}
# The api image carries no AI runtime by default — the target every instance has always had. The
# coach target adds the provider SDKs and the sandboxed runtimes, and is a much larger image; see
# the Dockerfile and docs/AI_COACH.md.
build api ./api api/Dockerfile "$IMAGE_API" --target "$API_TARGET"
# web compiles the React app inside the image (stage 1) and serves it from nginx (stage 2), which
# is why its context is the repository root: it needs frontend/ and api/coach/core/.
build web . web/Dockerfile "$IMAGE_WEB"
echo
echo "✓ done — ${IMAGE_API}:${TAG}, ${IMAGE_WEB}:${TAG}"
if [ "$PUSH" = "1" ]; then
echo
echo "These are exactly what docker-compose.yml asks for, so a redeploy on the host that runs it"
echo "pulls them straight away:"
echo " docker compose pull && docker compose up -d"
echo "In Dokploy: redeploy the stack. Keep the ./data bind mount where it is — the profiles,"
echo "workouts and weigh-ins live there."
else
echo
echo "PUSH=0: the images are local only. Either push them later with"
echo " TAG=${TAG} ./scripts/build-images.sh"
echo "or let compose build them in place: docker compose up -d --build"
fi