#!/usr/bin/env bash # Build a multi-arch (linux/amd64, linux/arm64) image and push it to a registry. # # Required: # MESHBOT_IMAGE Full image reference, e.g. registry.example.com/team/meshbot # # Optional: # PLATFORMS Comma-separated arch list (default: linux/amd64,linux/arm64) # EXTRA_TAGS Space-separated additional tags to push (e.g. "stable v0.1.0") # PUSH "1" (default) to push, "0" to build and load locally only # (note: --load only works with a single platform) # BUILDER buildx builder name (default: meshbot-builder, auto-created) set -euo pipefail if [[ -z "${MESHBOT_IMAGE:-}" ]]; then echo "error: MESHBOT_IMAGE is required (e.g. registry.example.com/team/meshbot)" >&2 exit 1 fi PLATFORMS="${PLATFORMS:-linux/amd64,linux/arm64}" PUSH="${PUSH:-1}" BUILDER="${BUILDER:-meshbot-builder}" cd "$(dirname "$0")/.." # Compute git-derived tags. Fall back to "dev" if not in a git tree. if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then GIT_SHA="$(git rev-parse --short=12 HEAD)" if [[ -n "$(git status --porcelain)" ]]; then GIT_SHA="${GIT_SHA}-dirty" fi else GIT_SHA="dev" fi TAGS=(--tag "${MESHBOT_IMAGE}:latest" --tag "${MESHBOT_IMAGE}:${GIT_SHA}") for t in ${EXTRA_TAGS:-}; do TAGS+=(--tag "${MESHBOT_IMAGE}:${t}") done # Make sure a buildx builder exists. Reuse if it's already there. if ! docker buildx inspect "${BUILDER}" >/dev/null 2>&1; then echo ">>> creating buildx builder '${BUILDER}'" docker buildx create --name "${BUILDER}" --driver docker-container --use >/dev/null else docker buildx use "${BUILDER}" >/dev/null fi docker buildx inspect --bootstrap >/dev/null OUTPUT_FLAG=() if [[ "${PUSH}" == "1" ]]; then OUTPUT_FLAG=(--push) echo ">>> building & pushing ${MESHBOT_IMAGE} (${PLATFORMS}) tags: latest, ${GIT_SHA}${EXTRA_TAGS:+, $EXTRA_TAGS}" else # --load only works with a single platform; warn if user requested multi. if [[ "${PLATFORMS}" == *,* ]]; then echo "error: PUSH=0 (local --load) only works with a single platform; got '${PLATFORMS}'" >&2 exit 1 fi OUTPUT_FLAG=(--load) echo ">>> building ${MESHBOT_IMAGE} for ${PLATFORMS} (local load, no push)" fi docker buildx build \ --platform "${PLATFORMS}" \ "${TAGS[@]}" \ "${OUTPUT_FLAG[@]}" \ --file Dockerfile \ . echo ">>> done"