Skip to content

Publish Docker Image #26

Publish Docker Image

Publish Docker Image #26

name: Publish Docker Image
on:
workflow_dispatch:
inputs:
tags:
description: "Docker image tags, comma-separated (e.g., latest,v0.1.0)"
required: false
default: "latest"
permissions:
contents: read
packages: write
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-image:
name: Build Docker image (${{ matrix.arch }})
strategy:
matrix:
include:
- runner: ubuntu-latest
arch: amd64
- runner: ubuntu-22.04-arm
arch: arm64
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Prepare tags
id: prep
run: |
TAGS=""
IFS=',' read -ra TAG_ARRAY <<< "${{ inputs.tags }}"
for t in "${TAG_ARRAY[@]}"; do
TAGS="${TAGS}${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${t}-${{ matrix.arch }},"
done
TAGS="${TAGS%,}" # Remove trailing comma
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
build-args: |
GIT_COMMIT=${{ github.sha }}
GIT_BRANCH=${{ github.ref_name }}
push: true
tags: ${{ steps.prep.outputs.tags }}
platforms: linux/${{ matrix.arch }}
cache-from: type=gha,scope=${{ matrix.arch }}
cache-to: type=gha,scope=${{ matrix.arch }},mode=max
publish-manifest:
name: Create and push multi-arch manifest
runs-on: ubuntu-latest
needs: build-image
steps:
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create multi-arch manifests
env:
SHORT_SHA: ${{ github.sha }}
run: |
IFS=',' read -ra TAG_ARRAY <<< "${{ inputs.tags }}"
FIRST_TAG="${TAG_ARRAY[0]}"
# Create manifest for first tag with SHA tag
docker buildx imagetools create \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${FIRST_TAG} \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${SHORT_SHA::7} \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${FIRST_TAG}-amd64 \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${FIRST_TAG}-arm64
# Create manifests for remaining tags
for t in "${TAG_ARRAY[@]:1}"; do
docker buildx imagetools create \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${t} \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${t}-amd64 \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${t}-arm64
done