mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-02 21:30:46 +09:00
[Backport release-1.6] perf(ci): build release lines from their own cache, in parallel (#5490)
## What this PR does Port of #5484 to release-1.6, same reason as for 1.7: `build-release.yaml` and `tags.yaml` run from the line's own copy. This line has no arm64 build and no buildx setup in `tags.yaml`, so only the amd64 cache and the parallel build apply. `packages/core/platform` waits for the rest here, because it writes `backupstrategy-controller` values.yaml (seaweedfs is not a unit on this line). `cache-args` took an optional per-call cache tag that no caller passes, so it takes main's form. Tests that live in files this line doesn't have went to `hack/release-build-cache-contract.bats`. ### Downstream repositories - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: ### Release note ```release-note perf(ci): release-1.6 builds images from its own build cache, several units at a time ```
This commit is contained in:
9 files changed
+221
-24
No files matched your search
@@ -3,7 +3,7 @@ name: Build release line
|
||||
# Publishes the per-line equivalent of what build-main.yaml publishes for main:
|
||||
# every image tagged with the line's branch name, and the whole packages tree as
|
||||
# the `cozystack-packages:<branch>` OCI artifact with each image reference
|
||||
# digest-pinned to the images this run just pushed (`make build` ->
|
||||
# digest-pinned to the images this run just pushed (`make build-parallel` ->
|
||||
# packages/core/installer image-packages does the `flux push artifact`).
|
||||
#
|
||||
# Why it exists: pull-requests.yaml overlays image refs for every package a PR
|
||||
@@ -16,9 +16,9 @@ name: Build release line
|
||||
# artifact published per line, the overlay reads the PR's own base branch and the
|
||||
# generations match.
|
||||
#
|
||||
# Cost: one full `make build` per push to a maintained line, i.e. per merged
|
||||
# backport. That is the price of release-line PRs testing their line's tip rather
|
||||
# than its last release.
|
||||
# Cost: one full `make build-parallel` per push to a maintained line, i.e. per
|
||||
# merged backport. That is the price of release-line PRs testing their line's
|
||||
# tip rather than its last release.
|
||||
|
||||
env:
|
||||
# Same per-CI registry as pull-requests.yaml and build-main.yaml.
|
||||
@@ -97,17 +97,19 @@ jobs:
|
||||
DOCKER_CONFIG: ${{ runner.temp }}/.docker
|
||||
|
||||
- name: Build line images and publish the packages artifact
|
||||
run: make build
|
||||
run: make build-parallel
|
||||
env:
|
||||
DOCKER_CONFIG: ${{ runner.temp }}/.docker
|
||||
BUILDER: ${{ steps.buildx.outputs.name }}
|
||||
# READ the shared mode=max cache, never write it. CACHE_REGISTRY/<img>:
|
||||
# buildcache is a single ref per image and build-main.yaml is
|
||||
# deliberately its only writer, serialized, precisely so concurrent
|
||||
# builds cannot race on the cache manifest (the 409 collision class
|
||||
# #2711 fixed for image tags). A line build can overlap a main build, so
|
||||
# writing here would reintroduce exactly that race.
|
||||
WRITE_CACHE: '0'
|
||||
# The line keeps a mode=max cache of its own, which its rc builds in
|
||||
# tags.yaml import too: main's cache (:buildcache, written only by
|
||||
# build-main.yaml) misses more with every Dockerfile main changes after
|
||||
# the line branched. The concurrency group above runs one build per
|
||||
# line, so this is the only writer of the line's ref, and main's ref is
|
||||
# only read.
|
||||
CACHE_TAG: buildcache-${{ github.ref_name }}
|
||||
CACHE_FROM_TAGS: buildcache-${{ github.ref_name }} buildcache
|
||||
WRITE_CACHE: '1'
|
||||
# Floating per-line handle: images and the packages artifact both land
|
||||
# under the branch name (e.g. :release-1.6). Versioned and :latest tags
|
||||
# belong to releases (tags.yaml / finalize), never to this workflow.
|
||||
|
||||
@@ -20,8 +20,8 @@ jobs:
|
||||
prepare-release:
|
||||
name: Prepare Release
|
||||
# Ephemeral pool, not the dedicated self-hosted runner (#2937 decommission).
|
||||
# Serial `make build` + `make assets` is CPU-bound, so use the large shape —
|
||||
# same as the main cache warmer (build-main.yaml).
|
||||
# `make build-parallel` runs several image builds at once, so use the large
|
||||
# shape, as the main cache warmer (build-main.yaml) does.
|
||||
runs-on: oracle-vm-24cpu-96gb-x86-64
|
||||
timeout-minutes: 120
|
||||
outputs:
|
||||
@@ -174,13 +174,16 @@ jobs:
|
||||
# Build project artifacts
|
||||
- name: Build
|
||||
if: steps.check_release.outputs.release_exists == 'false'
|
||||
run: make build
|
||||
run: make build-parallel
|
||||
env:
|
||||
DOCKER_CONFIG: ${{ runner.temp }}/.docker
|
||||
# Cache manifests are registry-portable (oci-mediatypes=true) — read
|
||||
# them from OCIR while pushing images to GHCR. A missing :buildcache
|
||||
# ref 404s harmlessly into a cold build.
|
||||
CACHE_REGISTRY: iad.ocir.io/idyksih5sir9/cozystack
|
||||
# The line's own cache, which build-release.yaml writes on every push
|
||||
# to the line, and main's.
|
||||
CACHE_FROM_TAGS: buildcache-release-${{ steps.tag.outputs.line }} buildcache
|
||||
# Push the release tag as :IMAGE_TAG (vX.Y.Z-rc.N) and the component
|
||||
# versions as :<component-ver>. Deliberately NO PUBLISH_FLOATING: this
|
||||
# job only ever runs for a prerelease (a stable tag is rejected or
|
||||
|
||||
@@ -44,6 +44,15 @@ build: build-deps
|
||||
make -C packages/core/installer image
|
||||
make manifests
|
||||
|
||||
# The same images as `build`, with the units in hack/build-matrix.sh built
|
||||
# BUILD_JOBS at a time. The installer goes last because it packs the tree every
|
||||
# other unit stamps.
|
||||
build-parallel: build-deps
|
||||
hack/build-units.sh
|
||||
make -C packages/core/talos image
|
||||
make -C packages/core/installer image
|
||||
make manifests
|
||||
|
||||
manifests:
|
||||
mkdir -p _out/assets
|
||||
cat internal/crdinstall/manifests/*.yaml > _out/assets/cozystack-crds.yaml
|
||||
|
||||
@@ -69,6 +69,7 @@ Root targets (run from the repo root):
|
||||
|
||||
```bash
|
||||
make build # Build all Docker images (needs: docker, skopeo, jq, gh, helm, yq, GNU tar/sed/awk)
|
||||
make build-parallel # The same images, BUILD_JOBS units at a time (hack/build-units.sh); what CI runs for release lines
|
||||
make unit-tests # Run all unit tests (Helm, BATS, Go, etc.)
|
||||
make generate # Code generation (hack/update-codegen.sh) — CRDs, DeepCopy, clients, RBAC
|
||||
make manifests # Generate CRD manifests and operator YAML variants
|
||||
@@ -93,6 +94,8 @@ Build environment variables:
|
||||
- `REGISTRY` — Docker registry (default `ghcr.io/cozystack/cozystack`).
|
||||
- `PUSH=1` / `LOAD=0` — control buildx push/load behaviour.
|
||||
- `LOAD=1 PUSH=0` — load images locally instead of pushing.
|
||||
- `CACHE_FROM_TAGS` — every cache a build imports; defaults to `CACHE_TAG` (`buildcache`), the one it writes under `WRITE_CACHE=1`. A release line imports its own `buildcache-release-X.Y`, which `build-release.yaml` writes on every push to the line, and main's `buildcache`, since main's Dockerfiles drift away from the line's.
|
||||
- `BUILD_JOBS` — how many units `make build-parallel` builds at a time. Defaults to 4. `packages/core/platform` waits for the others, because it writes `packages/system/backupstrategy-controller/values.yaml`, which the backupstrategy-controller build rewrites too.
|
||||
|
||||
### Values schema generation
|
||||
|
||||
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
# Builds the image units hack/build-matrix.sh lists, BUILD_JOBS at a time.
|
||||
# Several build at once, so every output line starts with its unit.
|
||||
#
|
||||
# Run from the repository root. Talos and the installer are not in the list;
|
||||
# `make build-parallel` builds them afterwards.
|
||||
set -euo pipefail
|
||||
|
||||
jobs="${BUILD_JOBS:-4}"
|
||||
|
||||
# It rewrites packages/system/backupstrategy-controller/values.yaml, which the
|
||||
# backupstrategy-controller build rewrites too, so it waits until the others
|
||||
# are done.
|
||||
last=packages/core/platform
|
||||
|
||||
units="$(hack/build-matrix.sh | jq --raw-output '.[]')"
|
||||
[ -n "$units" ]
|
||||
|
||||
build() {
|
||||
set -o pipefail
|
||||
if make -C "$1" image 2>&1 | sed --unbuffered "s|^|$1: |"; then
|
||||
echo "built $1"
|
||||
else
|
||||
echo "::error::$1 failed"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
export -f build
|
||||
|
||||
grep --invert-match --line-regexp --fixed-strings "$last" <<< "$units" |
|
||||
xargs --max-procs="$jobs" --max-args=1 bash -c 'build "$1"' _
|
||||
|
||||
if grep --quiet --line-regexp --fixed-strings "$last" <<< "$units"; then
|
||||
build "$last"
|
||||
fi
|
||||
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env bats
|
||||
# Unit tests for hack/build-units.sh, run against a fake unit list and a fake
|
||||
# `make` that records when each unit starts and ends and how many build at once.
|
||||
#
|
||||
# Test-level EXIT traps replace Bats' own handler and hide failing TAP results.
|
||||
# Cleanup follows aborting assertions; see docs/agents/e2e-testing.md.
|
||||
|
||||
load test_helper
|
||||
|
||||
# Copies the script into an empty tree with a five-unit matrix and a fake make.
|
||||
# Every fake build takes half a second, so builds allowed to overlap do overlap.
|
||||
fake_tree() {
|
||||
work="$(mktemp -d)"
|
||||
mkdir -p "$work/hack" "$work/bin"
|
||||
cp hack/build-units.sh "$work/hack/"
|
||||
echo 0 > "$work/active"
|
||||
cat > "$work/hack/build-matrix.sh" <<'EOF'
|
||||
#!/bin/sh
|
||||
[ -z "${MATRIX_FAILS:-}" ] || exit 1
|
||||
echo '["packages/system/backupstrategy-controller","packages/core/platform","packages/apps/a","packages/apps/b","packages/apps/c"]'
|
||||
EOF
|
||||
cat > "$work/bin/make" <<'EOF'
|
||||
#!/bin/sh
|
||||
unit="$2"
|
||||
add() {
|
||||
flock "$WORK/lock" sh -c 'n=$(( $(cat "$1/active") + $2 )); echo "$n" > "$1/active"; echo "$n" >> "$1/running"' _ "$WORK" "$1"
|
||||
}
|
||||
echo "start $unit" >> "$WORK/order"
|
||||
echo "working on $unit"
|
||||
add 1
|
||||
[ "$unit" != "${HANG_UNIT:-}" ] || sleep 3
|
||||
if [ "$unit" = packages/core/platform ] && ! grep -qx 'end packages/system/backupstrategy-controller' "$WORK/order"; then
|
||||
echo "platform started while backupstrategy-controller was still building"
|
||||
exit 1
|
||||
fi
|
||||
sleep 0.5
|
||||
add -1
|
||||
if [ "$unit" = "${FAIL_UNIT:-}" ]; then
|
||||
echo "boom from $unit"
|
||||
exit 1
|
||||
fi
|
||||
echo "end $unit" >> "$WORK/order"
|
||||
EOF
|
||||
chmod +x "$work/hack/build-matrix.sh" "$work/bin/make"
|
||||
}
|
||||
|
||||
run_units() {
|
||||
run bash -c 'cd "$1" && WORK="$1" PATH="$1/bin:$PATH" BUILD_JOBS=2 timeout "${TIMEOUT:-60}" hack/build-units.sh' _ "$work"
|
||||
echo "$output"
|
||||
}
|
||||
|
||||
@test "every unit is built once, two at a time, and platform after backupstrategy-controller" {
|
||||
fake_tree
|
||||
run_units
|
||||
[ "$status" -eq 0 ]
|
||||
for unit in packages/system/backupstrategy-controller packages/core/platform packages/apps/a packages/apps/b packages/apps/c; do
|
||||
[ "$(grep -cx "start $unit" "$work/order")" -eq 1 ]
|
||||
[ "$(grep -cx "end $unit" "$work/order")" -eq 1 ]
|
||||
done
|
||||
[ "$(sort -n "$work/running" | tail -n 1)" -eq 2 ]
|
||||
rm -rf "$work"
|
||||
}
|
||||
|
||||
@test "a failing unit fails the build, its output is shown, and platform is not built" {
|
||||
fake_tree
|
||||
FAIL_UNIT=packages/apps/b run_units
|
||||
[ "$status" -ne 0 ]
|
||||
echo "$output" | grep -qF 'boom from packages/apps/b'
|
||||
if grep -qx 'start packages/core/platform' "$work/order"; then
|
||||
echo "FAIL: platform was built after another unit failed"; false
|
||||
fi
|
||||
rm -rf "$work"
|
||||
}
|
||||
|
||||
@test "a unit list that cannot be generated fails the build before anything is built" {
|
||||
fake_tree
|
||||
MATRIX_FAILS=1 run_units
|
||||
[ "$status" -ne 0 ]
|
||||
if [ -e "$work/order" ]; then echo "FAIL: units were built without a unit list"; false; fi
|
||||
rm -rf "$work"
|
||||
}
|
||||
|
||||
@test "a failing platform, built after the others, fails the build too" {
|
||||
fake_tree
|
||||
FAIL_UNIT=packages/core/platform run_units
|
||||
[ "$status" -ne 0 ]
|
||||
echo "$output" | grep -qF 'packages/core/platform: boom from packages/core/platform'
|
||||
rm -rf "$work"
|
||||
}
|
||||
|
||||
@test "a unit's output shows while it builds, so a job that times out keeps it" {
|
||||
fake_tree
|
||||
HANG_UNIT=packages/apps/a TIMEOUT=1 run_units
|
||||
[ "$status" -eq 124 ]
|
||||
echo "$output" | grep -qF 'packages/apps/a: working on packages/apps/a'
|
||||
rm -rf "$work"
|
||||
}
|
||||
+9
-5
@@ -28,7 +28,7 @@ IMAGE_TAG ?= dev
|
||||
PUBLISH_VERSIONED ?= 0
|
||||
PUBLISH_FLOATING ?= 0
|
||||
|
||||
# WRITE_CACHE=1 enables `--cache-to` (mode=max) to CACHE_REGISTRY/<img>:buildcache.
|
||||
# WRITE_CACHE=1 enables `--cache-to` (mode=max) to CACHE_REGISTRY/<img>:$(CACHE_TAG).
|
||||
# Default off: only the serialized main/release cache-warming build writes the
|
||||
# shared cache ref. PR builds keep it 0 and read-only, so concurrent PR builds
|
||||
# never race on the cache manifest (the 409 collisions PR #2711 fixed for tags).
|
||||
@@ -69,19 +69,23 @@ define image-tags
|
||||
--tag $(REGISTRY)/$(1):$(IMAGE_TAG)$(if $(filter 1,$(PUBLISH_VERSIONED)),$(if $(filter-out $(IMAGE_TAG),$(strip $(2))), --tag $(REGISTRY)/$(1):$(strip $(2))))$(if $(filter 1,$(PUBLISH_FLOATING)), --tag $(REGISTRY)/$(1):latest)
|
||||
endef
|
||||
|
||||
# cache-args <image-name> [<cache-tag>]
|
||||
# cache-args <image-name>
|
||||
# Expands to buildx cache flags for one image:
|
||||
# --cache-from is always emitted (a missing cache 404s harmlessly -> cold build)
|
||||
# --cache-to is emitted only when WRITE_CACHE=1 (main/release), writing mode=max
|
||||
# so ALL stages are cached -- including the multistage `builder` layers that
|
||||
# `--cache-to type=inline` could never export. oci-mediatypes + image-manifest
|
||||
# keep the cache manifest portable across registries (OCIR/ghcr/ECR).
|
||||
# <cache-tag> defaults to `buildcache`; pass an explicit tag for images that build
|
||||
# a distinct artifact per loop iteration (e.g. ubuntu-container-disk per k8s ver).
|
||||
# ignore-error: OCIR refuses a blob upload now and then, and a lost cache
|
||||
# write should cost the next build time, not fail this one.
|
||||
# CACHE_FROM_TAGS lists every cache a build imports; a release line imports its
|
||||
# own as well as main's, since main's Dockerfiles drift away from the line's.
|
||||
# $(comma) escapes the literal commas in the --cache-to value so make does not
|
||||
# mis-parse them as $(if ...) argument separators.
|
||||
comma := ,
|
||||
cache-args = --cache-from type=registry,ref=$(CACHE_REGISTRY)/$(1):$(if $(2),$(2),buildcache)$(if $(filter 1,$(WRITE_CACHE)), --cache-to type=registry$(comma)ref=$(CACHE_REGISTRY)/$(1):$(if $(2),$(2),buildcache)$(comma)mode=max$(comma)oci-mediatypes=true$(comma)image-manifest=true)
|
||||
CACHE_TAG ?= buildcache
|
||||
CACHE_FROM_TAGS ?= $(CACHE_TAG)
|
||||
cache-args = $(foreach t,$(CACHE_FROM_TAGS),--cache-from type=registry$(comma)ref=$(CACHE_REGISTRY)/$(1):$(t))$(if $(filter 1,$(WRITE_CACHE)), --cache-to type=registry$(comma)ref=$(CACHE_REGISTRY)/$(1):$(CACHE_TAG)$(comma)mode=max$(comma)oci-mediatypes=true$(comma)image-manifest=true$(comma)ignore-error=true)
|
||||
|
||||
ifeq ($(COZYSTACK_VERSION),)
|
||||
$(shell git remote add upstream https://github.com/cozystack/cozystack.git || true)
|
||||
|
||||
@@ -210,9 +210,10 @@
|
||||
|
||||
# It must not write the shared mode=max cache: that ref has exactly one
|
||||
# serialized writer (build-main.yaml) so concurrent builds cannot race on the
|
||||
# cache manifest. A line build can overlap a main build.
|
||||
printf '%s\n' "$code" | grep -qF "WRITE_CACHE: '0'" || {
|
||||
echo "build-release.yaml must set WRITE_CACHE: '0' — writing the shared" >&2
|
||||
echo ":buildcache ref races build-main.yaml on the cache manifest." >&2
|
||||
# cache manifest. A line build can overlap a main build, so it writes only
|
||||
# the line's own ref.
|
||||
printf '%s\n' "$code" | grep -qF 'CACHE_TAG: buildcache-${{ github.ref_name }}' || {
|
||||
echo "build-release.yaml must write only its line's cache ref — writing the" >&2
|
||||
echo "shared :buildcache ref races build-main.yaml on the cache manifest." >&2
|
||||
exit 1; }
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env bats
|
||||
# The cache settings of the release-line image builds. build-release.yaml runs
|
||||
# only on a push to a line and tags.yaml only on a tag, so no PR lane would
|
||||
# notice a change to them.
|
||||
#
|
||||
# Test-level EXIT traps replace Bats' own handler and hide failing TAP results.
|
||||
# Cleanup follows aborting assertions; see docs/agents/e2e-testing.md.
|
||||
|
||||
load test_helper
|
||||
|
||||
@test "CACHE_FROM_TAGS imports every cache it lists and the build still writes only CACHE_TAG" {
|
||||
pkg=packages/system/cozystack-controller
|
||||
out=$(make -n -C "$pkg" image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 WRITE_CACHE=1 CACHE_TAG=buildcache-release-1.6 'CACHE_FROM_TAGS=buildcache-release-1.6 buildcache')
|
||||
echo "$out" | grep -q -- '--cache-from type=registry,ref=[^ ]*/cozystack-controller:buildcache-release-1.6 '
|
||||
echo "$out" | grep -q -- '--cache-from type=registry,ref=[^ ]*/cozystack-controller:buildcache '
|
||||
echo "$out" | grep -q -- '--cache-to type=registry,ref=[^ ]*/cozystack-controller:buildcache-release-1.6,[^ ]*ignore-error=true'
|
||||
[ "$(echo "$out" | grep -o -- '--cache-to' | wc -l)" -eq 1 ]
|
||||
for write in '' WRITE_CACHE=0; do
|
||||
out=$(make -n -C "$pkg" image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 $write 'CACHE_FROM_TAGS=buildcache-release-1.6 buildcache')
|
||||
[ "$(echo "$out" | grep -o -- '--cache-from' | wc -l)" -eq 2 ]
|
||||
[ "$(echo "$out" | grep -o -- '--cache-to' | wc -l)" -eq 0 ]
|
||||
done
|
||||
out=$(make -n -C "$pkg" image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0)
|
||||
[ "$(echo "$out" | grep -o -- '--cache-from' | wc -l)" -eq 1 ]
|
||||
echo "$out" | grep -q -- '--cache-from type=registry,ref=[^ ]*/cozystack-controller:buildcache '
|
||||
}
|
||||
|
||||
@test "a push to a release line writes the line's cache and imports main's too" {
|
||||
step='.jobs["build-line"].steps[] | select(.name == "Build line images and publish the packages artifact")'
|
||||
wf=.github/workflows/build-release.yaml
|
||||
[ "$(yq "$step | .run" "$wf")" = 'make build-parallel' ]
|
||||
[ "$(yq "$step | .env.WRITE_CACHE" "$wf")" = 1 ]
|
||||
[ "$(yq "$step | .env.CACHE_TAG" "$wf")" = 'buildcache-${{ github.ref_name }}' ]
|
||||
[ "$(yq "$step | .env.CACHE_FROM_TAGS" "$wf")" = 'buildcache-${{ github.ref_name }} buildcache' ]
|
||||
}
|
||||
|
||||
@test "an rc build imports its line's cache and main's, and writes none" {
|
||||
wf=.github/workflows/tags.yaml
|
||||
build='.jobs["prepare-release"].steps[] | select(.name == "Build")'
|
||||
[ "$(yq "$build | .run" "$wf")" = 'make build-parallel' ]
|
||||
[ "$(yq "$build | .env.CACHE_FROM_TAGS" "$wf")" = 'buildcache-release-${{ steps.tag.outputs.line }} buildcache' ]
|
||||
[ "$(yq "$build | .env.WRITE_CACHE" "$wf")" = null ]
|
||||
}
|
||||
Reference in new issue
Block a user