feat(image): publish the images under a semver tag #7

Merged
Defame1297 merged 2 commits from feat/image-semver into main 2026-10-25 11:45:53 +00:00
+49 -25
View File
@@ -6,7 +6,7 @@ name: Molecule image
#
# Never on pull_request: a workflow file comes from the PR head, so a fork PR could
# otherwise push to the registry. This workflow has no pull_request trigger, and the job
# `if:` repeats the rule for main, v* tags and manual dispatch only.
# `if:` repeats the rule: main only (pushes and manual dispatch from main).
#
# Design decisions (details in the PR description):
# - Sources: the job container has no Node, so actions/checkout cannot run. Kaniko
@@ -36,8 +36,6 @@ on: # yamllint disable-line rule:truthy
- 'requirements-dev.txt'
- '.pre-commit-config.yaml'
- '.gitea/workflows/image.yml'
tags:
- 'v*'
workflow_dispatch: {}
permissions:
@@ -54,20 +52,28 @@ jobs:
if: >-
github.repository == 'mortis/kamino' &&
github.event_name != 'pull_request' &&
(github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
github.ref == 'refs/heads/main'
runs-on: kamino-docker
timeout-minutes: 45
# One job at a time on the single-capacity runner. An empty subpath means the
# repository root is the build context.
#
# version: the semver tag of the image, independent of the Collection's releases.
# Bump it in the same PR as an image change you want to publish under a new tag.
# A version that is already in the registry is never moved (see the prepare step):
# such a build is tagged sha-<12> and latest only. Pin consumers as
# image:<version>@sha256:<digest>; the digest is what is enforced.
strategy:
fail-fast: false
max-parallel: 1
matrix:
include:
- name: molecule-ubuntu2404
version: 0.1.0
subpath: images/molecule-ubuntu2404
dockerfile: Dockerfile
- name: ci
version: 0.1.0
subpath: ""
dockerfile: images/ci/Dockerfile
container:
@@ -77,23 +83,17 @@ jobs:
env:
DEBIAN_FRONTEND: noninteractive
IMAGE_PATH: mortis/${{ matrix.name }}
IMAGE_VERSION: ${{ matrix.version }}
steps:
# A tag becomes the image tag. OCI tags cannot contain '+' (semver build metadata),
# and the trigger glob `v*` also matches names like `vfoo`: refuse both before
# The version becomes an image tag. OCI tags cannot contain '+' (semver build
# metadata), so only MAJOR.MINOR.PATCH[-prerelease] is accepted. Refuse before
# anything is installed or built. Only bash builtins are used.
- name: Validate the ref
env:
REF: ${{ github.ref }}
REF_NAME: ${{ github.ref_name }}
- name: Validate the image version
run: |
case "$REF" in
refs/tags/*)
if ! [[ "$REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "::error::Tag '$REF_NAME' is not vMAJOR.MINOR.PATCH[-prerelease]; refusing to build or push."
exit 1
fi
;;
esac
if ! [[ "$IMAGE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "::error::Image version '$IMAGE_VERSION' is not MAJOR.MINOR.PATCH[-prerelease]; refusing to build or push."
exit 1
fi
# One step, in this order, so the filesystem the build starts from is the pristine
# base image. Kaniko unpacks the base image into the job container's own root
@@ -172,6 +172,8 @@ jobs:
"https://${registry}/v2/${IMAGE_PATH}/blobs/uploads/" || true
}
write_config() {
cred_user="$1"
cred_token="$2"
auth="$(printf '%s:%s' "$1" "$2" | base64 -w0)"
mkdir -p /kaniko/.docker
(umask 077; printf '{"auths":{"%s":{"auth":"%s"}}}\n' "$registry" "$auth" > /kaniko/.docker/config.json)
@@ -205,6 +207,21 @@ jobs:
echo "$method" > /kaniko/credential-method
echo "Registry credential in use: $method"
# Gitea cannot make a tag immutable, so look the version tag up first: when it
# exists the build must not move it (the build step reads this marker).
code="$(printf 'user = "%s:%s"\n' "$cred_user" "$cred_token" \
| curl -sS -o /dev/null -w '%{http_code}' -I -K - \
-H 'Accept: application/vnd.oci.image.index.v1+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json' \
"https://${registry}/v2/${IMAGE_PATH}/manifests/${IMAGE_VERSION}" || true)"
case "$code" in
200) touch /kaniko/version-published; echo "Version tag ${IMAGE_VERSION} is already published; it will not be moved." ;;
404) echo "Version tag ${IMAGE_VERSION} is not published yet." ;;
*)
echo "::error::Unexpected HTTP status $code looking up the tag ${IMAGE_VERSION}; refusing to guess whether it is published."
exit 1
;;
esac
# Back to the pristine base image (see above): purge exactly what the install
# added, then compare packages (names and versions) and files with the record.
pkgs > /kaniko/pkgs.installed
@@ -247,11 +264,16 @@ jobs:
image="${registry}/${IMAGE_PATH}"
short="${SHA:0:12}"
dests=(--destination "${image}:sha-${short}")
case "$REF" in
refs/heads/main) dests+=(--destination "${image}:latest") ;;
refs/tags/v*) dests+=(--destination "${image}:${REF_NAME}") ;;
esac
# /kaniko is removed when the build finishes, so read the marker now.
dests=(--destination "${image}:sha-${short}" --destination "${image}:latest")
version_new=true
if [ -e /kaniko/version-published ]; then
version_new=false
echo "::warning::${image}:${IMAGE_VERSION} is already published and is not moved;" \
"bump the version in image.yml to release this build under a new tag."
else
dests+=(--destination "${image}:${IMAGE_VERSION}")
fi
# Context: this commit of this repository, cloned by kaniko (#<ref>#<sha>:
# clone <ref>, check out <sha>). With a sub-path only that directory is the
@@ -285,12 +307,14 @@ jobs:
echo "::error::kaniko finished but no pushed image digest was found in its output."
exit 1
fi
echo "Pinned reference: ${image}@${digest}"
ref="${image}@${digest}"
if [ "$version_new" = true ]; then ref="${image}:${IMAGE_VERSION}@${digest}"; fi
echo "Pinned reference: ${ref}"
if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
{
echo "### ${IMAGE_PATH} pushed"
echo
echo "Pin it by digest: \`${image}@${digest}\`"
echo "Pin it as: \`${ref}\`"
} >> "$GITHUB_STEP_SUMMARY" || true
fi