61 lines
2.7 KiB
Bash
Executable File
61 lines
2.7 KiB
Bash
Executable File
#!/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 <registry/repository> [metadata-file]
|
|
# Prints IMAGE=<registry/repository@sha256:...> 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 <registry/repository> [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)}"
|
|
|
|
runtime_image="${API_RUNTIME_IMAGE:-$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["image"])' "$ROOT_DIR/docs/release/api-runtime.json")}"
|
|
if [[ ! "$runtime_image" =~ ^[A-Za-z0-9][A-Za-z0-9._:/-]*@sha256:[0-9a-f]{64}$ ]]; then
|
|
echo "API_RUNTIME_IMAGE must be an immutable prepared runtime digest" >&2
|
|
exit 2
|
|
fi
|
|
|
|
"$DOCKER" buildx build \
|
|
--platform "$PLATFORM" \
|
|
--build-arg "API_RUNTIME_IMAGE=$runtime_image" \
|
|
--build-arg "API_RUNTIME_PREPARED=1" \
|
|
--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"
|
|
|
|
python3 - "$metadata.provenance.json" "$ROOT_DIR/Dockerfile.api" "$commit" "$runtime_image" "$digest" "$build_time" <<'PYPROOF'
|
|
import hashlib,json,pathlib,sys
|
|
output,recipe,commit,runtime,digest,at=sys.argv[1:]
|
|
pathlib.Path(output).write_text(json.dumps({"schemaVersion":1,"commit":commit,"runtimeImage":runtime,"imageDigest":digest,"buildTime":at,"recipeSHA256":hashlib.sha256(pathlib.Path(recipe).read_bytes()).hexdigest()},indent=2)+"\n")
|
|
PYPROOF
|