setup workflow

This commit is contained in:
2026-10-16 18:45:06 +02:00
commit b94bbe5ea4
3 changed files with 199 additions and 0 deletions
+141
View File
@@ -0,0 +1,141 @@
name: docker-image
# Reusable workflow: builds one or more images of a repository and tags them all
# with the same docker/metadata-action tag set:
#
# <UTC yyyymmddHHMMSS>-<sha7> every build (Flux image automation sorts on it)
# 1.2.3, 1.2, 1 on a v1.2.3 git tag (no "1" while major is 0)
# latest on the default branch
#
# The tag set is computed once in `meta` and shared by every image of the run, so a
# multi-image repo (antispam) gets one timestamp across all images.
#
# Caller:
#
# jobs:
# test: ...
# image:
# needs: test
# uses: traberph/workflows/.gitea/workflows/docker-image.yaml@main
# with:
# images: '[{"image": "registry.traberph.de/private/cv"}]'
# secrets:
# registry-username: ${{ secrets.REGISTRY_USERNAME }}
# registry-token: ${{ secrets.REGISTRY_TOKEN }}
on:
workflow_call:
inputs:
images:
description: >-
JSON array of images to build: [{"image": "<registry>/<project>/<name>",
"context": ".", "dockerfile": "<context>/Dockerfile"}]. Only "image" is required.
type: string
required: true
registry:
description: Registry to log in to.
type: string
default: registry.traberph.de
platforms:
type: string
default: linux/amd64
build-args:
description: Newline-separated KEY=value pairs, passed to every image.
type: string
default: ""
push:
description: Push the images. Pass false for pull requests.
type: boolean
default: true
cache:
description: Keep the layer cache in the registry as <image>:buildcache.
type: boolean
default: false
secrets:
registry-username:
required: false
registry-token:
required: false
outputs:
tag:
description: The timestamp tag, <UTC yyyymmddHHMMSS>-<sha7>.
value: ${{ jobs.meta.outputs.tag }}
version:
description: The version metadata-action resolved (1.2.3 on v1.2.3, else the timestamp tag).
value: ${{ jobs.meta.outputs.version }}
tag-names:
description: Newline-separated tag names pushed for every image.
value: ${{ jobs.meta.outputs.tag-names }}
jobs:
meta:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.tag.outputs.tag }}
version: ${{ steps.meta.outputs.version }}
tag-names: ${{ steps.meta.outputs.tag-names }}
labels: ${{ steps.meta.outputs.labels }}
steps:
# No `images`: metadata-action emits bare tag names, the build job prefixes them.
- id: meta
uses: docker/metadata-action@v6
with:
flavor: latest=false
tags: |
type=raw,value={{date 'YYYYMMDDHHmmss' tz='UTC'}}-{{sha}},priority=1000
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }}
type=raw,value=latest,enable={{is_default_branch}}
- id: tag
env:
TAG_NAMES: ${{ steps.meta.outputs.tag-names }}
run: |
tag="$(printf '%s\n' "$TAG_NAMES" | grep -E '^[0-9]{14}-[0-9a-f]{7}$')"
echo "tag=${tag}" >> "$GITHUB_OUTPUT"
printf 'Tags:\n%s\n' "$TAG_NAMES"
build:
needs: meta
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJSON(inputs.images) }}
steps:
- uses: actions/checkout@v7
- id: tags
env:
IMAGE: ${{ matrix.image }}
TAG_NAMES: ${{ needs.meta.outputs.tag-names }}
run: |
{
echo "tags<<EOF"
printf '%s\n' "$TAG_NAMES" | sed "/^$/d; s|^|${IMAGE}:|"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- if: contains(inputs.platforms, 'arm')
uses: docker/setup-qemu-action@v4
- uses: docker/setup-buildx-action@v4
- if: inputs.push
uses: docker/login-action@v4
with:
registry: ${{ inputs.registry }}
username: ${{ secrets.registry-username }}
password: ${{ secrets.registry-token }}
- uses: docker/build-push-action@v7
with:
context: ${{ matrix.context || '.' }}
file: ${{ matrix.dockerfile || format('{0}/Dockerfile', matrix.context || '.') }}
platforms: ${{ inputs.platforms }}
push: ${{ inputs.push }}
tags: ${{ steps.tags.outputs.tags }}
labels: ${{ needs.meta.outputs.labels }}
build-args: ${{ inputs.build-args }}
cache-from: ${{ inputs.cache && format('type=registry,ref={0}:buildcache', matrix.image) || '' }}
cache-to: ${{ inputs.cache && inputs.push && format('type=registry,ref={0}:buildcache,mode=max,image-manifest=true,oci-mediatypes=true', matrix.image) || '' }}