All checks were successful
Test build-and-push (self-test) / Build tiny multi-arch image (push) Successful in 9s
199 lines
7.5 KiB
YAML
199 lines
7.5 KiB
YAML
name: Build and Push Multi-Arch Image
|
|
description: Log in to a container registry, set up a remote Docker Buildx builder over SSH, and build + push a multi-arch image.
|
|
|
|
inputs:
|
|
registry_host:
|
|
description: Hostname of the container registry
|
|
required: true
|
|
default: git.ivanch.me
|
|
registry_username:
|
|
description: Username for the container registry
|
|
required: true
|
|
default: ivanch
|
|
registry_password:
|
|
description: Password / token for the container registry (pass via secrets)
|
|
required: true
|
|
image:
|
|
description: Full image reference (host/path/name) without tag
|
|
required: true
|
|
image_tag:
|
|
description: Tag(s) to apply to the built image. Comma-separated for multiple tags (e.g. "latest,abc1234").
|
|
required: false
|
|
default: latest
|
|
platforms:
|
|
description: Comma-separated list of target platforms
|
|
required: false
|
|
default: linux/amd64,linux/arm64
|
|
build_context:
|
|
description: Docker build context path
|
|
required: false
|
|
default: .
|
|
build_dockerfile:
|
|
description: Path to the Dockerfile (relative to context)
|
|
required: false
|
|
default: Dockerfile
|
|
build_args:
|
|
description: >-
|
|
Optional build arguments, one per line in KEY=VALUE form. Each line is
|
|
appended as a `--build-arg KEY=VALUE` to the build command.
|
|
required: false
|
|
default: ""
|
|
ssh_key:
|
|
description: SSH private key for the remote Docker builder host (pass via secrets)
|
|
required: true
|
|
docker_host:
|
|
description: Hostname of the remote Docker Buildx builder
|
|
required: false
|
|
default: docker-build.haven
|
|
docker_user:
|
|
description: SSH user for the remote Docker builder
|
|
required: false
|
|
default: root
|
|
|
|
runs:
|
|
using: composite
|
|
steps:
|
|
- name: Log in to Container Registry
|
|
shell: bash
|
|
env:
|
|
REGISTRY_HOST: ${{ inputs.registry_host }}
|
|
REGISTRY_USERNAME: ${{ inputs.registry_username }}
|
|
REGISTRY_PASSWORD: ${{ inputs.registry_password }}
|
|
run: |
|
|
echo "${REGISTRY_PASSWORD}" \
|
|
| docker login "${REGISTRY_HOST}" \
|
|
-u "${REGISTRY_USERNAME}" \
|
|
--password-stdin
|
|
|
|
- name: Set up SSH key for remote builder
|
|
shell: bash
|
|
env:
|
|
SSH_KEY: ${{ inputs.ssh_key }}
|
|
BUILDER_HOST: ${{ inputs.docker_host }}
|
|
BUILDER_USER: ${{ inputs.docker_user }}
|
|
run: |
|
|
# Defensive: strip any stray CR (CRLF checkout via core.autocrlf can
|
|
# turn "root" into "root\r", which ssh rejects with
|
|
# "remote username contains invalid characters").
|
|
SSH_KEY=$(printf '%s' "${SSH_KEY}" | tr -d '\r')
|
|
BUILDER_HOST=$(printf '%s' "${BUILDER_HOST}" | tr -d '\r')
|
|
BUILDER_USER=$(printf '%s' "${BUILDER_USER}" | tr -d '\r')
|
|
|
|
mkdir -p ~/.ssh
|
|
echo "${SSH_KEY}" > ~/.ssh/id_ed25519
|
|
chmod 600 ~/.ssh/id_ed25519
|
|
ssh-keyscan "${BUILDER_HOST}" >> ~/.ssh/known_hosts
|
|
|
|
# Fail fast with a real diagnostic instead of a slow buildx
|
|
# "context deadline exceeded" if the runner can't reach / auth to the
|
|
# remote docker daemon over SSH. A hard `timeout` wrapper guarantees we
|
|
# never hang: ConnectTimeout only bounds the TCP handshake.
|
|
echo "== probing ssh://***@${BUILDER_HOST} =="
|
|
if timeout 25 ssh \
|
|
-o BatchMode=yes \
|
|
-o ConnectTimeout=10 \
|
|
-o ServerAliveInterval=5 \
|
|
-o ServerAliveCountMax=2 \
|
|
-o StrictHostKeyChecking=accept-new \
|
|
-i ~/.ssh/id_ed25519 \
|
|
"${BUILDER_USER}@${BUILDER_HOST}" \
|
|
'docker version --format "server={{.Server.Version}}"'; then
|
|
echo "SSH probe OK"
|
|
else
|
|
rc=$?
|
|
echo "SSH probe FAILED (exit ${rc})"
|
|
if [ "${rc}" = "124" ]; then
|
|
echo "-> timed out: TCP opens but SSH/daemon does not answer."
|
|
echo "-> Check: dockerd running on ${BUILDER_HOST}? firewall path? MTU?"
|
|
fi
|
|
exit "${rc}"
|
|
fi
|
|
|
|
- name: Set up remote Docker context and buildx
|
|
shell: bash
|
|
env:
|
|
BUILDER_HOST: ${{ inputs.docker_host }}
|
|
BUILDER_USER: ${{ inputs.docker_user }}
|
|
PLATFORMS: ${{ inputs.platforms }}
|
|
run: |
|
|
# NOTE on approach: we drive the *remote daemon's own default buildx
|
|
# builder* through a docker context over SSH. We deliberately do NOT use
|
|
# `docker buildx create --name X ssh://...` (docker-container driver over
|
|
# SSH): that path hangs on "waiting for connection: context deadline
|
|
# exceeded" against this builder host, whereas a plain docker context +
|
|
# the daemon's built-in buildkit (containerd-snapshotter) works and
|
|
# supports multi-arch. Also never set DOCKER_HOST (it overrides the
|
|
# active context).
|
|
unset DOCKER_HOST || true
|
|
|
|
docker context rm -f remote-builder 2>/dev/null || true
|
|
docker context create remote-builder \
|
|
--docker "host=ssh://${BUILDER_USER}@${BUILDER_HOST}"
|
|
|
|
# Ensure qemu/binfmt is registered on the remote for cross-arch builds.
|
|
# This registration lives in the kernel and is lost on daemon/host
|
|
# restart, so (re)install it every run. Cheap and idempotent.
|
|
if echo "${PLATFORMS}" | grep -q 'arm'; then
|
|
echo "== ensuring qemu/binfmt on remote for cross-arch =="
|
|
docker --context remote-builder run --privileged --rm \
|
|
tonistiigi/binfmt --install all >/dev/null 2>&1 || \
|
|
echo "WARN: binfmt install returned non-zero (may already be present)"
|
|
fi
|
|
|
|
# Sanity-check the remote builder answers (fast, no container spawn).
|
|
docker --context remote-builder buildx inspect >/dev/null
|
|
|
|
- name: Generate tags list
|
|
id: gentags
|
|
shell: bash
|
|
env:
|
|
IMAGE: ${{ inputs.image }}
|
|
IMAGE_TAGS: ${{ inputs.image_tag }}
|
|
run: |
|
|
TAGS_ARGS=""
|
|
IFS=',' read -ra ADDR <<< "${IMAGE_TAGS}"
|
|
for tag in "${ADDR[@]}"; do
|
|
trimmed_tag=$(echo "${tag}" | xargs)
|
|
if [ -n "${trimmed_tag}" ]; then
|
|
TAGS_ARGS="${TAGS_ARGS} -t ${IMAGE}:${trimmed_tag}"
|
|
fi
|
|
done
|
|
echo "tag_args=${TAGS_ARGS}" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Generate build args
|
|
id: genbuildargs
|
|
shell: bash
|
|
env:
|
|
BUILD_ARGS_INPUT: ${{ inputs.build_args }}
|
|
run: |
|
|
ARGS=""
|
|
while IFS= read -r line; do
|
|
# strip stray CR (CRLF checkout) and surrounding whitespace
|
|
trimmed=$(printf '%s' "${line}" | tr -d '\r' | xargs)
|
|
if [ -n "${trimmed}" ]; then
|
|
ARGS="${ARGS} --build-arg ${trimmed}"
|
|
fi
|
|
done <<< "${BUILD_ARGS_INPUT}"
|
|
echo "build_args=${ARGS}" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Build and Push Multi-Arch Image
|
|
shell: bash
|
|
env:
|
|
BUILD_CONTEXT: ${{ inputs.build_context }}
|
|
BUILD_DOCKERFILE: ${{ inputs.build_dockerfile }}
|
|
PLATFORMS: ${{ inputs.platforms }}
|
|
TAG_ARGS: ${{ steps.gentags.outputs.tag_args }}
|
|
BUILD_ARGS: ${{ steps.genbuildargs.outputs.build_args }}
|
|
run: |
|
|
unset DOCKER_HOST || true
|
|
# Build on the remote daemon's default buildkit via the SSH context and
|
|
# push directly. Registry credentials come from the `docker login` in
|
|
# the first step (buildx forwards the runner's auth to buildkit).
|
|
docker --context remote-builder buildx build \
|
|
--platform "${PLATFORMS}" \
|
|
--file "${BUILD_CONTEXT}/${BUILD_DOCKERFILE}" \
|
|
${BUILD_ARGS} \
|
|
${TAG_ARGS} \
|
|
--push \
|
|
"${BUILD_CONTEXT}"
|