droboticflow-new/scripts/build-and-push-images.sh
d-robotics 54a960284c 🏷️ Stamp images with their build commit and time
Dockerfiles bake DROBOTICFLOW_IMAGE_BUILD_COMMIT from a build-arg that
defaults to 'unknown', and the API prefers it over the deployment stamp, so
release images built without the arg reported buildCommit 'unknown'.

Add scripts/release-build-api-image.sh for release.py builds: it refuses
uncommitted tracked changes, passes the checkout commit and UTC build time,
pushes, and prints the digest reference for prepare --image. The general
build-and-push script now passes both args too. Document the step in the
release README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 00:36:20 +08:00

69 lines
2.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DEPLOY_ENV="${DEPLOY_ENV:-}"
if [[ $# -gt 0 && -z "$DEPLOY_ENV" && "${1:-}" != -* ]]; then
DEPLOY_ENV="$1"
shift
fi
export DEPLOY_ENV
# shellcheck source=scripts/load-deploy-env.sh
source "$ROOT_DIR/scripts/load-deploy-env.sh"
DEPLOY_ENV="${DEPLOY_ENV:-}"
REGISTRY_HOST="${REGISTRY_HOST:-}"
REGISTRY_NAMESPACE="${REGISTRY_NAMESPACE:-}"
PLATFORM="${PLATFORM:-linux/amd64}"
PUSH="${PUSH:-true}"
# shellcheck source=scripts/lib/image-config.sh
source "$ROOT_DIR/scripts/lib/image-config.sh"
droboticflow_resolve_image_configuration
# shellcheck source=scripts/lib/deploy-validation.sh
source "$ROOT_DIR/scripts/lib/deploy-validation.sh"
validate_image_reference API_IMAGE "$API_IMAGE"
validate_image_reference WORKER_IMAGE "$WORKER_IMAGE"
validate_image_reference DROSYNC_COORDINATOR_IMAGE "$DROSYNC_COORDINATOR_IMAGE"
validate_image_reference OPERATOR_RUNNER_IMAGE "$OPERATOR_RUNNER_IMAGE"
docker buildx inspect >/dev/null 2>&1 || docker buildx create --use >/dev/null
# Images bake these into DROBOTICFLOW_IMAGE_BUILD_*, which /api/ready prefers
# over the deployment stamp; without them every image reports "unknown".
BUILD_COMMIT="${DROBOTICFLOW_BUILD_COMMIT:-$(git -C "$ROOT_DIR" rev-parse HEAD)}"
BUILD_TIME="${DROBOTICFLOW_BUILD_TIME:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}"
build_image() {
local dockerfile="$1"
local image="$2"
local action_flag="--load"
if [[ "$PUSH" == "true" ]]; then
action_flag="--push"
fi
# GOPROXY 可注入(默认 proxy.golang.org 在部分网络不可达;GOPROXY 环境变量透传为 build-arg)。
docker buildx build \
--platform "$PLATFORM" \
--build-arg "GOPROXY=${GOPROXY:-https://proxy.golang.org,direct}" \
--build-arg "DROBOTICFLOW_BUILD_COMMIT=$BUILD_COMMIT" \
--build-arg "DROBOTICFLOW_BUILD_TIME=$BUILD_TIME" \
"$action_flag" \
-f "$ROOT_DIR/$dockerfile" \
-t "$image" \
"$ROOT_DIR"
}
build_image "Dockerfile.api" "$API_IMAGE"
build_image "Dockerfile.worker" "$WORKER_IMAGE"
build_image "Dockerfile.drosync-coordinator" "$DROSYNC_COORDINATOR_IMAGE"
build_image "Dockerfile.operator-runner" "$OPERATOR_RUNNER_IMAGE"
printf 'API_IMAGE=%s\n' "$API_IMAGE"
printf 'WORKER_IMAGE=%s\n' "$WORKER_IMAGE"
printf 'DROSYNC_COORDINATOR_IMAGE=%s\n' "$DROSYNC_COORDINATOR_IMAGE"
printf 'OPERATOR_RUNNER_IMAGE=%s\n' "$OPERATOR_RUNNER_IMAGE"