#!/usr/bin/env bash # Build and push the API image (also run by the annotation processor) for a # release.py image release. The image carries the checkout commit and build # time, so /api/ready reports the commit the image was built from. # # Usage: scripts/release-build-api-image.sh [metadata-file] # Prints IMAGE= for `release.py prepare --image`. set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" DOCKER="${DOCKER:-docker}" PLATFORM="${PLATFORM:-linux/amd64}" repository="${1:-}" if [[ -z "$repository" || "$repository" == *@* || "$repository" =~ :[^/]*$ ]]; then echo "usage: $0 [metadata-file] (repository without tag or digest)" >&2 exit 2 fi # The stamp names the commit; uncommitted tracked changes would make it lie. if [[ -n "$(git -C "$ROOT_DIR" status --porcelain --untracked-files=no)" ]]; then echo "tracked files have uncommitted changes; build from a clean release checkout" >&2 exit 1 fi commit="$(git -C "$ROOT_DIR" rev-parse HEAD)" build_time="$(date -u +%Y-%m-%dT%H:%M:%SZ)" metadata="${2:-$(mktemp -t droboticflow-api-build.XXXXXX)}" "$DOCKER" buildx build \ --platform "$PLATFORM" \ --build-arg "GOPROXY=${GOPROXY:-https://proxy.golang.org,direct}" \ --build-arg "DROBOTICFLOW_BUILD_COMMIT=$commit" \ --build-arg "DROBOTICFLOW_BUILD_TIME=$build_time" \ --metadata-file "$metadata" \ --push \ -f "$ROOT_DIR/Dockerfile.api" \ -t "$repository:${commit:0:8}" \ "$ROOT_DIR" digest="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["containerimage.digest"])' "$metadata")" if [[ "$digest" != sha256:* ]]; then echo "build metadata has no image digest: $metadata" >&2 exit 1 fi printf 'COMMIT=%s\nBUILD_TIME=%s\nMETADATA=%s\nIMAGE=%s@%s\n' "$commit" "$build_time" "$metadata" "$repository" "$digest"