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 # 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. # 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: # Caller:
# #
# jobs: # jobs:
@@ -18,24 +21,42 @@ name: image
# needs: test # needs: test
# uses: traberph/ci/.gitea/workflows/image.yaml@main # uses: traberph/ci/.gitea/workflows/image.yaml@main
# with: # with:
# images: '[{"image": "registry.traberph.de/private/cv"}]' # project: private
# image: cv
# secrets: # secrets:
# registry-username: ${{ secrets.REGISTRY_USERNAME }} # registry-username: ${{ secrets.REGISTRY_USERNAME }}
# registry-token: ${{ secrets.REGISTRY_TOKEN }} # registry-token: ${{ secrets.REGISTRY_TOKEN }}
# # optional, also pushes to Docker Hub
# dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
# dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }}
on: on:
workflow_call: workflow_call:
inputs: 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: registry:
description: Registry to log in to.
type: string type: string
default: registry.traberph.de 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: platforms:
type: string type: string
default: linux/amd64,linux/arm64 default: linux/amd64,linux/arm64
@@ -48,7 +69,7 @@ on:
type: boolean type: boolean
default: true default: true
cache: cache:
description: Keep the layer cache in the registry as <image>:buildcache. description: Keep the layer cache in Harbor as <image>:buildcache.
type: boolean type: boolean
default: false default: false
secrets: secrets:
@@ -56,6 +77,11 @@ on:
required: false required: false
registry-token: registry-token:
required: false 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: outputs:
tag: tag:
description: The timestamp tag, <UTC yyyymmddHHMMSS>-<sha7>. description: The timestamp tag, <UTC yyyymmddHHMMSS>-<sha7>.
@@ -102,18 +128,34 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: 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: steps:
- uses: actions/checkout@v7 - uses: actions/checkout@v7
- id: tags - id: tags
env: env:
IMAGE: ${{ matrix.image }} NAME: ${{ matrix.image }}
TAG_NAMES: ${{ needs.meta.outputs.tag-names }} TAG_NAMES: ${{ needs.meta.outputs.tag-names }}
run: | 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" 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" echo "EOF"
} >> "$GITHUB_OUTPUT" } >> "$GITHUB_OUTPUT"
@@ -128,6 +170,12 @@ jobs:
username: ${{ secrets.registry-username }} username: ${{ secrets.registry-username }}
password: ${{ secrets.registry-token }} 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 - uses: docker/build-push-action@v7
with: with:
context: ${{ matrix.context || '.' }} context: ${{ matrix.context || '.' }}
@@ -137,5 +185,5 @@ jobs:
tags: ${{ steps.tags.outputs.tags }} tags: ${{ steps.tags.outputs.tags }}
labels: ${{ needs.meta.outputs.labels }} labels: ${{ needs.meta.outputs.labels }}
build-args: ${{ inputs.build-args }} build-args: ${{ inputs.build-args }}
cache-from: ${{ inputs.cache && format('type=registry,ref={0}:buildcache', 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', matrix.image) || '' }} cache-to: ${{ inputs.cache && inputs.push && format('type=registry,ref={0}:buildcache,mode=max,image-manifest=true,oci-mediatypes=true', env.IMAGE) || '' }}
+24 -5
View File
@@ -1,6 +1,8 @@
# ci # ci
Reusable Gitea Actions workflows for the apps in the cluster. Reusable Gitea Actions workflows for the apps in the cluster. Needs Gitea 1.27 or later, which
expands called workflows on the server, so each image builds as its own job, in parallel across
the runners. The repository is public so every repo can call it.
## `image.yaml` ## `image.yaml`
@@ -15,6 +17,8 @@ Builds and pushes one or more images with a uniform tag set from
The tag set is computed once per run, so every image of a multi-image repo gets the same tags. The tag set is computed once per run, so every image of a multi-image repo gets the same tags.
If the `dockerhub-username` and `dockerhub-token` secrets are passed, every image is also pushed with the same tags to `docker.io/<dockerhub-username>/<image>`, e.g. `registry.traberph.de/public/homepage` → `docker.io/traberph/homepage`. Only pass them for public images: Docker Hub creates new repositories as public by default.
```yaml ```yaml
jobs: jobs:
test: ... test: ...
@@ -22,21 +26,36 @@ jobs:
needs: test needs: test
uses: traberph/ci/.gitea/workflows/image.yaml@main uses: traberph/ci/.gitea/workflows/image.yaml@main
with: with:
images: '[{"image": "registry.traberph.de/private/cv"}]' project: public
image: homepage
push: ${{ github.event_name != 'pull_request' }} push: ${{ github.event_name != 'pull_request' }}
secrets: secrets:
registry-username: ${{ secrets.REGISTRY_USERNAME }} registry-username: ${{ secrets.REGISTRY_USERNAME }}
registry-token: ${{ secrets.REGISTRY_TOKEN }} registry-token: ${{ secrets.REGISTRY_TOKEN }}
dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }} # optional
dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }} # optional
``` ```
Inputs: Inputs:
- `images` (required): JSON array of `{"image", "context", "dockerfile"}`; `context` defaults to `.`, `dockerfile` to `<context>/Dockerfile`. - `registry`: default `registry.traberph.de`.
- `registry`: registry to log in to (default `registry.traberph.de`). - `project` (required): Harbor project, e.g. `public` or `private`.
- `image`: image name, used in Harbor (`<registry>/<project>/<image>`) and on Docker Hub.
- `context`: default `.`.
- `dockerfile`: default `<context>/Dockerfile`.
- `images`: several images instead of `image`/`context`/`dockerfile`, as a JSON array of `{"image", "context", "dockerfile"}`:
```yaml
project: uni-spam
images: |
[
{"image": "inference", "context": "services/inference"},
{"image": "webapp", "context": "services/webapp/frontend"}
]
```
- `platforms`: default `linux/amd64,linux/arm64`. QEMU is set up when an `arm` platform is listed. - `platforms`: default `linux/amd64,linux/arm64`. QEMU is set up when an `arm` platform is listed.
- `build-args`: newline-separated `KEY=value`, passed to every image. - `build-args`: newline-separated `KEY=value`, passed to every image.
- `push`: default `true`. - `push`: default `true`.
- `cache`: keep the layer cache in the registry as `<image>:buildcache` (default `false`). - `cache`: keep the layer cache in Harbor as `<image>:buildcache` (default `false`).
Outputs: `tag` (the timestamp tag), `version`, `tag-names`. Outputs: `tag` (the timestamp tag), `version`, `tag-names`.