mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-02 21:30:46 +09:00
perf(ci): build release lines from their own cache, in parallel
Release-line image builds read only main's build cache. Once main moved its builder and base images, release-1.7's Dockerfiles stopped matching that cache, and the rc Build step went from 19 to 53 minutes on amd64 and from 10 to 46 on arm64 between v1.7.1-rc.2 and v1.7.2-rc.2. Each line now keeps a cache of its own. build-release.yaml writes buildcache-release-X.Y on every push to the line, and rc builds import it together with main's buildcache. The arm64 rc build, the only arm64 build a line has, keeps buildcache-arm64-release-X.Y itself. CACHE_FROM_TAGS lists every cache a build imports; unset, it is CACHE_TAG, as before. The units of hack/build-matrix.sh now build four at a time (make build- parallel, hack/build-units.sh), since they spend most of their time on registry round trips, one image after another. Every output line carries its unit and is written as it comes, so a job that times out still shows where it stopped. objectstorage-controller waits for the rest because it writes seaweedfs' values.yaml. The rc jobs also leave their buildx builder to the throwaway runner: removing it took up to 11 minutes on arm64. A cache write that fails no longer fails the build (ignore-error=true on every --cache-to): OCIR refuses a blob upload now and then, which already failed the arm64 nightly twice in its last thirty runs, and rc and line builds now write a cache too. Tested with bats: the cache macro, the unit builder against a fake make (the BUILD_JOBS limit, ordering, a failing unit, a failing objectstorage-controller, a unit list that cannot be generated, and output that shows while a unit still builds) and the workflow settings, plus a dry run of hack/build-units.sh over the real units with make -n. Assisted-by: LLM Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
12 files changed
+251
-35
No files matched your search
Executable
+34
@@ -0,0 +1,34 @@
|
||||
#!/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/seaweedfs/values.yaml, which the seaweedfs build
|
||||
# rewrites too, so it waits until the others are done.
|
||||
last=packages/system/objectstorage-controller
|
||||
|
||||
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/seaweedfs","packages/system/objectstorage-controller","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/system/objectstorage-controller ] && ! grep -qx 'end packages/system/seaweedfs' "$WORK/order"; then
|
||||
echo "objectstorage-controller started while seaweedfs 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 objectstorage-controller after seaweedfs" {
|
||||
fake_tree
|
||||
run_units
|
||||
[ "$status" -eq 0 ]
|
||||
for unit in packages/system/seaweedfs packages/system/objectstorage-controller 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 objectstorage-controller 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/system/objectstorage-controller' "$work/order"; then
|
||||
echo "FAIL: objectstorage-controller 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 objectstorage-controller, built after the others, fails the build too" {
|
||||
fake_tree
|
||||
FAIL_UNIT=packages/system/objectstorage-controller run_units
|
||||
[ "$status" -ne 0 ]
|
||||
echo "$output" | grep -qF 'packages/system/objectstorage-controller: boom from packages/system/objectstorage-controller'
|
||||
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"
|
||||
}
|
||||
+6
-1
@@ -148,12 +148,17 @@ endef
|
||||
# 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).
|
||||
# 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_TAG lets a build for another platform keep a cache of its own
|
||||
# (buildcache-arm64) instead of overwriting the amd64 one with every write.
|
||||
# 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) (defined above) escapes the literal commas in the --cache-to value so
|
||||
# make does not mis-parse them as $(if ...) argument separators.
|
||||
CACHE_TAG ?= buildcache
|
||||
cache-args = --cache-from type=registry,ref=$(CACHE_REGISTRY)/$(1):$(CACHE_TAG)$(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)
|
||||
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)
|
||||
|
||||
@@ -202,6 +202,30 @@ load test_helper
|
||||
echo "$out" | grep -q -- '--cache-to type=registry,ref=[^ ]*/img:buildcache-arm64,'
|
||||
}
|
||||
|
||||
@test "CACHE_FROM_TAGS imports every cache it lists and the build still writes only CACHE_TAG" {
|
||||
# A release line imports its own cache and main's, whose Dockerfiles drift
|
||||
# away from the line's over time.
|
||||
pkg=packages/system/cozystack-controller
|
||||
out=$(make -n -C "$pkg" image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b WRITE_CACHE=1 CACHE_TAG=buildcache-release-1.7 'CACHE_FROM_TAGS=buildcache-release-1.7 buildcache')
|
||||
echo "$out" | grep -q -- '--cache-from type=registry,ref=[^ ]*/cozystack-controller:buildcache-release-1.7 '
|
||||
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.7,'
|
||||
# A failed cache write must not fail the build: OCIR refuses a blob upload now
|
||||
# and then.
|
||||
echo "$out" | grep -q -- '--cache-to type=registry,ref=[^ ]*/cozystack-controller:buildcache-release-1.7,[^ ]*ignore-error=true'
|
||||
[ "$(echo "$out" | grep -o -- '--cache-to' | wc -l)" -eq 1 ]
|
||||
# Importing from several caches writes none of them unless WRITE_CACHE=1:
|
||||
# PR builds and the amd64 rc build only read.
|
||||
for write in '' WRITE_CACHE=0; do
|
||||
out=$(make -n -C "$pkg" image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b $write 'CACHE_FROM_TAGS=buildcache-release-1.7 buildcache')
|
||||
[ "$(echo "$out" | grep -o -- '--cache-from' | wc -l)" -eq 2 ]
|
||||
[ "$(echo "$out" | grep -o -- '--cache-to' | wc -l)" -eq 0 ]
|
||||
done
|
||||
# Without it, a build imports exactly the one cache it would write.
|
||||
out=$(make -n -C "$pkg" image IMAGE_TAG=pr-1-abc COZYSTACK_VERSION=0 BUILDER=b)
|
||||
[ "$(echo "$out" | grep -o -- '--cache-from' | wc -l)" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "the kamaji provider image is pushed under the build's IMAGE_TAG like every other image" {
|
||||
# An IMAGE_TAG assigned in a package Makefile beats the value CI puts in the
|
||||
# environment, so every PR, main and line build would push the same
|
||||
|
||||
@@ -320,10 +320,11 @@ load test_helper
|
||||
|
||||
# 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,36 @@
|
||||
#!/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 "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 only arm64 writes one" {
|
||||
wf=.github/workflows/tags.yaml
|
||||
amd64='.jobs["build-amd64"].steps[] | select(.name == "Build")'
|
||||
[ "$(yq "$amd64 | .env.CACHE_FROM_TAGS" "$wf")" = 'buildcache-release-${{ steps.tag.outputs.line }} buildcache' ]
|
||||
[ "$(yq "$amd64 | .env.WRITE_CACHE" "$wf")" = null ]
|
||||
arm64='.jobs["build-arm64"].steps[] | select(.name == "Build arm64 images")'
|
||||
[ "$(yq "$arm64 | .env.WRITE_CACHE" "$wf")" = 1 ]
|
||||
yq "$arm64 | .run" "$wf" | grep -qxF 'export CACHE_TAG="buildcache-arm64-$line"'
|
||||
yq "$arm64 | .run" "$wf" | grep -qxF 'export CACHE_FROM_TAGS="buildcache-arm64-$line buildcache-arm64"'
|
||||
}
|
||||
|
||||
@test "the rc build jobs leave their buildx builder to the throwaway runner" {
|
||||
wf=.github/workflows/tags.yaml
|
||||
setup='[(.jobs["build-arm64"].steps[], .jobs["build-amd64"].steps[]) | select((.uses // "") | test("^docker/setup-buildx-action@"))]'
|
||||
[ "$(yq "$setup | length" "$wf")" -eq 2 ]
|
||||
[ "$(yq "$setup | map(.with.cleanup == false) | all" "$wf")" = true ]
|
||||
}
|
||||
@@ -383,7 +383,7 @@ _make_world() {
|
||||
amd_steps=$(yq -r '.jobs.build-amd64.steps[].name' "$wf")
|
||||
echo "$amd_steps" | grep -qx 'Check if release already exists'
|
||||
echo "$amd_steps" | grep -qx 'Reject hand-pushed stable tags'
|
||||
[ "$(yq -r '.jobs.build-arm64.steps[] | select(.run // "" | test("build-matrix")) | .env.IMAGE_TAG' "$wf")" = '${{ github.ref_name }}-arm64' ]
|
||||
[ "$(yq -r '.jobs.build-arm64.steps[] | select(.run // "" | test("build-units")) | .env.IMAGE_TAG' "$wf")" = '${{ github.ref_name }}-arm64' ]
|
||||
[ "$(yq -r '.jobs.prepare-release.needs | sort | join(" ")' "$wf")" = 'build-amd64 build-arm64' ]
|
||||
cond=$(yq -r '.jobs.prepare-release.if' "$wf")
|
||||
echo "$cond" | grep -qF '!cancelled()'
|
||||
|
||||
Reference in new issue
Block a user