take registry, project and image as inputs, optional docker hub push

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-16 19:13:14 +02:00
co-authored by Claude Opus 5.5
parent e75f481d68
commit 9998d345d7
2 changed files with 86 additions and 19 deletions
+62 -14
View File
@@ -10,6 +10,9 @@ name: image
# 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.
#
# Images go to <registry>/<project>/<image>, and to docker.io/<dockerhub-username>/<image>
# when the Docker Hub secrets are passed.
#
# Caller:
#
# jobs:
@@ -18,24 +21,42 @@ name: image
# needs: test
# uses: traberph/ci/.gitea/workflows/image.yaml@main
# with:
# images: '[{"image": "registry.traberph.de/private/cv"}]'
# project: private
# image: cv
# secrets:
# registry-username: ${{ secrets.REGISTRY_USERNAME }}
# registry-token: ${{ secrets.REGISTRY_TOKEN }}
# # optional, also pushes to Docker Hub
# dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
# dockerhub-token: ${{ secrets.DOCKERHUB_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
project:
description: Harbor project, e.g. public or private.
type: string
required: true
image:
description: Image name, in Harbor and on Docker Hub.
type: string
default: ""
context:
type: string
default: "."
dockerfile:
description: Defaults to <context>/Dockerfile.
type: string
default: ""
images:
description: >-
Several images instead of image/context/dockerfile, as a JSON array:
[{"image": "<name>", "context": ".", "dockerfile": "<context>/Dockerfile"}].
type: string
default: ""
platforms:
type: string
default: linux/amd64,linux/arm64
@@ -48,7 +69,7 @@ on:
type: boolean
default: true
cache:
description: Keep the layer cache in the registry as <image>:buildcache.
description: Keep the layer cache in Harbor as <image>:buildcache.
type: boolean
default: false
secrets:
@@ -56,6 +77,11 @@ on:
required: false
registry-token:
required: false
# Optional: with both set, every image is also pushed to docker.io/<dockerhub-username>/<image>.
dockerhub-username:
required: false
dockerhub-token:
required: false
outputs:
tag:
description: The timestamp tag, <UTC yyyymmddHHMMSS>-<sha7>.
@@ -102,18 +128,34 @@ jobs:
strategy:
fail-fast: false
matrix:
include: ${{ fromJSON(inputs.images) }}
# A single image/context/dockerfile becomes a one-entry list.
include: ${{ fromJSON(inputs.images || format('[{{"image":"{0}","context":"{1}","dockerfile":"{2}"}}]', inputs.image, inputs.context, inputs.dockerfile)) }}
env:
IMAGE: ${{ inputs.registry }}/${{ inputs.project }}/${{ matrix.image }}
# Secrets can't be used in `if:` directly, env can.
DOCKERHUB: ${{ secrets.dockerhub-username != '' && secrets.dockerhub-token != '' }}
DOCKERHUB_USERNAME: ${{ secrets.dockerhub-username }}
steps:
- uses: actions/checkout@v7
- id: tags
env:
IMAGE: ${{ matrix.image }}
NAME: ${{ matrix.image }}
TAG_NAMES: ${{ needs.meta.outputs.tag-names }}
run: |
if [ -z "${NAME}" ]; then
echo "::error::set the image input (or images)"
exit 1
fi
images="${IMAGE}"
if [ "${DOCKERHUB}" = "true" ]; then
images="${images} docker.io/${DOCKERHUB_USERNAME}/${NAME}"
fi
{
echo "tags<<EOF"
printf '%s\n' "$TAG_NAMES" | sed "/^$/d; s|^|${IMAGE}:|"
for image in ${images}; do
printf '%s\n' "$TAG_NAMES" | sed "/^$/d; s|^|${image}:|"
done
echo "EOF"
} >> "$GITHUB_OUTPUT"
@@ -128,6 +170,12 @@ jobs:
username: ${{ secrets.registry-username }}
password: ${{ secrets.registry-token }}
- if: inputs.push && env.DOCKERHUB == 'true'
uses: docker/login-action@v4
with:
username: ${{ secrets.dockerhub-username }}
password: ${{ secrets.dockerhub-token }}
- uses: docker/build-push-action@v7
with:
context: ${{ matrix.context || '.' }}
@@ -137,5 +185,5 @@ jobs:
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) || '' }}
cache-from: ${{ inputs.cache && format('type=registry,ref={0}:buildcache', env.IMAGE) || '' }}
cache-to: ${{ inputs.cache && inputs.push && format('type=registry,ref={0}:buildcache,mode=max,image-manifest=true,oci-mediatypes=true', env.IMAGE) || '' }}