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 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: | 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, so a # firewall that accepts the SYN but drops later packets would otherwise # stall SSH negotiation indefinitely. echo "== probing ssh://${BUILDER_USER}@${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 likely opens but SSH negotiation stalls." echo "-> Check firewall path runner -> ${BUILDER_HOST}:22 (VLAN), or MTU." fi exit "${rc}" fi - name: Set up remote Docker Buildx builder shell: bash env: BUILDER_HOST: ${{ inputs.docker_host }} BUILDER_USER: ${{ inputs.docker_user }} run: | # Never set the DOCKER_HOST env var to the builder hostname: a bare # value is treated as tcp://host:2375 and, more importantly, DOCKER_HOST # *overrides* the active docker context/builder. Keep it unset so buildx # talks to the SSH endpoint we specify below. unset DOCKER_HOST || true # Create the buildx builder directly from the SSH endpoint. We do NOT # create a docker context of the same name first: buildx refuses to # create a builder instance whose name collides with an existing # context ("instance name already exists as context builder"). docker buildx rm -f remote-builder 2>/dev/null || true docker context rm -f remote-builder 2>/dev/null || true docker buildx create \ --name remote-builder \ --use \ "ssh://${BUILDER_USER}@${BUILDER_HOST}" docker buildx inspect --bootstrap - name: Generate tags list id: gentags shell: bash env: IMAGE: ${{ inputs.image }} IMAGE_TAGS: ${{ inputs.image_tag }} run: | TAGS_LIST="" IFS=',' read -ra ADDR <<< "${IMAGE_TAGS}" for tag in "${ADDR[@]}"; do trimmed_tag=$(echo "${tag}" | xargs) if [ -n "${trimmed_tag}" ]; then TAGS_LIST="${TAGS_LIST}${IMAGE}:${trimmed_tag}"$'\n' fi done echo "tags<> "$GITHUB_OUTPUT" echo -n "${TAGS_LIST}" >> "$GITHUB_OUTPUT" echo "EOF" >> "$GITHUB_OUTPUT" - name: Build and Push Multi-Arch Image uses: docker/build-push-action@v6 with: push: true context: ${{ inputs.build_context }} file: ${{ inputs.build_dockerfile }} builder: remote-builder platforms: ${{ inputs.platforms }} tags: ${{ steps.gentags.outputs.tags }}