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 # 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 # 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): # Design decisions (details in the PR description):
# - Sources: the job container has no Node, so actions/checkout cannot run. Kaniko # - 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' - 'requirements-dev.txt'
- '.pre-commit-config.yaml' - '.pre-commit-config.yaml'
- '.gitea/workflows/image.yml' - '.gitea/workflows/image.yml'
tags:
- 'v*'
workflow_dispatch: {} workflow_dispatch: {}
permissions: permissions:
@@ -54,20 +52,28 @@ jobs:
if: >- if: >-
github.repository == 'mortis/kamino' && github.repository == 'mortis/kamino' &&
github.event_name != 'pull_request' && 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 runs-on: kamino-docker
timeout-minutes: 45 timeout-minutes: 45
# One job at a time on the single-capacity runner. An empty subpath means the # One job at a time on the single-capacity runner. An empty subpath means the
# repository root is the build context. # 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: strategy:
fail-fast: false fail-fast: false
max-parallel: 1 max-parallel: 1
matrix: matrix:
include: include:
- name: molecule-ubuntu2404 - name: molecule-ubuntu2404
version: 0.1.0
subpath: images/molecule-ubuntu2404 subpath: images/molecule-ubuntu2404
dockerfile: Dockerfile dockerfile: Dockerfile
- name: ci - name: ci
version: 0.1.0
subpath: "" subpath: ""
dockerfile: images/ci/Dockerfile dockerfile: images/ci/Dockerfile
container: container:
@@ -77,23 +83,17 @@ jobs:
env: env:
DEBIAN_FRONTEND: noninteractive DEBIAN_FRONTEND: noninteractive
IMAGE_PATH: mortis/${{ matrix.name }} IMAGE_PATH: mortis/${{ matrix.name }}
IMAGE_VERSION: ${{ matrix.version }}
steps: steps:
# A tag becomes the image tag. OCI tags cannot contain '+' (semver build metadata), # The version becomes an image tag. OCI tags cannot contain '+' (semver build
# and the trigger glob `v*` also matches names like `vfoo`: refuse both before # metadata), so only MAJOR.MINOR.PATCH[-prerelease] is accepted. Refuse before
# anything is installed or built. Only bash builtins are used. # anything is installed or built. Only bash builtins are used.
- name: Validate the ref - name: Validate the image version
env:
REF: ${{ github.ref }}
REF_NAME: ${{ github.ref_name }}
run: | run: |
case "$REF" in if ! [[ "$IMAGE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
refs/tags/*) echo "::error::Image version '$IMAGE_VERSION' is not MAJOR.MINOR.PATCH[-prerelease]; refusing to build or push."
if ! [[ "$REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then exit 1
echo "::error::Tag '$REF_NAME' is not vMAJOR.MINOR.PATCH[-prerelease]; refusing to build or push." fi
exit 1
fi
;;
esac
# One step, in this order, so the filesystem the build starts from is the pristine # 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 # 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 "https://${registry}/v2/${IMAGE_PATH}/blobs/uploads/" || true
} }
write_config() { write_config() {
cred_user="$1"
cred_token="$2"
auth="$(printf '%s:%s' "$1" "$2" | base64 -w0)" auth="$(printf '%s:%s' "$1" "$2" | base64 -w0)"
mkdir -p /kaniko/.docker mkdir -p /kaniko/.docker
(umask 077; printf '{"auths":{"%s":{"auth":"%s"}}}\n' "$registry" "$auth" > /kaniko/.docker/config.json) (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 "$method" > /kaniko/credential-method
echo "Registry credential in use: $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 # 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. # added, then compare packages (names and versions) and files with the record.
pkgs > /kaniko/pkgs.installed pkgs > /kaniko/pkgs.installed
@@ -247,11 +264,16 @@ jobs:
image="${registry}/${IMAGE_PATH}" image="${registry}/${IMAGE_PATH}"
short="${SHA:0:12}" short="${SHA:0:12}"
dests=(--destination "${image}:sha-${short}") # /kaniko is removed when the build finishes, so read the marker now.
case "$REF" in dests=(--destination "${image}:sha-${short}" --destination "${image}:latest")
refs/heads/main) dests+=(--destination "${image}:latest") ;; version_new=true
refs/tags/v*) dests+=(--destination "${image}:${REF_NAME}") ;; if [ -e /kaniko/version-published ]; then
esac 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>: # 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 # 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." echo "::error::kaniko finished but no pushed image digest was found in its output."
exit 1 exit 1
fi 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 if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
{ {
echo "### ${IMAGE_PATH} pushed" echo "### ${IMAGE_PATH} pushed"
echo echo
echo "Pin it by digest: \`${image}@${digest}\`" echo "Pin it as: \`${ref}\`"
} >> "$GITHUB_STEP_SUMMARY" || true } >> "$GITHUB_STEP_SUMMARY" || true
fi fi