91 lines
3.1 KiB
Bash
Executable File
91 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Build a multi-arch (linux/amd64, linux/arm64) image and push it to a registry.
|
|
#
|
|
# Required:
|
|
# LORABOT_IMAGE Full image reference, e.g. registry.example.com/team/lorabot
|
|
#
|
|
# 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: lorabot-builder, auto-created)
|
|
|
|
set -euo pipefail
|
|
|
|
if [[ -z "${LORABOT_IMAGE:-}" ]]; then
|
|
echo "error: LORABOT_IMAGE is required (e.g. registry.example.com/team/lorabot)" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Preflight: buildx must be installed (it's a separate package on Arch / some
|
|
# minimal distros). If it's missing, `docker buildx ...` is parsed by docker as
|
|
# `docker ...` with unknown flags, producing a confusing "unknown flag" error.
|
|
if ! docker buildx version >/dev/null 2>&1; then
|
|
echo "error: docker buildx is not available." >&2
|
|
echo " Arch: sudo pacman -S docker-buildx" >&2
|
|
echo " Debian/Ubu: sudo apt install docker-buildx-plugin" >&2
|
|
echo " Other: https://github.com/docker/buildx#installing" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Preflight: daemon reachable.
|
|
if ! docker info >/dev/null 2>&1; then
|
|
echo "error: cannot reach the docker daemon (is it running? are you in the 'docker' group?)" >&2
|
|
echo " Arch: sudo systemctl start docker" >&2
|
|
exit 1
|
|
fi
|
|
|
|
PLATFORMS="${PLATFORMS:-linux/amd64,linux/arm64}"
|
|
PUSH="${PUSH:-1}"
|
|
BUILDER="${BUILDER:-lorabot-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 "${LORABOT_IMAGE}:latest" --tag "${LORABOT_IMAGE}:${GIT_SHA}")
|
|
for t in ${EXTRA_TAGS:-}; do
|
|
TAGS+=(--tag "${LORABOT_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 ${LORABOT_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 ${LORABOT_IMAGE} for ${PLATFORMS} (local load, no push)"
|
|
fi
|
|
|
|
docker buildx build \
|
|
--platform "${PLATFORMS}" \
|
|
"${TAGS[@]}" \
|
|
"${OUTPUT_FLAG[@]}" \
|
|
--file Dockerfile \
|
|
.
|
|
|
|
echo ">>> done"
|