Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/actions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Shared Docker image CI

The active image repositories use the same release boundary, implemented with
Bash and `jq` here in `kool-dev/kool`. Keep image-specific builds, smoke tests,
scanners, supported tags and dispatch recipients in each image repository.

## Pipeline

1. Run generation checks once. Export the complete expected stable-tag list.
2. Build/test on native `ubuntu-24.04` and/or `ubuntu-24.04-arm` jobs through
`image-build`. Its Docker driver keeps fresh same-job parents available.
3. Pull upstream parents only for base builds. Do not force-pull derived parents.
Use `image-parent` to assert the tested base ID and full layer prefix.
4. After **all** tests/scans in a native job pass, call `image-stage` with a JSON
array of `{tag,id}` pairs. It checks the local IDs/platforms, tags those IDs,
pushes them once and captures Docker's returned digests without remote
inspections. It uploads one run/attempt/group/architecture artifact.
5. A single `image-publish.yml` job needs the entire native matrix and checks.
It validates every expected tag/platform record before login or stable writes,
then assembles indexes from those exact immutable digests. Single-arch
releases get a one-platform index (not ARM support). Node can retain its historical
`-x86_64`/`-aarch64` aliases through `architecture-aliases`.
6. Repository-specific downstream dispatch jobs need successful publication.

Pin **all actions, the reusable workflow and its `tooling-ref` input to the same
40-character kool commit**. This prevents a branch update from silently changing
other repositories' release code. Update pins deliberately after testing shared
changes. The publisher checks out this pinned tooling, not mutable caller files.

## Publication guards

Call staging/publication only for the canonical repository's default branch and
non-PR events. The shared Bash guard repeats this check before login/staging.
Do not pass credentials into build/test steps or PR checks. The reusable workflow
only accepts push, schedule, manual and repository-dispatch releases.

Artifacts are scoped to the current workflow run **and attempt**. The publisher
rejects missing/duplicate/foreign/stale/malformed records and requires the exact
expected tag × architecture matrix. It trusts platform/ID checks from the tested
local images; it does not re-read every staged image's remote manifest/config.
Docker manifest assembly still performs registry reads and can hit pull quotas.

**Recovery remains Re-run all jobs.** There is no resume workflow or cross-run
artifact reuse. Stable-tag updates are sequential, not atomic across a release.
On partial publication, downstream notification stays blocked. Run-scoped staging
tags accumulate; these actions do not implement registry cleanup.

Keep existing architecture support: PHP, Swoole, WordPress and Node use amd64 +
arm64; NGINX, SQL Server and OCI8 remain amd64-only until their dependencies are
separately validated for ARM. This standardizes CI, not runtime support policy.

## Tests

```bash
bash .github/actions/image-stage/tests/release.sh
bash .github/actions/image-stage/tests/registry.sh
```

The first test mocks Docker and checks guards, immutable IDs, negative metadata,
absence of extra registry inspections and fresh-parent assertions. The second
uses only an ephemeral loopback registry and synthetic scratch images to verify
actual push digest capture, original config IDs, two/single-platform membership and aliases.
Scratch fixtures do not execute ARM application code; consumers' native CI does.
21 changes: 21 additions & 0 deletions .github/actions/image-build/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Native Docker builder
description: Check native architecture and use Docker's same-job local image store.
inputs:
arch:
description: Native architecture (amd64 or arm64)
required: true
runs:
using: composite
steps:
- name: Verify native runner
shell: bash
env:
ARCH: ${{ inputs.arch }}
run: |
case "$ARCH:$(uname -m)" in
amd64:x86_64|arm64:aarch64) ;;
*) echo "::error::Runner is not native $ARCH"; exit 1 ;;
esac
- uses: docker/setup-buildx-action@v4
with:
driver: docker
21 changes: 21 additions & 0 deletions .github/actions/image-parent/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Verify tested parent
description: Assert that a derived image retains the same-job tested parent ID/layers.
inputs:
base-ref:
description: Local base tag
required: true
base-id:
description: Tested base image ID
required: true
image-id:
description: Derived image ID
required: true
runs:
using: composite
steps:
- shell: bash
env:
BASE_REF: ${{ inputs.base-ref }}
BASE_ID: ${{ inputs.base-id }}
IMAGE_ID: ${{ inputs.image-id }}
run: bash "$GITHUB_ACTION_PATH/../image-stage/release.sh" parent "$BASE_REF" "$BASE_ID" "$IMAGE_ID"
56 changes: 56 additions & 0 deletions .github/actions/image-stage/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Stage tested native images
description: Push immutable tested IDs and upload current-attempt digest metadata.
inputs:
canonical-repository:
description: Exact GitHub repository allowed to publish
required: true
default-branch:
description: Release branch
required: true
repository:
description: Docker Hub repository
required: true
arch:
description: Native architecture
required: true
images:
description: JSON array of tested tag/id pairs
required: true
group:
description: Unique matrix group name
required: true
username:
description: Docker Hub username
required: true
password:
description: Docker Hub token
required: true
runs:
using: composite
steps:
- name: Check publication boundary before login
shell: bash
env:
RELEASE_CANONICAL_REPOSITORY: ${{ inputs.canonical-repository }}
RELEASE_BRANCH: ${{ inputs.default-branch }}
RELEASE_REPOSITORY: ${{ inputs.repository }}
run: bash "$GITHUB_ACTION_PATH/release.sh" guard
- uses: docker/login-action@v4
with:
username: ${{ inputs.username }}
password: ${{ inputs.password }}
- name: Push tested IDs and record digests
shell: bash
env:
RELEASE_CANONICAL_REPOSITORY: ${{ inputs.canonical-repository }}
RELEASE_BRANCH: ${{ inputs.default-branch }}
RELEASE_REPOSITORY: ${{ inputs.repository }}
RELEASE_ARCH: ${{ inputs.arch }}
RELEASE_IMAGES: ${{ inputs.images }}
run: bash "$GITHUB_ACTION_PATH/release.sh" stage
- uses: actions/upload-artifact@v7.0.2
with:
name: images-${{ github.run_id }}-${{ github.run_attempt }}-${{ inputs.group }}-${{ inputs.arch }}
path: release-metadata/*.json
if-no-files-found: error
retention-days: 7
136 changes: 136 additions & 0 deletions .github/actions/image-stage/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env bash
# Shared image release boundary. Requires Bash, jq and Docker; never rebuilds.
set -euo pipefail

die() { echo "::error::$*" >&2; exit 1; }

scope() {
[[ ${GITHUB_RUN_ID:-} =~ ^[1-9][0-9]*$ && ${GITHUB_RUN_ATTEMPT:-} =~ ^[1-9][0-9]*$ ]] ||
die "Invalid run/attempt"
[[ ${RELEASE_REPOSITORY:-} =~ ^[a-z0-9.-]+(:[0-9]+)?/[a-z0-9._/-]+$ ]] ||
die "Invalid registry repository"
}

guard() {
scope
[[ -n ${RELEASE_CANONICAL_REPOSITORY:-} && -n ${RELEASE_BRANCH:-} &&
${GITHUB_REPOSITORY:-} == "$RELEASE_CANONICAL_REPOSITORY" &&
${GITHUB_REF:-} == "refs/heads/$RELEASE_BRANCH" ]] ||
die "Publication requires the canonical repository's default branch"
case ${GITHUB_EVENT_NAME:-} in
push|schedule|workflow_dispatch|repository_dispatch) ;;
*) die "This event cannot publish images" ;;
esac
}

stage() {
guard
[[ ${RELEASE_ARCH:-} == amd64 || ${RELEASE_ARCH:-} == arm64 ]] ||
die "Unsupported native architecture"
# Validate the entire staging batch before pushing its first image.
jq -e '
type == "array" and length > 0 and
(length == (unique_by(.tag) | length)) and
all(.[]; type == "object" and (keys == ["id", "tag"]) and
(.tag | type == "string" and test("^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$")) and
(.id | type == "string" and test("^sha256:[0-9a-f]{64}$")))
' <<< "${RELEASE_IMAGES:-}" >/dev/null || die "Invalid tested image batch"
local tag id info staging output digest directory=${RELEASE_METADATA:-release-metadata}
while IFS=$'\t' read -r tag id; do
info=$(docker image inspect "$id")
jq -e --arg id "$id" --arg arch "$RELEASE_ARCH" '
length == 1 and .[0].Id == $id and .[0].Os == "linux" and .[0].Architecture == $arch
' <<< "$info" >/dev/null || die "Tested image ID/platform mismatch: $tag"
done < <(jq -r '.[] | [.tag, .id] | @tsv' <<< "$RELEASE_IMAGES")
while IFS=$'\t' read -r tag id; do
staging="ci-${GITHUB_REPOSITORY##*/}-$GITHUB_RUN_ID-$GITHUB_RUN_ATTEMPT-$tag-$RELEASE_ARCH"
((${#staging} <= 128)) || die "Staging tag is too long"
docker tag "$id" "$RELEASE_REPOSITORY:$staging"
output=$(docker push "$RELEASE_REPOSITORY:$staging")
printf '%s\n' "$output"
# Docker's successful push response is authoritative; no remote inspection.
digest=$(sed -nE 's/.*digest: (sha256:[0-9a-f]{64})([[:space:]].*)?$/\1/p' <<< "$output")
[[ $digest =~ ^sha256:[0-9a-f]{64}$ ]] || die "Push did not report exactly one digest"
mkdir -p "$directory"
jq -n --arg repository "$RELEASE_REPOSITORY" --arg tag "$tag" --arg arch "$RELEASE_ARCH" \
--arg id "$id" --arg digest "$digest" --arg run "$GITHUB_RUN_ID" --arg attempt "$GITHUB_RUN_ATTEMPT" \
'{schema:1, repository:$repository, tag:$tag, arch:$arch, os:"linux",
image_id:$id, digest:$digest, run_id:$run, run_attempt:$attempt}' \
> "$directory/$tag-$RELEASE_ARCH.json"
done < <(jq -r '.[] | [.tag, .id] | @tsv' <<< "$RELEASE_IMAGES")
}

validate() {
guard
jq -en --argjson tags "${RELEASE_TAGS:-null}" --argjson arches "${RELEASE_ARCHES:-null}" '
($tags | type == "array" and length > 0 and length == (unique | length) and
all(.[]; type == "string" and test("^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$"))) and
($arches | type == "array" and length > 0 and length == (unique | length) and
all(.[]; . == "amd64" or . == "arm64"))
' >/dev/null || die "Invalid expected tag/architecture matrix"
local directory=${RELEASE_METADATA:-release-metadata}
local -a files=()
[[ -d $directory ]] || die "Missing release metadata directory"
mapfile -d '' -t files < <(find "$directory" -type f -name '*.json' -print0 | sort -z)
((${#files[@]})) || die "Missing release metadata"
jq -es --arg repository "$RELEASE_REPOSITORY" --arg run "$GITHUB_RUN_ID" \
--arg attempt "$GITHUB_RUN_ATTEMPT" --argjson tags "$RELEASE_TAGS" --argjson arches "$RELEASE_ARCHES" '
. as $records |
if all(.[];
type == "object" and
keys == ["arch","digest","image_id","os","repository","run_attempt","run_id","schema","tag"] and
.schema == 1 and .repository == $repository and .run_id == $run and .run_attempt == $attempt and
.os == "linux" and (.tag as $t | $tags | index($t) != null) and
(.arch as $a | $arches | index($a) != null) and
(.digest | type == "string" and test("^sha256:[0-9a-f]{64}$")) and
(.image_id | type == "string" and test("^sha256:[0-9a-f]{64}$"))) and
(length == ($tags | length) * ($arches | length)) and
(length == (unique_by([.tag,.arch]) | length)) and
(length == (unique_by(.digest) | length))
then $records | sort_by(.tag,.arch)
else error("Incomplete, duplicate, stale or malformed release metadata") end
' "${files[@]}"
}

publish() {
local records tag arch digest suffix aliases=${RELEASE_ARCH_ALIASES:-false}
records=$(validate) # Entire set checked before the FIRST stable-tag write.
[[ $aliases == false || $aliases == true ]] || die "Invalid architecture alias setting"
local -a sources=()
while IFS= read -r tag; do
mapfile -t sources < <(jq -r --arg tag "$tag" --arg repo "$RELEASE_REPOSITORY" \
'.[] | select(.tag == $tag) | $repo + "@" + .digest' <<< "$records")
docker buildx imagetools create --tag "$RELEASE_REPOSITORY:$tag" "${sources[@]}"
# Node's existing architecture tags remain compatibility aliases, but are
# updated only after the complete release set passes validation.
if [[ $aliases == true ]]; then
while IFS=$'\t' read -r arch digest; do
case $arch in amd64) suffix=x86_64 ;; arm64) suffix=aarch64 ;; esac
docker buildx imagetools create --tag "$RELEASE_REPOSITORY:$tag-$suffix" \
"$RELEASE_REPOSITORY@$digest"
done < <(jq -r --arg tag "$tag" '.[] | select(.tag == $tag) | [.arch,.digest] | @tsv' <<< "$records")
fi
done < <(jq -r '.[]' <<< "$RELEASE_TAGS")
}

parent() {
local base_ref=$1 base_id=$2 derived_id=$3 base derived
[[ $base_id =~ ^sha256:[0-9a-f]{64}$ && $derived_id =~ ^sha256:[0-9a-f]{64}$ ]] ||
die "Invalid tested parent/derived ID"
base=$(docker image inspect "$base_ref")
derived=$(docker image inspect "$derived_id")
jq -en --arg id "$base_id" --arg derived_id "$derived_id" --argjson base "$base" --argjson derived "$derived" '
$base[0].Id == $id and $derived[0].Id == $derived_id and
($base[0].RootFS.Layers | length) > 0 and
$derived[0].RootFS.Layers[0:($base[0].RootFS.Layers | length)] == $base[0].RootFS.Layers
' >/dev/null || die "Derived image did not retain the tested same-job parent"
}

case ${1:-} in
guard) guard ;;
stage) stage ;;
validate) validate ;;
publish) publish ;;
parent) shift; [[ $# == 3 ]] || die "parent requires BASE_REF BASE_ID DERIVED_ID"; parent "$@" ;;
*) die "Usage: release.sh guard|stage|validate|publish|parent" ;;
esac
75 changes: 75 additions & 0 deletions .github/actions/image-stage/tests/registry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash
# Real local-only pushes and promotion; scratch configs do not execute ARM code.
set -euo pipefail
script=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)/release.sh
temporary=$(mktemp -d)
container=
cleanup() {
[[ -z $container ]] || docker rm -f "$container" > /dev/null
rm -rf -- "$temporary"
}
trap cleanup EXIT
container=$(docker create -p 127.0.0.1::5000 registry:2)
docker start "$container" > /dev/null
address=$(docker port "$container" 5000/tcp)
curl --fail --silent --retry 20 --retry-all-errors --retry-delay 1 "http://$address/v2/" > /dev/null
export GITHUB_REPOSITORY=kool-dev/docker-php GITHUB_REF=refs/heads/master GITHUB_EVENT_NAME=push
export GITHUB_RUN_ID=123 GITHUB_RUN_ATTEMPT=2
export RELEASE_CANONICAL_REPOSITORY=kool-dev/docker-php RELEASE_BRANCH=master
export RELEASE_REPOSITORY="$address/test/php" RELEASE_TAGS='["v1","v2"]'
export RELEASE_ARCHES='["amd64","arm64"]' RELEASE_METADATA="$temporary/metadata"
printf 'FROM scratch\nCOPY payload /payload\n' > "$temporary/Dockerfile"
for tag in v1 v2; do
for arch in amd64 arm64; do
printf '%s-%s\n' "$tag" "$arch" > "$temporary/payload"
docker build --builder default --platform "linux/$arch" --provenance=false \
--iidfile "$temporary/id" "$temporary" > /dev/null
id=$(cat "$temporary/id")
docker image save "$id" -o "$temporary/image.tar"
config=$(tar -xOf "$temporary/image.tar" manifest.json | jq -r '.[0].Config')
printf 'sha256:%s\n' "$(tar -xOf "$temporary/image.tar" "$config" | sha256sum | cut -d ' ' -f1)" \
> "$temporary/config-$tag-$arch"
export RELEASE_ARCH=$arch RELEASE_IMAGES
RELEASE_IMAGES=$(jq -cn --arg tag "$tag" --arg id "$id" '[{tag:$tag,id:$id}]')
bash "$script" stage
done
done
bash "$script" publish
for tag in v1 v2; do
manifest=$(docker buildx imagetools inspect --raw "$RELEASE_REPOSITORY:$tag")
expected=$(jq -s 'sort_by(.arch) | map({digest,platform:{architecture:.arch,os:.os}})' \
"$RELEASE_METADATA/$tag-amd64.json" "$RELEASE_METADATA/$tag-arm64.json")
jq -e --argjson expected "$expected" '
[.manifests[] | {digest,platform}] | sort_by(.platform.architecture) == $expected
' <<< "$manifest" > /dev/null
for arch in amd64 arm64; do
digest=$(jq -r .digest "$RELEASE_METADATA/$tag-$arch.json")
config=$(docker buildx imagetools inspect --raw "$RELEASE_REPOSITORY@$digest" | jq -r .config.digest)
[[ $config == "$(cat "$temporary/config-$tag-$arch")" ]]
done
done
# Single-architecture repositories use the same index path, with one descriptor.
export RELEASE_TAGS='["v1"]' RELEASE_ARCHES='["amd64"]' RELEASE_METADATA="$temporary/single"
mkdir "$RELEASE_METADATA"
cp "$temporary/metadata/v1-amd64.json" "$RELEASE_METADATA/"
bash "$script" publish
manifest=$(docker buildx imagetools inspect --raw "$RELEASE_REPOSITORY:v1")
digest=$(jq -r .digest "$RELEASE_METADATA/v1-amd64.json")
jq -e --arg digest "$digest" '
.manifests | length == 1 and .[0].digest == $digest and .[0].platform.architecture == "amd64"
' <<< "$manifest" > /dev/null

# Historical Node architecture aliases also contain exactly their tested image.
export RELEASE_TAGS='["v1","v2"]' RELEASE_ARCHES='["amd64","arm64"]'
export RELEASE_METADATA="$temporary/metadata" RELEASE_ARCH_ALIASES=true
bash "$script" publish
for suffix in x86_64 aarch64; do
arch=amd64
[[ $suffix != aarch64 ]] || arch=arm64
manifest=$(docker buildx imagetools inspect --raw "$RELEASE_REPOSITORY:v1-$suffix")
digest=$(jq -r .digest "$RELEASE_METADATA/v1-$arch.json")
jq -e --arg digest "$digest" --arg arch "$arch" '
.manifests | length == 1 and .[0].digest == $digest and .[0].platform.architecture == $arch
' <<< "$manifest" > /dev/null
done
echo "PASS: real staging, tested config IDs, multi/single-arch indexes and compatibility aliases"
Loading
Loading