seriesoftubes/.github/workflows/docker.yml
2026-11-05 20:14:04 +01:00

72 lines
2.2 KiB
YAML

name: Build and push Docker image
# Every push to main publishes :latest and :<sha>. A release publishes :<version>
# built from its tag: auto-release.yml dispatches this workflow alongside the Pages
# deploy, because releases created with the workflow token do not fire the
# "released" event. Hand-published releases fire it directly.
on:
push:
branches: [main]
release:
types: [released, prereleased]
workflow_dispatch:
inputs:
tag:
description: Existing release tag to build (e.g. v2.10.0); empty builds main
required: false
permissions:
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.release.tag_name || inputs.tag }}
- name: Work out the image tags
id: tags
env:
TAG: ${{ github.event.release.tag_name || inputs.tag }}
run: |
set -euo pipefail
# The image is named after this repository (ghcr.io/<owner>/<repo>, lowercased).
image=ghcr.io/${GITHUB_REPOSITORY,,}
if [ -n "$TAG" ]; then
want="v$(jq -r .version package.json)"
if [ "$TAG" != "$want" ]; then
echo "::error::Release tag $TAG does not match package.json version ($want)"
exit 1
fi
echo "tags=$image:${TAG#v}" >> "$GITHUB_OUTPUT"
else
echo "tags=$image:latest,$image:$GITHUB_SHA" >> "$GITHUB_OUTPUT"
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: docker/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.tags.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max