name: Build Docker Image
on:
workflow_dispatch:
inputs:
version:
description: "Version tag (e.g. 0.3.0). Leave empty to use git tag."
required: false
type: string
release:
types: [published]
env:
GITHUB_CR_REPO: ghcr.io/${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine version
id: version
run: |
if [ -n "${{ inputs.version }}" ]; then
VERSION="${{ inputs.version }}"
elif [ "${{ github.event_name }}" = "release" ]; then
VERSION="${GITHUB_REF_NAME#v}"
else
# Convert git describe to PEP 440: v0.2.1-2-gabcdef -> 0.2.1.dev2
RAW=$(git describe --tags --always 2>/dev/null || echo "0.0.0")
VERSION=$(echo "$RAW" | sed 's/^v//; s/-\([0-9]*\)-g[0-9a-f]*$/.dev\1/')
fi
echo "value=$VERSION" >> $GITHUB_OUTPUT
echo "Building version: $VERSION"
- name: Prepare platform
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.GITHUB_CR_REPO }}
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_PAT }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
tags: ${{ env.GITHUB_CR_REPO }}
build-args: |
SETUPTOOLS_SCM_PRETEND_VERSION=${{ steps.version.outputs.value }}
outputs: type=image,push-by-digest=true,name-canonical=true,push=true
- name: Export digest
run: |
mkdir -p ${{ runner.temp }}/digests
digest="${{ steps.build.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
merge:
runs-on: ubuntu-latest
needs: build
steps:
- name: Determine version
id: version
run: |
if [ -n "${{ inputs.version }}" ]; then
echo "value=${{ inputs.version }}" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" = "release" ]; then
echo "value=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT
else
echo "value=${{ github.ref_name }}" >> $GITHUB_OUTPUT
fi
- name: Download digests
uses: actions/download-artifact@v4
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_PAT }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta for merge
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.GITHUB_CR_REPO }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable=${{ github.event_name == 'release' }}
type=raw,value=${{ steps.version.outputs.value }},enable=${{ github.event_name == 'workflow_dispatch' }}
- name: Docker tags
id: tags
run: |
tags=$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON")
first_tag=$(jq -cr '.tags[0]' <<< "$DOCKER_METADATA_OUTPUT_JSON")
if [ -z "$tags" ] || [ "$tags" = "null" ]; then
tags="-t ${{ env.GITHUB_CR_REPO }}:${{ steps.version.outputs.value }}"
first_tag="${{ env.GITHUB_CR_REPO }}:${{ steps.version.outputs.value }}"
fi
echo "DOCKER_METADATA_TAGS=$tags" >> $GITHUB_ENV
echo "first_tag=$first_tag" >> $GITHUB_OUTPUT
- name: Create multi-arch manifest
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create ${{ env.DOCKER_METADATA_TAGS }} \
$(printf '${{ env.GITHUB_CR_REPO }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ steps.tags.outputs.first_tag }}