Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

ci(release): finish half-published charts on re-run, share the chart build with PR CI

Petr Bauchd0e2b9f1344f
diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml
index 2259a1539..75a63ce7e 100644
--- a/.github/workflows/pr-ci.yml
+++ b/.github/workflows/pr-ci.yml
@@ -273,26 +273,12 @@ jobs:
uses: azure/setup-helm@1a275c3b69536ee54be43f2070a358922e12c8d4 # v4.3.1
- name: Every first-party image is pinned by digest once pinned
- # The release's own pin, with placeholder digests, then each of the
- # chart's check renders (defaults, and the optional workloads on), so an
- # image behind a feature flag is checked too.
+ # The release's own build step, with placeholder digests: pin, package,
+ # lint, and verify each of the chart's check renders (for the switch
+ # chart, defaults and the optional workloads on), so an image behind a
+ # feature flag is checked too.
run: |
- python3 scripts/pin_chart_images.py charts > "$RUNNER_TEMP/charts.json"
- for chart in $(jq -r '.[].chart' "$RUNNER_TEMP/charts.json"); do
- path="$(jq -r --arg c "$chart" '.[] | select(.chart == $c) | .path' "$RUNNER_TEMP/charts.json")"
- python3 scripts/pin_chart_images.py pin --chart "$chart" \
- --registry ghcr.io/render-check --version 0.0.0-check --placeholder-digests
- renders="$(jq -c --arg c "$chart" '.[] | select(.chart == $c) | .renders[]' "$RUNNER_TEMP/charts.json")"
- while read -r files; do
- args=()
- for f in $(jq -r '.[]' <<<"$files"); do args+=(-f "$f"); done
- # Linted with the check values: a chart with a values schema
- # does not lint without the values it requires.
- helm lint "$path" "${args[@]}"
- helm template switch "$path" "${args[@]}" \
- | python3 scripts/pin_chart_images.py verify --chart "$chart" --registry ghcr.io/render-check
- done <<<"$renders"
- done
+ scripts/release_charts.sh build ghcr.io/render-check 0.0.0-check "$RUNNER_TEMP/chart" --placeholder-digests
gateway:
name: Gateway UI
diff --git a/.github/workflows/switch-release.yml b/.github/workflows/switch-release.yml
index 72729dd05..71d8a63ba 100644
--- a/.github/workflows/switch-release.yml
+++ b/.github/workflows/switch-release.yml
@@ -1,9 +1,9 @@
name: switch release
# Builds and publishes the Switch core stack — the switch-core, gateway, setup
-# and hosted-controller container images, the Helm chart, and the standalone
-# Docker Compose file (as an OCI artifact) — so adopters can deploy without
-# building from source.
+# and switch-hosted-controller container images, the switch and
+# switch-hosted-controller Helm charts, and the standalone Docker Compose file
+# (as an OCI artifact) — so adopters can deploy without building from source.
# See RELEASING.md for the full procedure.
#
# Trigger by pushing a tag like `switch-v0.2.0` (the version after `switch-v`
@@ -29,14 +29,17 @@ name: switch release
# prunes old ones. No compose artifact: Switch Console bundles that from
# releases only.
#
-# Build once, pin by digest: the chart is packaged only after the images are
-# pushed, with each image's registry digest written into its values.yaml
-# (scripts/pin_chart_images.py). Pinning the chart therefore pins the exact
-# images, and what runs is reproducible from that one pin. The run summary and
-# the `release-pins` artifact carry every digest, chart included. A version is
-# published once: a run (or re-run) whose chart version already exists in the
-# registry fails before building, since a rebuilt chart would point the same
-# version at different digests.
+# Build once, pin by digest: the charts are packaged only after the images are
+# pushed, with each image's registry digest written into its chart's values.yaml
+# (scripts/pin_chart_images.py, scripts/release_charts.sh). Pinning a chart
+# therefore pins its exact images, and what runs is reproducible from that one
+# pin. The run summary and the `release-pins` artifact carry every digest,
+# charts included. A version is published once: a run whose chart version
+# already exists in the registry fails before building, since a rebuilt chart
+# would point the same version at different digests. Re-running only a failed
+# chart job reuses the same image digests, so it may finish publishing a version
+# whose charts were partly pushed. Dev chart builds also move a `main` tag
+# that development follows.
#
# Artifacts go to GHCR by default, and the packages are public alongside the
# repo. REGISTRY / IMAGE_NAMESPACE are the only things to change to retarget
@@ -182,7 +185,7 @@ jobs:
uses: azure/setup-helm@1a275c3b69536ee54be43f2070a358922e12c8d4 # v4.3.1
- name: Refuse to republish a version
- # Checked before anything is built; see scripts/refuse_published_chart.sh.
+ # Checked before anything is built; see scripts/release_charts.sh.
if: ${{ steps.version.outputs.push == 'true' }}
env:
NAMESPACE: ${{ steps.version.outputs.namespace }}
@@ -190,9 +193,7 @@ jobs:
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | \
helm registry login "$REGISTRY" -u "${{ github.actor }}" --password-stdin
- for chart in $(python3 scripts/pin_chart_images.py charts | jq -r '.[].chart'); do
- scripts/refuse_published_chart.sh "oci://$REGISTRY/$NAMESPACE/charts/$chart" "$VERSION"
- done
+ scripts/release_charts.sh refuse-published "oci://$REGISTRY/$NAMESPACE" "$VERSION"
# The published compose artifact declares its stack-compose range twice:
# in the file (for whoever has already downloaded it) and as an OCI
@@ -328,9 +329,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- with:
- # To ask, before moving the dev `main` tag, whether this is still main's head.
- fetch-depth: 0
- name: Set up Helm
uses: azure/setup-helm@1a275c3b69536ee54be43f2070a358922e12c8d4 # v4.3.1
@@ -364,8 +362,8 @@ jobs:
- name: Pin, lint, package and check each chart
# A dry run pushed no images, so it pins placeholder digests: the pin
- # and the checks still run, and nothing is published. The check renders
- # the .tgz that is about to be pushed, not the source tree.
+ # and the checks still run, and nothing is published. The checks lint
+ # and render the .tgz that is about to be pushed.
run: |
if [ "${{ needs.prep.outputs.push }}" = true ]; then
source=(--digests-dir "$RUNNER_TEMP/digests")
@@ -373,24 +371,7 @@ jobs:
source=(--placeholder-digests)
fi
python3 scripts/pin_chart_images.py charts > "$RUNNER_TEMP/charts.json"
- for chart in $(jq -r '.[].chart' "$RUNNER_TEMP/charts.json"); do
- path="$(jq -r --arg c "$chart" '.[] | select(.chart == $c) | .path' "$RUNNER_TEMP/charts.json")"
- python3 scripts/pin_chart_images.py pin --chart "$chart" \
- --registry "$REGISTRY/$NAMESPACE" --version "$VERSION" "${source[@]}"
- git diff -- "$path/values.yaml"
- helm package "$path" --version "$VERSION" --app-version "$VERSION" \
- --destination "$RUNNER_TEMP/chart"
- renders="$(jq -c --arg c "$chart" '.[] | select(.chart == $c) | .renders[]' "$RUNNER_TEMP/charts.json")"
- while read -r files; do
- args=()
- for f in $(jq -r '.[]' <<<"$files"); do args+=(-f "$f"); done
- # Linted with the check values: a chart with a values schema
- # does not lint without the values it requires.
- helm lint "$path" "${args[@]}"
- helm template switch "$RUNNER_TEMP/chart/$chart-$VERSION.tgz" "${args[@]}" \
- | python3 scripts/pin_chart_images.py verify --chart "$chart" --registry "$REGISTRY/$NAMESPACE"
- done <<<"$renders"
- done
+ scripts/release_charts.sh build "$REGISTRY/$NAMESPACE" "$VERSION" "$RUNNER_TEMP/chart" "${source[@]}"
- name: Log in to GHCR (Helm OCI)
if: ${{ needs.prep.outputs.push == 'true' }}
@@ -401,21 +382,12 @@ jobs:
- name: Push the charts
id: push
if: ${{ needs.prep.outputs.push == 'true' }}
+ # prep refused a published version, but a re-run of this job alone
+ # skips prep. The script checks every chart before pushing any, and
+ # keeps a chart a failed earlier attempt already pushed with these
+ # same images, so re-running the job finishes a half-published version.
run: |
- flags=""
- for chart in $(jq -r '.[].chart' "$RUNNER_TEMP/charts.json"); do
- # prep refused a published version, but a re-run of this job alone
- # skips prep, so ask again right before pushing.
- scripts/refuse_published_chart.sh "oci://$REGISTRY/$NAMESPACE/charts/$chart" "$VERSION"
- helm push "$RUNNER_TEMP/chart/$chart-$VERSION.tgz" "oci://$REGISTRY/$NAMESPACE/charts" 2>&1 \
- | tee "$RUNNER_TEMP/helm-push.log"
- digest="$(sed -n 's/^Digest: *//p' "$RUNNER_TEMP/helm-push.log")"
- if [[ ! "$digest" =~ ^sha256:[0-9a-f]{64}$ ]]; then
- echo "::error::helm push of $chart printed no chart digest; got '$digest'"
- exit 1
- fi
- flags="$flags --chart-digest $chart=$digest"
- done
+ flags="$(scripts/release_charts.sh publish "oci://$REGISTRY/$NAMESPACE" "$VERSION" "$RUNNER_TEMP/chart")"
echo "chart_digest_flags=$flags" >> "$GITHUB_OUTPUT"
- name: Record the pins
diff --git a/RELEASING.md b/RELEASING.md
index 42ac94e68..c7245eaae 100644
--- a/RELEASING.md
+++ b/RELEASING.md
@@ -121,7 +121,8 @@ building from source at deploy time.
the run moves their `main` tag to its build, but only while the commit it
built is still the tip of main, so a slower run cannot move it backwards. An
environment that follows main (development, through ArgoCD) tracks that tag
- and resolves it to a digest; every other environment pins a version.
+ and resolves it to a digest; every other environment pins a version. The
+ cleanup never deletes the build `main` points at.
- **Version** `<next patch>-dev.<run number>.g<short sha>`, e.g.
`0.29.1-dev.512.g7f0c803` after `switch-v0.29.0`: one patch above the newest
release tag on main, so a dev build sorts after the release it contains and
@@ -150,8 +151,10 @@ switchCore:
```
and the same for `gateway` and `setup`. The hosted controller chart
-(`charts/switch-hosted-controller`) is pinned the same way, through its
-`image.repository` and `image.digest`. So pinning a chart pins the exact
+(`charts/switch-hosted-controller`) is pinned through `image.repository` and
+`image.digest` instead, so its image renders as
+`ghcr.io/<owner>/switch-hosted-controller@sha256:…`, by digest with no tag.
+So pinning a chart pins the exact
images: a deployment sets no image values, and what runs is reproducible from
the chart pin alone. The tag in front of the digest is for people; the runtime
pulls by digest, and the chart pulls a digest-pinned image `IfNotPresent` (any
@@ -160,9 +163,14 @@ pin. A mirror set with `global.imageRegistry` keeps the pin as long as it copies
images byte for byte, which registry-to-registry copies do.
**A version is published once.** Before building, the release asks the registry
-whether the chart version exists and fails if it does (and asks again right
-before pushing the chart). Builds are not bit-for-bit reproducible, so a re-run
-would point the same version at different digests. Cut a new version instead.
+whether any chart already has the version and fails if one does. Builds are not
+bit-for-bit reproducible, so a re-run of the whole release would point the same
+version at different digests. Cut a new version instead. If only a chart push
+failed, re-run just the failed chart job: it reuses the run's image digests,
+checks every chart before pushing any, keeps a chart already pushed with those
+same images, and pushes the rest. A chart already published with any other
+images fails it. `scripts/release_charts.sh` does the packaging, checks and
+push, and PR CI runs its build step with placeholder digests.
Every publishing run lists every digest (the charts' own included) in its
run summary, and uploads them as a `release-pins` artifact (`release-pins.json`)
diff --git a/core/tests/test_pin_chart_images.py b/core/tests/test_pin_chart_images.py
index 617ceb968..f74643cd0 100644
--- a/core/tests/test_pin_chart_images.py
+++ b/core/tests/test_pin_chart_images.py
@@ -314,3 +314,35 @@ def test_prune_deletes_old_builds_beyond_the_kept_ones_with_their_platform_manif
"b1",
}
assert len(kept) == 4 # two indexes and both of their platform manifests
+
+
+def test_prune_never_deletes_the_build_main_points_at() -> None:
+ builds = [_version(NOW - timedelta(days=40 + n), f"b{n}") for n in range(4)]
+ builds.append(_version(NOW - timedelta(days=90), "b-stale", "main"))
+
+ doomed = prune_dev_packages.doomed(builds, NOW, timedelta(days=30), keep=2)
+
+ assert {tag for v in doomed for tag in v["metadata"]["container"]["tags"]} == {
+ "b2",
+ "b3",
+ }
+
+
+def test_the_pin_style_comes_from_the_chart() -> None:
+ styles = {name: chart.pin_style for name, chart in pin_chart_images.CHARTS.items()}
+ assert styles == {
+ "switch": "tagged",
+ "switch-hosted-controller": "repository-digest",
+ }
+
+
+def test_verify_ignores_a_third_party_image_sharing_a_first_party_name() -> None:
+ rendered = "---\n".join(
+ [
+ _pod(_pinned("switch-hosted-controller")),
+ _pod("docker.io/envoyproxy/gateway:v1"),
+ ]
+ )
+ assert pin_chart_images.verify(
+ "switch-hosted-controller", rendered, "ghcr.io/acme"
+ ) == [_pinned("switch-hosted-controller")]
diff --git a/scripts/pin_chart_images.py b/scripts/pin_chart_images.py
index be20c5e45..a16f29173 100644
--- a/scripts/pin_chart_images.py
+++ b/scripts/pin_chart_images.py
@@ -14,6 +14,8 @@ Two charts are published:
* `switch-hosted-controller` (deploy/hosted/chart): the controller that runs
Switch cloud machines, and its one image.
+Packaging and publishing them is scripts/release_charts.sh, which reads `charts`.
+
`IMAGES` and `CHARTS` below are the only lists. The release workflow builds its
image matrix from `images` and packages the charts `charts` names, and every
other subcommand refuses a set of digests that is not exactly `IMAGES`, so
@@ -23,16 +25,18 @@ fails loudly.
Subcommands:
* `images` prints the image build matrix as JSON; `charts` prints the charts.
-* `pin --chart <name>` rewrites that chart's `values.yaml` in place. The switch
- chart gets `global.imageRegistry` and `<name>:<version>@sha256:…` per image
- (it pulls a digest-pinned image `IfNotPresent` on its own); the hosted
- controller chart gets `image.repository` and `image.digest`. The edit is
+* `pin --chart <name>` rewrites that chart's `values.yaml` in place, in the
+ chart's `pin_style`. The switch chart gets `global.imageRegistry` and
+ `<name>:<version>@sha256:…` per image (it pulls a digest-pinned image
+ `IfNotPresent` on its own); the hosted controller chart gets
+ `image.repository` and `image.digest`, so its image renders as
+ `<registry>/<name>@sha256:…`, with no tag. The edit is
line-based so the file's comments, which are the chart's documentation under
`helm show values`, survive.
* `verify --chart <name>` reads `helm template` output and fails unless every
image of that chart — recognised by repository name, whatever registry it
names — renders from the pinning registry and by digest, all of them are
- present, and the pinning registry serves no image the chart should not have.
+ present, and the chart renders nothing else from the pinning registry.
* `record` writes the pins (images and charts) as JSON for whatever promotes the
build next, and as a Markdown table for the run summary.
@@ -62,7 +66,7 @@ import sys
from collections.abc import Iterator
from dataclasses import dataclass
from pathlib import Path
-from typing import Any
+from typing import Any, Literal
import yaml
@@ -70,8 +74,8 @@ import yaml
@dataclass(frozen=True)
class Image:
chart: str
- # Top-level values key holding the image: `<key>.image` in the switch
- # chart, `<key>.repository` / `<key>.digest` in the hosted controller's.
+ # Top-level values key holding the image; what goes under it is the
+ # chart's `pin_style`.
values_key: str
dockerfile: str
context: str
@@ -80,6 +84,12 @@ class Image:
@dataclass(frozen=True)
class Chart:
path: str
+ # "tagged": `global.imageRegistry`, and `<key>.image: <name>:<version>@<digest>`.
+ # "repository-digest": `<key>.repository: <registry>/<name>`, `<key>.digest`.
+ pin_style: Literal["tagged", "repository-digest"]
+ # Whether `helm lint` passes on the defaults alone; a chart whose values
+ # schema requires values is linted only with its renders' values.
+ lint_defaults: bool
# Each inner list is one `helm template` render (its -f files) that
# `verify` checks: placeholder secrets, and the optional workloads on.
renders: tuple[tuple[str, ...], ...]
@@ -88,6 +98,8 @@ class Chart:
CHARTS: dict[str, Chart] = {
"switch": Chart(
"deploy/remote/helm/switch",
+ "tagged",
+ True,
(
("deploy/remote/helm/render-check-values.yaml",),
(
@@ -97,7 +109,10 @@ CHARTS: dict[str, Chart] = {
),
),
"switch-hosted-controller": Chart(
- "deploy/hosted/chart", (("deploy/hosted/render-check-values.yaml",),)
+ "deploy/hosted/chart",
+ "repository-digest",
+ False,
+ (("deploy/hosted/render-check-values.yaml",),),
),
}
@@ -211,7 +226,7 @@ def pin(
lines = values_text.splitlines(keepends=True)
expected: dict[tuple[str, str], str] = {}
- if chart == "switch":
+ if CHARTS[chart].pin_style == "tagged":
expected[("global", "imageRegistry")] = registry
for name, image in images.items():
expected[(image.values_key, "image")] = f"{name}:{version}@{digests[name]}"
@@ -272,7 +287,7 @@ def verify(chart: str, rendered: str, registry: str) -> list[str]:
problems.append(f"{ref}: not from {registry}")
if not re.search(r"@sha256:[0-9a-f]{64}$", ref):
problems.append(f"{ref}: not pinned by digest")
- elif ref.startswith(prefix) or name in IMAGES:
+ elif ref.startswith(prefix):
problems.append(
f"{ref}: not an image of the {chart} chart; add it to IMAGES if it should be"
)
@@ -410,7 +425,12 @@ def main(argv: list[str]) -> int:
print(
json.dumps(
[
- {"chart": name, "path": chart.path, "renders": chart.renders}
+ {
+ "chart": name,
+ "path": chart.path,
+ "lint_defaults": chart.lint_defaults,
+ "renders": chart.renders,
+ }
for name, chart in CHARTS.items()
]
)
diff --git a/scripts/prune_dev_packages.py b/scripts/prune_dev_packages.py
index a27cb050e..dfdcd482b 100644
--- a/scripts/prune_dev_packages.py
+++ b/scripts/prune_dev_packages.py
@@ -6,7 +6,8 @@ Every push to main publishes the images and chart under `<owner>/dev/…`
builds, so pruning them cannot touch a release. A version is deleted when it is
older than `--max-age-days` AND older than the `--keep` newest tagged builds,
so a quiet month does not empty the package and leave the dev environment
-pinned to a digest that no longer exists.
+pinned to a digest that no longer exists. The version a chart's moving `main`
+tag points at is never deleted, however old: development follows that tag.
A multi-arch image is an index plus one untagged manifest per platform, pushed
moments before it. The cutoff is set an hour before the oldest kept build, so a
@@ -61,18 +62,23 @@ def _created(version: dict) -> datetime:
return datetime.fromisoformat(version["created_at"].replace("Z", "+00:00"))
+FOLLOWED_TAG = "main"
+
+
+def _tags(version: dict) -> list[str]:
+ return version.get("metadata", {}).get("container", {}).get("tags") or []
+
+
def doomed(
versions: list[dict], now: datetime, max_age: timedelta, keep: int
) -> list[dict]:
- tagged = sorted(
- (v for v in versions if v.get("metadata", {}).get("container", {}).get("tags")),
- key=_created,
- reverse=True,
- )
+ tagged = sorted((v for v in versions if _tags(v)), key=_created, reverse=True)
if len(tagged) <= keep:
return []
cutoff = min(now - max_age, _created(tagged[keep - 1]) - timedelta(hours=1))
- return [v for v in versions if _created(v) < cutoff]
+ return [
+ v for v in versions if _created(v) < cutoff and FOLLOWED_TAG not in _tags(v)
+ ]
def main(argv: list[str]) -> int:
@@ -103,9 +109,7 @@ def main(argv: list[str]) -> int:
delete = doomed(versions, now, timedelta(days=args.max_age_days), args.keep)
print(f"{package}: {len(versions)} versions, deleting {len(delete)}")
for version in delete:
- tags = version.get("metadata", {}).get("container", {}).get("tags") or [
- "<untagged>"
- ]
+ tags = _tags(version) or ["<untagged>"]
print(
f" {'would delete' if args.dry_run else 'delete'} {version['name']} {','.join(tags)} {version['created_at']}"
)
diff --git a/scripts/refuse_published_chart.sh b/scripts/refuse_published_chart.sh
deleted file mode 100755
index d0d693450..000000000
--- a/scripts/refuse_published_chart.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/usr/bin/env bash
-# Fail unless a chart version is NOT yet in the registry.
-#
-# A version is published once: a re-run of the release rebuilds the images
-# (builds are not bit-for-bit reproducible) and would push the chart again
-# under the same version, pointing at new digests, so a version pin would no
-# longer mean what it meant. Only helm's plain "not found" counts as
-# unpublished; an auth or network error fails rather than reading as
-# permission to push. Needs `helm registry login` done first.
-#
-# Usage: refuse_published_chart.sh oci://<registry>/<namespace>/charts/<name> <version>
-set -euo pipefail
-
-chart="${1:?chart reference required}"
-version="${2:?version required}"
-
-if out="$(helm show chart "$chart" --version "$version" 2>&1)"; then
- echo "::error::$chart:$version is already published. A version is published once; cut a new one."
- exit 1
-fi
-if ! grep -q ': not found$' <<<"$out"; then
- echo "::error::Could not tell whether $chart:$version exists: $out"
- exit 1
-fi
-echo "$chart:$version is not published yet"
diff --git a/scripts/release_charts.sh b/scripts/release_charts.sh
new file mode 100755
index 000000000..1f17ecd41
--- /dev/null
+++ b/scripts/release_charts.sh
@@ -0,0 +1,140 @@
+#!/usr/bin/env bash
+# Package, check and publish the charts scripts/pin_chart_images.py names.
+#
+# One script for the release and PR CI, so PR CI checks exactly what the
+# release builds.
+#
+# refuse-published <oci-base> <version>
+# Fail if any chart already has <version>. The release runs this before
+# building anything.
+# build <registry> <version> <out-dir> (--digests-dir <dir> | --placeholder-digests)
+# For each chart: pin values.yaml, package it into <out-dir>, lint the
+# package (defaults where the chart allows, then each render's values),
+# and render the package for each render through `verify`. The image refs
+# the renders produce go to <out-dir>/<chart>.refs.
+# publish <oci-base> <version> <out-dir>
+# Push what `build` packaged and print `--chart-digest <chart>=<digest>`
+# for each chart. Every chart is checked before any is pushed. A chart
+# already at <version> is accepted only if it renders exactly the images
+# this build pinned, which is what a re-run after a failed push sees; its
+# registry digest is used. Anything else at that version fails the run.
+#
+# <oci-base> is oci://<registry>/<namespace>; charts live under <oci-base>/charts.
+# Whether a version exists is decided only by helm's plain "not found"; an auth
+# or network error fails rather than reading as permission to push. GHCR
+# answers "not found" for a package that does not exist yet as well. Needs
+# `helm registry login` done first, for refuse-published and publish.
+set -euo pipefail
+
+charts_json() { python3 scripts/pin_chart_images.py charts; }
+field() { jq -r --arg c "$1" ".[] | select(.chart == \$c) | $2" <<<"$CHARTS"; }
+
+# Prints "published" or "absent"; exits 1 when it cannot tell.
+status() {
+ local ref="$1" version="$2" out
+ if out="$(helm show chart "$ref" --version "$version" 2>&1)"; then
+ echo published
+ elif grep -q ': not found$' <<<"$out"; then
+ echo absent
+ else
+ echo "::error::Could not tell whether $ref:$version exists: $out" >&2
+ return 1
+ fi
+}
+
+# Renders <chart-ref> once per render of <chart> and prints the sorted image
+# refs `verify` accepts.
+rendered_refs() {
+ local chart="$1" ref="$2" registry="$3" files args
+ while read -r files; do
+ args=()
+ for f in $(jq -r '.[]' <<<"$files"); do args+=(-f "$f"); done
+ helm template switch "$ref" "${args[@]}" \
+ | python3 scripts/pin_chart_images.py verify --chart "$chart" --registry "$registry"
+ done < <(field "$chart" '.renders[] | tojson') | sort -u
+}
+
+CHARTS="$(charts_json)"
+command="${1:?usage: release_charts.sh refuse-published|build|publish ...}"
+shift
+
+case "$command" in
+refuse-published)
+ base="${1:?oci base required}" version="${2:?version required}"
+ for chart in $(jq -r '.[].chart' <<<"$CHARTS"); do
+ state="$(status "$base/charts/$chart" "$version")"
+ if [ "$state" = published ]; then
+ echo "::error::$base/charts/$chart:$version is already published. A version is published once; cut a new one."
+ exit 1
+ fi
+ echo "$base/charts/$chart:$version is not published yet"
+ done
+ ;;
+
+build)
+ registry="${1:?registry required}" version="${2:?version required}" out="${3:?out dir required}"
+ shift 3
+ mkdir -p "$out"
+ for chart in $(jq -r '.[].chart' <<<"$CHARTS"); do
+ path="$(field "$chart" .path)"
+ python3 scripts/pin_chart_images.py pin --chart "$chart" \
+ --registry "$registry" --version "$version" "$@"
+ git --no-pager diff -- "$path/values.yaml"
+ helm package "$path" --version "$version" --app-version "$version" --destination "$out"
+ tgz="$out/$chart-$version.tgz"
+ if [ "$(field "$chart" .lint_defaults)" = true ]; then
+ helm lint "$tgz"
+ fi
+ while read -r files; do
+ args=()
+ for f in $(jq -r '.[]' <<<"$files"); do args+=(-f "$f"); done
+ helm lint "$tgz" "${args[@]}"
+ done < <(field "$chart" '.renders[] | tojson')
+ rendered_refs "$chart" "$tgz" "$registry" | tee "$out/$chart.refs"
+ done
+ ;;
+
+publish)
+ base="${1:?oci base required}" version="${2:?version required}" out="${3:?out dir required}"
+ registry="${base#oci://}"
+ to_push=()
+ flags=()
+ for chart in $(jq -r '.[].chart' <<<"$CHARTS"); do
+ ref="$base/charts/$chart"
+ state="$(status "$ref" "$version")"
+ case "$state" in
+ absent) to_push+=("$chart") ;;
+ published)
+ pulled="$(mktemp -d)"
+ log="$(helm pull "$ref" --version "$version" --destination "$pulled" 2>&1)"
+ digest="$(sed -n 's/^Digest: *//p' <<<"$log")"
+ rendered_refs "$chart" "$pulled/$chart-$version.tgz" "$registry" >"$pulled/refs"
+ if ! diff "$pulled/refs" "$out/$chart.refs" >&2; then
+ echo "::error::$ref:$version is already published with other images than this build's. A version is published once; cut a new one." >&2
+ exit 1
+ fi
+ echo "$ref:$version is already published with this build's images; keeping it." >&2
+ flags+=(--chart-digest "$chart=$digest")
+ ;;
+ esac
+ done
+ for chart in ${to_push[@]+"${to_push[@]}"}; do
+ log="$(helm push "$out/$chart-$version.tgz" "$base/charts" 2>&1)"
+ echo "$log" >&2
+ digest="$(sed -n 's/^Digest: *//p' <<<"$log")"
+ flags+=(--chart-digest "$chart=$digest")
+ done
+ for ((i = 1; i < ${#flags[@]}; i += 2)); do
+ if [[ ! "${flags[i]#*=}" =~ ^sha256:[0-9a-f]{64}$ ]]; then
+ echo "::error::no chart digest for ${flags[i]%%=*}; got '${flags[i]#*=}'" >&2
+ exit 1
+ fi
+ done
+ echo "${flags[*]}"
+ ;;
+
+*)
+ echo "unknown command $command" >&2
+ exit 2
+ ;;
+esac