From 9998d345d706522d683ba8d7244923e9b4725be8 Mon Sep 17 00:00:00 2001 From: Philipp Traber Date: Fri, 16 Oct 2026 19:13:14 +0200 Subject: [PATCH] take registry, project and image as inputs, optional docker hub push Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/image.yaml | 76 ++++++++++++++++++++++++++++++------- README.md | 29 +++++++++++--- 2 files changed, 86 insertions(+), 19 deletions(-) diff --git a/.gitea/workflows/image.yaml b/.gitea/workflows/image.yaml index 929de73..61a7031 100644 --- a/.gitea/workflows/image.yaml +++ b/.gitea/workflows/image.yaml @@ -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 //, and to docker.io// +# 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": "//", - "context": ".", "dockerfile": "/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 /Dockerfile. + type: string + default: "" + images: + description: >- + Several images instead of image/context/dockerfile, as a JSON array: + [{"image": "", "context": ".", "dockerfile": "/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 :buildcache. + description: Keep the layer cache in Harbor as :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: + required: false + dockerhub-token: + required: false outputs: tag: description: The timestamp tag, -. @@ -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<> "$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) || '' }} diff --git a/README.md b/README.md index 2d1a32b..3a71222 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # 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` @@ -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. +If the `dockerhub-username` and `dockerhub-token` secrets are passed, every image is also pushed with the same tags to `docker.io//`, 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 jobs: test: ... @@ -22,21 +26,36 @@ jobs: needs: test uses: traberph/ci/.gitea/workflows/image.yaml@main with: - images: '[{"image": "registry.traberph.de/private/cv"}]' + project: public + image: homepage push: ${{ github.event_name != 'pull_request' }} secrets: registry-username: ${{ secrets.REGISTRY_USERNAME }} registry-token: ${{ secrets.REGISTRY_TOKEN }} + dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }} # optional + dockerhub-token: ${{ secrets.DOCKERHUB_TOKEN }} # optional ``` Inputs: -- `images` (required): JSON array of `{"image", "context", "dockerfile"}`; `context` defaults to `.`, `dockerfile` to `/Dockerfile`. -- `registry`: registry to log in to (default `registry.traberph.de`). +- `registry`: default `registry.traberph.de`. +- `project` (required): Harbor project, e.g. `public` or `private`. +- `image`: image name, used in Harbor (`//`) and on Docker Hub. +- `context`: default `.`. +- `dockerfile`: default `/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. - `build-args`: newline-separated `KEY=value`, passed to every image. - `push`: default `true`. -- `cache`: keep the layer cache in the registry as `:buildcache` (default `false`). +- `cache`: keep the layer cache in Harbor as `:buildcache` (default `false`). Outputs: `tag` (the timestamp tag), `version`, `tag-names`.