feature(ci): add actions workflow to publish Docker image to GHCR

This commit is contained in:
Alexander Nicholson 4584443+DragonStuff@users.noreply.github.com
2025-09-04 12:26:00 +02:00
parent eecdb29332
commit ecb0ecaccf

105
.github/workflows/publish-image.yml vendored Normal file
View File

@@ -0,0 +1,105 @@
name: Build and publish container image to GHCR
on:
push:
branches:
- main
tags:
- 'v*'
release:
types: [published]
pull_request:
workflow_dispatch:
concurrency:
group: publish-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
packages: write
id-token: write
attestations: write
jobs:
build-and-push:
name: Build and push image
runs-on: ubuntu-latest
timeout-minutes: 45
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU (multi-arch)
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Compute canonical image name (lowercase)
id: vars
shell: bash
run: |
set -eu
# Use provided IMAGE_NAME or default to the GitHub repository path
IMAGE_REF="${IMAGE_NAME:-$GITHUB_REPOSITORY}"
# Lowercase image ref for GHCR compatibility
CANONICAL_IMAGE="${REGISTRY}/${IMAGE_REF,,}"
echo "canonical=${CANONICAL_IMAGE}" >> "$GITHUB_OUTPUT"
- name: Extract Docker metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ steps.vars.outputs.canonical }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=sha,format=short,prefix=sha-
type=semver,pattern={{version}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=semver,pattern={{major}}.{{minor}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=semver,pattern={{major}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
flavor: |
latest=auto
- name: Build and push
id: build
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
sbom: true
provenance: true
- name: Sign image with Cosign (keyless OIDC)
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@v3
- name: Cosign sign
if: github.event_name != 'pull_request'
run: cosign sign --yes ${{ steps.vars.outputs.canonical }}@${{ steps.build.outputs.digest }}
- name: Attest image provenance
if: github.event_name != 'pull_request'
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ steps.vars.outputs.canonical }}
subject-digest: ${{ steps.build.outputs.digest }}
push-to-registry: true