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 <noreply@anthropic.com>
This commit is contained in:
+48
-71
@@ -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:
|
||||
#
|
||||
# <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.
|
||||
# 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 <registry>/<project>/<image>, and to docker.io/<dockerhub-username>/<image>
|
||||
# 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 <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
|
||||
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/<dockerhub-username>/<image>.
|
||||
# Optional: with both set, the image is also pushed to docker.io/<dockerhub-username>/<image>.
|
||||
dockerhub-username:
|
||||
required: false
|
||||
dockerhub-token:
|
||||
@@ -85,80 +83,59 @@ on:
|
||||
outputs:
|
||||
tag:
|
||||
description: The timestamp tag, <UTC yyyymmddHHMMSS>-<sha7>.
|
||||
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<<EOF"
|
||||
for image in ${images}; do
|
||||
printf '%s\n' "$TAG_NAMES" | sed "/^$/d; s|^|${image}:|"
|
||||
done
|
||||
echo "ts=${ts}"
|
||||
echo "tag=${ts}-${sha}"
|
||||
echo "images<<EOF"
|
||||
echo "${images}"
|
||||
echo "EOF"
|
||||
} >> "$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) || '' }}
|
||||
|
||||
Reference in New Issue
Block a user