From f55707417ff0dee483746465a84227666754d08b Mon Sep 17 00:00:00 2001 From: Philipp Traber Date: Fri, 16 Oct 2026 19:29:22 +0200 Subject: [PATCH] one image per call, timestamp tag from commit time Gitea 1.27.0 panics expanding a called workflow whose matrix comes from inputs, and the caller run is never created. Build one image per call instead; the commit time keeps the tag identical across calls. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/image.yaml | 119 +++++++++++++++--------------------- README.md | 59 ++++++++++++------ 2 files changed, 89 insertions(+), 89 deletions(-) diff --git a/.gitea/workflows/image.yaml b/.gitea/workflows/image.yaml index 61a7031..ac43a3f 100644 --- a/.gitea/workflows/image.yaml +++ b/.gitea/workflows/image.yaml @@ -1,25 +1,29 @@ name: image -# Reusable workflow: builds one or more images of a repository and tags them all -# with the same docker/metadata-action tag set: +# Reusable workflow: builds one image and tags it with the docker/metadata-action tag set +# shared by every repo: # # - 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. +# The timestamp is the commit time, not the build time, so the tag depends only on the +# commit: a repo with several images calls this once per image (the calls run in +# parallel) and all of them get the same tag. Rebuilding a commit re-pushes its tag. # # Images go to //, and to docker.io// # when the Docker Hub secrets are passed. # +# One image per call: Gitea 1.27.0 can't expand a matrix built from inputs in a called +# workflow (the run is silently not created). +# # Caller: # # jobs: # test: ... # image: # needs: test -# uses: traberph/ci/.gitea/workflows/image.yaml@main +# uses: traberph/ci/.gitea/workflows/image.yaml@v1.0.0 # with: # project: private # image: cv @@ -43,7 +47,7 @@ on: image: description: Image name, in Harbor and on Docker Hub. type: string - default: "" + required: true context: type: string default: "." @@ -51,21 +55,15 @@ on: 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 build-args: - description: Newline-separated KEY=value pairs, passed to every image. + description: Newline-separated KEY=value pairs. type: string default: "" push: - description: Push the images. Pass false for pull requests. + description: Push the image. Pass false for pull requests. type: boolean default: true cache: @@ -77,7 +75,7 @@ on: required: false registry-token: required: false - # Optional: with both set, every image is also pushed to docker.io//. + # Optional: with both set, the image is also pushed to docker.io//. dockerhub-username: required: false dockerhub-token: @@ -85,80 +83,59 @@ on: outputs: tag: description: The timestamp tag, -. - value: ${{ jobs.meta.outputs.tag }} + value: ${{ jobs.build.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 }} + value: ${{ jobs.build.outputs.version }} + tags: + description: Newline-separated full image references that were pushed. + value: ${{ jobs.build.outputs.tags }} jobs: - meta: + build: runs-on: ubuntu-latest outputs: - tag: ${{ steps.tag.outputs.tag }} + tag: ${{ steps.ts.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: - # 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)) }} + tags: ${{ steps.meta.outputs.tags }} env: - IMAGE: ${{ inputs.registry }}/${{ inputs.project }}/${{ matrix.image }} + IMAGE: ${{ inputs.registry }}/${{ inputs.project }}/${{ inputs.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: - NAME: ${{ matrix.image }} - TAG_NAMES: ${{ needs.meta.outputs.tag-names }} + - id: ts run: | - if [ -z "${NAME}" ]; then - echo "::error::set the image input (or images)" - exit 1 - fi + ts="$(TZ=UTC git log -1 --format=%cd --date=format-local:%Y%m%d%H%M%S)" + # same as metadata-action's {{sha}} + sha="$(echo "${GITHUB_SHA}" | cut -c1-7)" images="${IMAGE}" if [ "${DOCKERHUB}" = "true" ]; then - images="${images} docker.io/${DOCKERHUB_USERNAME}/${NAME}" + images="${images} + docker.io/${DOCKERHUB_USERNAME}/${{ inputs.image }}" fi { - echo "tags<> "$GITHUB_OUTPUT" + - id: meta + uses: docker/metadata-action@v6 + with: + images: ${{ steps.ts.outputs.images }} + flavor: latest=false + tags: | + type=raw,value=${{ steps.ts.outputs.ts }}-{{sha}} + 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}} + - if: contains(inputs.platforms, 'arm') uses: docker/setup-qemu-action@v4 - uses: docker/setup-buildx-action@v4 @@ -178,12 +155,12 @@ jobs: - uses: docker/build-push-action@v7 with: - context: ${{ matrix.context || '.' }} - file: ${{ matrix.dockerfile || format('{0}/Dockerfile', matrix.context || '.') }} + context: ${{ inputs.context }} + file: ${{ inputs.dockerfile || format('{0}/Dockerfile', inputs.context) }} platforms: ${{ inputs.platforms }} push: ${{ inputs.push }} - tags: ${{ steps.tags.outputs.tags }} - labels: ${{ needs.meta.outputs.labels }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} build-args: ${{ inputs.build-args }} 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 3a71222..ff55b40 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,23 @@ # ci 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. +expands called workflows on the server into their own jobs. The repository is public so every +repo can call it. ## `image.yaml` -Builds and pushes one or more images with a uniform tag set from +Builds and pushes one image with a uniform tag set from [docker/metadata-action](https://github.com/docker/metadata-action): | Tag | When | |---|---| -| `-` | every build; Flux `ImagePolicy` sorts on the timestamp | +| `-` | every build; the timestamp is the **commit** time, Flux `ImagePolicy` sorts on it | | `1.2.3`, `1.2`, `1` | on a `v1.2.3` git tag (`1` is skipped while the major is `0`; pre-releases only get the full version) | | `latest` | on the default branch | -The tag set is computed once per run, so every image of a multi-image repo gets the same tags. +The tags depend only on the commit, not on when it was built. A repo with several images calls +the workflow once per image (the calls run as separate jobs, in parallel) and every image gets the +same tags; rebuilding a commit re-pushes its tags instead of producing a newer one. 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. @@ -24,7 +26,7 @@ jobs: test: ... image: needs: test - uses: traberph/ci/.gitea/workflows/image.yaml@main + uses: traberph/ci/.gitea/workflows/image.yaml@v1.0.0 with: project: public image: homepage @@ -40,24 +42,45 @@ Inputs: - `registry`: default `registry.traberph.de`. - `project` (required): Harbor project, e.g. `public` or `private`. -- `image`: image name, used in Harbor (`//`) and on Docker Hub. +- `image` (required): 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. +- `build-args`: newline-separated `KEY=value`. - `push`: default `true`. - `cache`: keep the layer cache in Harbor as `:buildcache` (default `false`). -Outputs: `tag` (the timestamp tag), `version`, `tag-names`. +Outputs: `tag` (the timestamp tag), `version`, `tags` (the full image references). + +Several images: one job per image, e.g. + +```yaml +jobs: + inference: + uses: traberph/ci/.gitea/workflows/image.yaml@v1.0.0 + with: {project: uni-spam, image: inference, context: services/inference} + secrets: ... + webapp: + uses: traberph/ci/.gitea/workflows/image.yaml@v1.0.0 + with: {project: uni-spam, image: webapp, context: services/webapp/frontend} + secrets: ... +``` + +A matrix over images inside the called workflow (`matrix: ${{ fromJSON(inputs.…) }}`) does not +work on Gitea 1.27.0: its parser panics while expanding the call and the run is never created. + +## Versions + +Callers pin a release tag (`@v1.0.0`). Renovate in the calling repos proposes new tags; it needs +a rule pointing `traberph/ci` at this Gitea: + +```json +{ + "matchDepNames": ["traberph/ci"], + "overrideDatasource": "gitea-tags", + "registryUrls": ["https://git.traberph.de"] +} +``` ## Updates