Files
cozystack/hack/promote-packages-contract.bats
T
Myasnikov Daniil a5b1f77f10 fix(ci): push the stable chart once, so a re-run keeps cozy-installer:X.Y.Z
Since finalize publishes the release page last, a run that fails in
Publish draft release is re-run after the chart is already pushed. The
re-run packaged and pushed the chart again, and helm package does not
give the same bytes twice (two packagings of the same tree with
different file times give two digests), so cozy-installer:X.Y.Z moved
to another digest.

The chart step now pushes only when the registry answers that cozy-
installer:X.Y.Z is missing, read as manifest_digest in hack/promote-
retag.sh reads it; any other lookup error fails the step instead of
being taken for a missing tag. cozy-installer:latest still follows the
latest decision on every run.

A bats test runs the step against stub skopeo, helm and yq: the first
run pushes once, a re-run pushes nothing, an older patch leaves latest
alone, a failed push fails the step, and a failed lookup fails it
without packaging or pushing.

Fixes: e1472e58de ("fix(ci): publish the release only after the stable images and chart exist")
Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
(cherry picked from commit e99a15dbee)
2026-11-27 13:51:37 +00:00

302 lines
16 KiB
Bash

#!/usr/bin/env bats
# Structural contract for publishing and verifying stable-candidate packages.
# The registry workflow cannot run in a PR lane, so ordering around the first
# write-once stable tag is pinned here. Behaviour lives in the two *_test suites.
load test_helper
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
PROMOTE="$REPO_ROOT/.github/workflows/promote-rc.yaml"
FINALIZE="$REPO_ROOT/.github/workflows/pull-requests-release.yaml"
PULL_REQUESTS="$REPO_ROOT/.github/workflows/pull-requests.yaml"
code_lines() {
rc=0
grep -v '^[[:space:]]*#' || rc=$?
[ "$rc" -le 1 ]
}
job_block() {
awk -v job=" $1:" '
$0 == job { inside = 1; next }
/^ [a-z0-9_-]+:$/ { inside = 0 }
inside' "$2"
}
step_block() {
awk -v want=" - name: $1" '
$0 == want { inside = 1; next }
/^ - name: / { inside = 0 }
inside' "$2"
}
step_line() {
code_lines < "$2" | grep -nF " - name: $1" | awk -F: 'NR == 1 { print $1 }'
}
@test "candidate publish and verification helpers exist and are executable" {
[ -x "$REPO_ROOT/hack/promote-packages-artifact.sh" ]
[ -x "$REPO_ROOT/hack/verify-promoted-packages.sh" ]
}
@test "promote commits the rewritten tree then publishes and commits its pin" {
block="$(step_block 'Prepare stable branch' "$PROMOTE")"
[ -n "$block" ]
rewrite="$(printf '%s\n' "$block" | code_lines | grep -nF '.release-tooling/hack/promote-rewrite-tags.sh' | awk -F: 'NR == 1 { print $1 }')"
publish="$(printf '%s\n' "$block" | code_lines | grep -nF '.release-tooling/hack/promote-packages-artifact.sh' | awk -F: 'NR == 1 { print $1 }')"
content_commit="$(printf '%s\n' "$block" | code_lines | grep -nF 'git commit -s -m "Prepare release' | awk -F: 'NR == 1 { print $1 }')"
pin_commit="$(printf '%s\n' "$block" | code_lines | grep -nF 'git commit -s -m "Pin promoted packages artifact' | awk -F: 'NR == 1 { print $1 }')"
[ -n "$rewrite" ] && [ -n "$content_commit" ] && [ -n "$publish" ] && [ -n "$pin_commit" ]
[ "$rewrite" -lt "$content_commit" ]
[ "$content_commit" -lt "$publish" ]
[ "$publish" -lt "$pin_commit" ]
# The helper comes from the dispatch ref's sparse tooling checkout, so the
# first release after this fix does not depend on the older rc tree carrying it.
printf '%s\n' "$block" | code_lines | grep -qF '.release-tooling/hack/promote-packages-artifact.sh'
printf '%s\n' "$block" | code_lines | grep -qF 'SOURCE_SHA="$(git rev-parse HEAD)"'
printf '%s\n' "$block" | code_lines | grep -qF 'PROMOTION_ID: ${{ github.run_id }}-${{ github.run_attempt }}'
}
@test "promote authenticates Flux with the same Docker config used by publish" {
job="$(job_block promote "$PROMOTE")"
login="$(step_block 'Login to registry (GHCR)' "$PROMOTE")"
prepare="$(step_block 'Prepare stable branch' "$PROMOTE")"
printf '%s\n' "$job" | code_lines | grep -qF ' packages: write'
printf '%s\n' "$job" | code_lines | grep -qF 'uses: fluxcd/flux2/action@'
printf '%s\n' "$login" | code_lines | grep -qF 'DOCKER_CONFIG: ${{ runner.temp }}/.docker'
printf '%s\n' "$prepare" | code_lines | grep -qF 'DOCKER_CONFIG: ${{ runner.temp }}/.docker'
}
@test "every promotion toolchain install pins its tools" {
# A release gate whose tooling comes from `releases/latest/download` is not a
# fixed input: the bytes that decide whether vX.Y.Z may be created, and the
# yq that rewrites the installer pin, change whenever upstream publishes.
# Pinned by version, and pinned to STAY pinned — an unpinned one-liner comes
# back by copy-paste rather than by anyone deciding to unpin a release step.
# flux is pinned by hack/ci-tool-installs-contract.bats across every workflow.
for step_and_file in \
"Set up toolchain (yq)|$PROMOTE" \
"Set up promotion toolchain (yq)|$PULL_REQUESTS" \
"Set up promotion toolchain (skopeo, yq, helm)|$FINALIZE"; do
block="$(step_block "${step_and_file%%|*}" "${step_and_file#*|}")"
[ -n "$block" ]
printf '%s\n' "$block" | code_lines | grep -qF 'YQ_VERSION: "'
count="$(printf '%s\n' "$block" | code_lines | grep -c 'releases/latest/download' || true)"
[ "${count:-0}" -eq 0 ]
# yq is pinned by content as well as by version: it is the one downloaded
# binary that WRITES what a release contains, stamping the candidate digest
# into installer values and platformVersion into the chart. A version
# variable authenticates nothing about the bytes it names.
printf '%s\n' "$block" | code_lines | grep -qF 'YQ_SHA256: "'
printf '%s\n' "$block" | code_lines | grep -qF 'sha256sum -c -'
done
# One version AND one checksum across all three, so the yq that writes the
# candidate's installer values is the yq that reads them back out of the
# artifact — and so bumping the version in one file without its checksum, or
# either without the other two, cannot pass.
for key in YQ_VERSION YQ_SHA256; do
values="$(cat "$PROMOTE" "$PULL_REQUESTS" "$FINALIZE" | code_lines \
| grep -o "$key: \"[0-9a-f.]*\"" | sort -u)"
[ -n "$values" ]
[ "$(printf '%s\n' "$values" | wc -l | tr -d ' ')" -eq 1 ]
done
}
@test "finalize verifies the pinned candidate before the first stable tag" {
verify="$(step_line 'Verify stable packages candidate' "$FINALIZE")"
create="$(step_line 'Create tag on merge commit (write-once)' "$FINALIZE")"
[ -n "$verify" ] && [ -n "$create" ]
[ "$verify" -lt "$create" ]
block="$(step_block 'Verify stable packages candidate' "$FINALIZE")"
printf '%s\n' "$block" | code_lines | grep -qF '.release-tooling/hack/verify-promoted-packages.sh "${TAG#v}"'
setup="$(step_block 'Set up flux' "$FINALIZE")"
printf '%s\n' "$setup" | code_lines | grep -qF 'uses: fluxcd/flux2/action@'
}
# The release page announces the version, so it must not go public before the
# stable images and chart it points at exist. A step that fails after the draft
# is undrafted also cannot be re-run: the draft lookup in resolve no longer finds
# anything to publish.
@test "finalize publishes the release page only after the stable images and chart exist" {
resolve="$(step_line 'Resolve draft release' "$FINALIZE")"
retag="$(step_line 'Retag rc images to stable' "$FINALIZE")"
chart="$(step_line 'Publish stable cozy-installer chart' "$FINALIZE")"
publish="$(step_line 'Publish draft release' "$FINALIZE")"
[ -n "$resolve" ] && [ -n "$retag" ] && [ -n "$chart" ] && [ -n "$publish" ]
[ "$resolve" -lt "$retag" ]
[ "$retag" -lt "$chart" ]
[ "$chart" -lt "$publish" ]
# One latest decision, made before the first registry write; the registry
# steps and the release page read it instead of recomputing it.
for step in 'Retag rc images to stable' 'Publish stable cozy-installer chart' 'Publish draft release'; do
step_block "$step" "$FINALIZE" | code_lines | grep -qF 'steps.resolve.outputs.make_latest'
done
}
# Finalize decides `latest` minutes before it moves it, so two release PRs
# merged close together must not finalize at once. The group is built from the
# job's own condition: a PR that skips finalize must not take the shared
# group's one pending slot from a release waiting in it.
@test "finalize runs one release at a time, and only release PRs share the queue" {
[ "$(yq '.jobs.finalize.concurrency.cancel-in-progress' "$FINALIZE")" = false ]
# One pending job per group is GitHub's default: a third release merged while
# two are in line would cancel the one waiting.
[ "$(yq '.jobs.finalize.concurrency.queue' "$FINALIZE")" = max ]
cond="$(yq '.jobs.finalize.if' "$FINALIZE" | tr '\n' ' ' | sed 's/[[:space:]][[:space:]]*/ /g; s/^ //; s/ $//')"
[ -n "$cond" ]
want="\${{ ($cond) && 'release-finalize' || format('release-finalize-skipped-{0}', github.event.pull_request.number) }}"
[ "$(yq '.jobs.finalize.concurrency.group' "$FINALIZE")" = "$want" ]
}
# Order alone does not hold the page back. A registry step allowed to fail
# without failing the job, or a publish step that runs whatever happened before
# it, publishes over a missing image or chart all the same. And a patch on an
# older line must move no `latest`: the image and chart aliases follow the one
# decision, not a default.
@test "finalize publishes nothing after a failed registry step, and latest moves only on the resolved decision" {
for step in 'Resolve draft release' 'Retag rc images to stable' 'Publish stable cozy-installer chart' 'Publish draft release'; do
STEP="$step" yq --exit-status '.jobs.finalize.steps[] | select(.name == strenv(STEP)) | has("continue-on-error") | not' "$FINALIZE" >/dev/null
done
# No condition at all on the publish step, so it runs only when every step
# before it succeeded; and no registry command whose failure is swallowed.
yq --exit-status '.jobs.finalize.steps[] | select(.name == "Publish draft release") | has("if") | not' "$FINALIZE" >/dev/null
[ "$(yq '.jobs.finalize.steps[] | select(.name == "Retag rc images to stable") | .run' "$FINALIZE")" = './hack/promote-retag.sh "${STABLE_TAG}"' ]
chart_code="$(yq '.jobs.finalize.steps[] | select(.name == "Publish stable cozy-installer chart") | .run' "$FINALIZE" | code_lines)"
[ "$(printf '%s\n' "$chart_code" | head -n 1)" = 'set -eu' ]
if printf '%s\n' "$chart_code" | grep -qF '||'; then
echo "Publish stable cozy-installer chart swallows a failure with ||" >&2; false
fi
# A registry step skipped for a stable tag publishes over a missing artifact
# just as a failed one would; only a prerelease skips them.
for step in 'Retag rc images to stable' 'Publish stable cozy-installer chart'; do
[ "$(STEP="$step" yq '.jobs.finalize.steps[] | select(.name == strenv(STEP)) | .if' "$FINALIZE")" = "\${{ !contains(steps.get_tag.outputs.tag, '-') }}" ]
done
move_latest="$(yq '.jobs.finalize.steps[] | select(.name == "Retag rc images to stable") | .env.MOVE_LATEST' "$FINALIZE")"
[ "$move_latest" = "\${{ steps.resolve.outputs.make_latest == 'true' && '1' || '0' }}" ]
chart_run="$(yq '.jobs.finalize.steps[] | select(.name == "Publish stable cozy-installer chart") | .run' "$FINALIZE")"
[ "$(printf '%s\n' "$chart_run" | code_lines | grep -cF 'cozy-installer:latest')" -eq 1 ]
printf '%s\n' "$chart_run" | code_lines | grep -A3 -F 'if [ "${MAKE_LATEST}" = "true" ]; then' | grep -qF 'cozy-installer:latest'
}
# helm package does not give the same bytes twice, so a re-run that pushed the
# chart again would move cozy-installer:X.Y.Z to another digest. The step runs
# here as the workflow carries it, against stub skopeo, helm and yq.
@test "finalize pushes the stable chart once, and a re-run keeps the first push" {
tmp="$(mktemp -d)"
mkdir -p "$tmp/bin" "$tmp/tree"
yq '.jobs.finalize.steps[] | select(.name == "Publish stable cozy-installer chart") | .run' "$FINALIZE" > "$tmp/step.sh"
cat > "$tmp/bin/helm" <<'EOF'
#!/bin/sh
echo "helm $*" >> "$CALLS"
case "$1" in
package) echo "Successfully packaged chart and saved it to: $TMP_DIR/cozy-installer.tgz" ;;
push) [ -z "${PUSH_FAILS:-}" ] || exit 1 ;;
esac
EOF
# CHART_EXISTS: 1 finds the tag, unset answers as GHCR does for a missing tag,
# and error answers with any other failure.
cat > "$tmp/bin/skopeo" <<'EOF'
#!/bin/sh
echo "skopeo $*" >> "$CALLS"
[ "$1" = inspect ] || exit 0
case "${CHART_EXISTS:-}" in
1) echo '{"schemaVersion":2}' ;;
error) echo 'toomanyrequests: retry-after: 60s' >&2; exit 1 ;;
*) echo 'reading manifest 1.2.3 in ghcr.io/example/cozy-installer: manifest unknown' >&2; exit 2 ;;
esac
EOF
printf '#!/bin/sh\necho "yq $*" >> "$CALLS"\n' > "$tmp/bin/yq"
chmod +x "$tmp/bin/helm" "$tmp/bin/skopeo" "$tmp/bin/yq"
# run_chart_step <case> <MAKE_LATEST> <CHART_EXISTS> [<PUSH_FAILS>]
run_chart_step() {
(cd "$tmp/tree" && PATH="$tmp/bin:$PATH" TMP_DIR="$tmp" CALLS="$tmp/$1" TAG=v1.2.3 REGISTRY=ghcr.io/example \
MAKE_LATEST="$2" CHART_EXISTS="$3" PUSH_FAILS="${4:-}" bash -e "$tmp/step.sh")
}
run_chart_step first true ''
grep -qx 'helm push .*/cozy-installer.tgz oci://ghcr.io/example' "$tmp/first"
grep -qF 'docker://ghcr.io/example/cozy-installer:1.2.3 docker://ghcr.io/example/cozy-installer:latest' "$tmp/first"
run_chart_step rerun true 1
if grep -q '^helm ' "$tmp/rerun"; then echo "FAIL: a re-run packaged or pushed the chart again"; false; fi
grep -qF 'docker://ghcr.io/example/cozy-installer:1.2.3 docker://ghcr.io/example/cozy-installer:latest' "$tmp/rerun"
run_chart_step older false ''
grep -q '^helm push ' "$tmp/older"
if grep -qF 'cozy-installer:latest' "$tmp/older"; then echo "FAIL: an older patch moved cozy-installer:latest"; false; fi
if run_chart_step failing true '' 1; then echo "FAIL: a failed helm push did not fail the step"; false; fi
# A lookup that fails for another reason says nothing about the tag.
if run_chart_step lookup-error true error; then echo "FAIL: a failed lookup did not fail the step"; false; fi
if grep -q '^helm ' "$tmp/lookup-error"; then echo "FAIL: a failed lookup packaged or pushed the chart"; false; fi
rm -rf "$tmp"
}
@test "promote rejects an old target base before publishing a candidate" {
validate="$(step_block 'Validate rc is promotable' "$PROMOTE")"
prepare_line="$(step_line 'Prepare stable branch' "$PROMOTE")"
validate_line="$(step_line 'Validate rc is promotable' "$PROMOTE")"
[ -n "$validate" ] && [ -n "$validate_line" ] && [ -n "$prepare_line" ]
[ "$validate_line" -lt "$prepare_line" ]
printf '%s\n' "$validate" | code_lines | grep -qF "['.github/workflows/pull-requests.yaml', 'verify-release-candidate:']"
printf '%s\n' "$validate" | code_lines | grep -qF "['.github/workflows/pull-requests-release.yaml', 'Verify stable packages candidate']"
printf '%s\n' "$validate" | code_lines | grep -qF "['hack/verify-promoted-packages.sh', 'EXPECTED_PACKAGES_REPOSITORY']"
# The verifier resolves its libraries relative to itself at run time, so a
# base carrying the script without them fails the gate it is meant to be.
# BOTH of them: the shared image-reference enumeration is sourced through the
# same `dirname $0` as the promoted-packages library, so a base missing it
# fails in the same place. Pinned per entry rather than only as a block —
# dropping any single pair used to leave every assertion here green.
printf '%s\n' "$validate" | code_lines | grep -qF "['hack/lib/image-refs.sh', 'collect_image_refs()']"
printf '%s\n' "$validate" | code_lines | grep -qF "['hack/lib/promoted-packages.sh', 'PACKAGES_DIGEST_REF_PATTERN']"
printf '%s\n' "$validate" | code_lines | grep -qF "core.setOutput('base_branch', baseBranch)"
open_pr="$(step_block 'Open promote (release) PR' "$PROMOTE")"
printf '%s\n' "$open_pr" | code_lines | grep -qF 'BASE_BRANCH: ${{ needs.promote.outputs.base_branch }}'
printf '%s\n' "$open_pr" | code_lines | grep -qF 'BASE="$BASE_BRANCH"'
}
@test "pull request gate verifies the prospective merge with trusted base tooling" {
job="$(job_block verify-release-candidate "$PULL_REQUESTS")"
[ -n "$job" ]
printf '%s\n' "$job" | code_lines | grep -qF "github.event.pull_request.user.login == 'cozystack-ci[bot]'"
printf '%s\n' "$job" | code_lines | grep -qF 'ref: ${{ github.event.pull_request.base.sha }}'
printf '%s\n' "$job" | code_lines | grep -qF '.release-tooling/hack/verify-promoted-packages.sh "$STABLE_VERSION"'
printf '%s\n' "$job" | code_lines | grep -qF 'uses: fluxcd/flux2/action@'
report="$(job_block e2e-report "$PULL_REQUESTS")"
printf '%s\n' "$report" | code_lines | grep -qF '"verify-release-candidate"'
printf '%s\n' "$report" | code_lines | grep -qF "IS_RELEASE === 'true' && CANDIDATE_RESULT !== 'success'"
# `e2e` is the other consumer, and the one that RUNS the suite. Reporting a
# red is a lesser guarantee than not running a full-e2e promotion suite
# against a candidate that did not verify, so pin both halves: the dependency
# and the clause on the release arm. Removing them together left every other
# assertion in this file green.
e2e="$(job_block e2e "$PULL_REQUESTS")"
[ -n "$e2e" ]
printf '%s\n' "$e2e" | code_lines | grep -qF '"verify-release-candidate"'
printf '%s\n' "$e2e" | code_lines | grep -qF 'needs.verify-release-candidate.result == '"'"'success'"'"''
# gh applies --label after opening a PR. The release label event must replace
# the initial run and publish a fresh status that includes candidate verify.
grep -qF "github.event.label.name != 'release'" "$PULL_REQUESTS"
plan="$(job_block plan "$PULL_REQUESTS")"
printf '%s\n' "$plan" | code_lines | grep -qF "github.event.label.name == 'release'"
printf '%s\n' "$report" | code_lines | grep -qF "github.event.label.name == 'release'"
}