Files

207 lines
7.9 KiB
YAML

name: image
# 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 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>/<project>-<image> (or <dockerhub-image>) when the Docker Hub
# secrets are passed.
#
# After a push, the compressed size per platform is printed to the log and the job summary.
#
# 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@v1.0.0
# with:
# 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:
registry:
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 as <project>-<image>.
type: string
required: true
dockerhub-image:
description: Docker Hub repository name, defaults to <project>-<image>.
type: string
default: ""
context:
type: string
default: "."
dockerfile:
description: Defaults to <context>/Dockerfile.
type: string
default: ""
platforms:
type: string
default: linux/amd64,linux/arm64
build-args:
description: Newline-separated KEY=value pairs.
type: string
default: ""
# push and cache are "true"/"false" strings, not booleans: on Gitea 1.27 with
# gitea/runner v2.0.1 every boolean input of a called workflow arrives as false
# (the runner compares Gitea's native true against the string "true").
push:
description: Push the image, "true" or "false". Pass false for pull requests.
type: string
default: "true"
cache:
description: Keep the layer cache in Harbor as <image>:buildcache, "true" (default) or "false".
type: string
default: "true"
secrets:
registry-username:
required: false
registry-token:
required: false
# Optional: with both set, the image is also pushed to
# docker.io/<dockerhub-username>/<dockerhub-image>.
dockerhub-username:
required: false
dockerhub-token:
required: false
outputs:
tag:
description: The timestamp tag, <UTC yyyymmddHHMMSS>-<sha7>.
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.build.outputs.version }}
tags:
description: Newline-separated full image references that were pushed.
value: ${{ jobs.build.outputs.tags }}
jobs:
build:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.ts.outputs.tag }}
version: ${{ steps.meta.outputs.version }}
tags: ${{ steps.meta.outputs.tags }}
env:
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 }}
DOCKERHUB_IMAGE: ${{ inputs.dockerhub-image || format('{0}-{1}', inputs.project, inputs.image) }}
steps:
- uses: actions/checkout@v7
- id: ts
run: |
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}/${DOCKERHUB_IMAGE}"
fi
{
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
- if: inputs.push == 'true'
uses: docker/login-action@v4
with:
registry: ${{ inputs.registry }}
username: ${{ secrets.registry-username }}
password: ${{ secrets.registry-token }}
- if: inputs.push == 'true' && env.DOCKERHUB == 'true'
uses: docker/login-action@v4
with:
username: ${{ secrets.dockerhub-username }}
password: ${{ secrets.dockerhub-token }}
- id: build
uses: docker/build-push-action@v7
with:
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile || format('{0}/Dockerfile', inputs.context) }}
platforms: ${{ inputs.platforms }}
push: ${{ inputs.push == 'true' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: ${{ inputs.build-args }}
cache-from: ${{ inputs.cache == 'true' && format('type=registry,ref={0}:buildcache', env.IMAGE) || '' }}
cache-to: ${{ inputs.cache == 'true' && inputs.push == 'true' && format('type=registry,ref={0}:buildcache,mode=max,image-manifest=true,oci-mediatypes=true', env.IMAGE) || '' }}
# Compressed size per platform (sum of the layer sizes, what a pull downloads), read
# back from the registry. Only after a push: a multi-platform build isn't kept locally.
- if: inputs.push == 'true'
env:
DIGEST: ${{ steps.build.outputs.digest }}
PLATFORMS: ${{ inputs.platforms }}
run: |
raw="$(docker buildx imagetools inspect --raw "${IMAGE}@${DIGEST}")"
# a single-platform push without attestations is a plain manifest, not an index
if echo "${raw}" | jq -e '.manifests' >/dev/null; then
list="$(echo "${raw}" | jq -r '.manifests[]
| select(.annotations["vnd.docker.reference.type"] != "attestation-manifest")
| "\(.platform.os)/\(.platform.architecture)\(if .platform.variant then "/" + .platform.variant else "" end) \(.digest)"')"
else
list="${PLATFORMS} ${DIGEST}"
fi
{
echo "### ${IMAGE}"
echo ""
echo "| Platform | Compressed size |"
echo "|---|---|"
echo "${list}" | while read -r platform digest; do
bytes="$(docker buildx imagetools inspect --raw "${IMAGE}@${digest}" | jq '[.layers[].size] | add')"
echo "| ${platform} | $(numfmt --to=iec --suffix=B "${bytes}") |"
done
} | tee -a "${GITHUB_STEP_SUMMARY:-/dev/null}"