diff --git a/.gitea/workflows/docker-publish.yml b/.gitea/workflows/docker-publish.yml index 3bbae1e..1b7d683 100644 --- a/.gitea/workflows/docker-publish.yml +++ b/.gitea/workflows/docker-publish.yml @@ -137,7 +137,10 @@ jobs: # persistent state volume) is reused as-is and never recreated or restarted here, so # concurrent jobs cannot race on reconfiguration. Existing builders are migrated once, # manually: see docs/buildkit-gc.md. maxUsedSpace is a GC threshold, not a hard quota. + BUILDER="multiarch" + BUILDKIT_CONTAINER="buildx_buildkit_${BUILDER}0" BUILDKITD_CONFIG="$(mktemp)" + trap 'rm -f "${BUILDKITD_CONFIG}"' EXIT cat > "${BUILDKITD_CONFIG}" <<'EOF' [worker.oci] gc = true @@ -145,19 +148,51 @@ jobs: maxUsedSpace = "20GB" minFreeSpace = "50GB" EOF - docker buildx inspect multiarch >/dev/null 2>&1 || \ - docker buildx create --name multiarch --driver docker-container \ - --buildkitd-config "${BUILDKITD_CONFIG}" --bootstrap || \ - docker buildx inspect --bootstrap multiarch - docker buildx use multiarch - # Non-fatal: flag builders that predate the GC config. Authoritative check of the - # running daemon is the "GC Policy" section of `docker buildx inspect multiarch`. - if ! docker exec buildx_buildkit_multiarch0 \ - grep -q 'maxUsedSpace = "20GB"' /etc/buildkit/buildkitd.toml 2>/dev/null; then - echo "::warning::BuildKit builder 'multiarch' has no managed GC config; cache may grow unbounded. See docs/buildkit-gc.md in SharedWorkflows." + # Job containers are ephemeral, so buildx metadata is usually missing even when the + # BuildKit container exists on the build daemon. Register metadata only (no --bootstrap) + # so a create error is reported as such; the container is started separately below. + CONTAINER_EXISTED=false + if docker container inspect "${BUILDKIT_CONTAINER}" >/dev/null 2>&1; then + CONTAINER_EXISTED=true + echo "=== Reusing existing BuildKit container ${BUILDKIT_CONTAINER} (not recreated) ===" + fi + # true only when this job registers the builder with the config AND buildx will create + # the container from it; only then is a missing GC policy a hard error. + EXPECT_GC_CONFIG=false + if ! docker buildx inspect "${BUILDER}" >/dev/null 2>&1; then + docker buildx create --name "${BUILDER}" --driver docker-container \ + --buildkitd-config "${BUILDKITD_CONFIG}" + if [ "${CONTAINER_EXISTED}" = "false" ]; then + echo "=== Creating ${BUILDKIT_CONTAINER} with the GC config ===" + EXPECT_GC_CONFIG=true + fi + fi + + # Start/attach the builder. Bootstrap reuses an existing container and its state volume. + # One retry covers a concurrent job creating the same container at the same moment; a + # second failure fails the job with buildx's own error. + if ! docker buildx inspect --bootstrap "${BUILDER}" >/dev/null; then + echo "=== Bootstrap of ${BUILDER} failed, retrying once in 10s ===" + sleep 10 + docker buildx inspect --bootstrap "${BUILDER}" >/dev/null + fi + docker buildx use "${BUILDER}" + + # Verify the GC policy the *running* daemon reports (buildx renders sizes with binary + # units; accept both the 10GiB and the 9.313GiB rendering of "10GB", etc.). + BUILDER_INFO="$(docker buildx inspect "${BUILDER}")" + echo "${BUILDER_INFO}" | sed -n '/GC Policy/,$p' + if echo "${BUILDER_INFO}" | grep -Eq 'Reserved Space:[[:space:]]+(10GiB|9\.313GiB)' && + echo "${BUILDER_INFO}" | grep -Eq 'Max Used Space:[[:space:]]+(20GiB|18\.63GiB)' && + echo "${BUILDER_INFO}" | grep -Eq 'Min Free Space:[[:space:]]+(50GiB|46\.57GiB)'; then + echo "=== BuildKit GC config is active (reservedSpace 10GB, maxUsedSpace 20GB, minFreeSpace 50GB) ===" + elif [ "${EXPECT_GC_CONFIG}" = "true" ]; then + echo "::error::Builder ${BUILDER} was created with --buildkitd-config but the running BuildKit daemon does not report the expected GC policy." + exit 1 + else + echo "::warning::The managed BuildKit GC limits (20GB target) are NOT active on the existing builder ${BUILDER} (${BUILDKIT_CONTAINER}): it predates the GC config and is reused unchanged, so it still runs with BuildKit's disk-percentage defaults and its cache can keep growing. A one-time manual migration is required, see docs/buildkit-gc.md in SharedWorkflows." fi - docker buildx inspect multiarch | sed -n '/GC Policy/,$p' || true - name: Build image for vulnerability scan if: ${{ inputs.run_image_scan }} diff --git a/docs/buildkit-gc.md b/docs/buildkit-gc.md index 4e5588a..928da56 100644 --- a/docs/buildkit-gc.md +++ b/docs/buildkit-gc.md @@ -23,7 +23,8 @@ target the host daemon that runs the production containers. - The `reservedSpace`/`maxUsedSpace`/`minFreeSpace` keys need **BuildKit >= v0.17**. Older daemons only understand `gckeepstorage`. `moby/buildkit:buildx-stable-1` is far newer than v0.17. Check the deployed version anyway (section 3). -- Sizes are decimal: `20GB` = 20·10⁹ bytes. `buildx inspect` shows it as `18.63GiB`. +- BuildKit parses the sizes with go-units. Depending on the version, `docker buildx inspect` + shows `20GB` as `20GiB` (binary) or `18.63GiB` (decimal). The workflow check accepts both. - `maxUsedSpace` is a **GC threshold, not a quota**. GC runs after builds and periodically, so the cache can briefly exceed 20 GB during a large build. GC removes the least recently used records first, so hot base and dependency layers survive. @@ -38,14 +39,25 @@ The file is in the container's filesystem, so it survives `docker restart` and D restarts. It is lost only if the container is removed. When the container already exists, the workflow **reuses it unchanged**. It never removes, -recreates, restarts or reconfigures a builder, so concurrent jobs cannot race on that. The only -race left is two jobs creating a *missing* builder at the same time. That race existed before -this change. The losing job now falls back to `docker buildx inspect --bootstrap multiarch`, -which never recreates anything. +recreates, restarts or reconfigures a builder, so concurrent jobs cannot race on that. -If the running builder has no managed config, every publish job prints a non-fatal warning -(`::warning::BuildKit builder 'multiarch' has no managed GC config`). The job log also shows the -daemon's GC policy. +The setup step runs these steps in order, and none of them hides an error: + +1. If this job has no buildx metadata for `multiarch`, run `docker buildx create + --buildkitd-config` without `--bootstrap`. If that fails, the job fails. +2. Run `docker buildx inspect --bootstrap multiarch`. This starts the existing container, or + creates it if it is missing. If it fails, the step retries once after 10 seconds. That retry + covers two jobs creating a missing builder at the same moment. A second failure fails the + job with buildx's own error. +3. Check the GC policy that the **running daemon** reports (`docker buildx inspect multiarch`; + the job log shows the "GC Policy" section): + - all three limits match: logs `BuildKit GC config is active`; + - this job created the container from the config, but the limits do not match: **job + fails** (`::error::`); + - the builder already existed without the config: **non-fatal warning** that the managed GC + limits are not active and the one-time migration below is required. The build continues + unchanged. +4. Delete the temporary config file when the step exits. ## 2. One-time migration of the existing `multiarch` builder (manual) @@ -88,7 +100,11 @@ docker exec gitea-dind docker restart buildx_buildkit_multiarch0 docker exec gitea-dind rm -rf /tmp/bk ``` -The config is not active until the restart. Verify it (section 3) before releasing the runner. +The config is not active until the restart. Verify it (section 3) before you release the runner. +If the daemon does not report the expected policy, or the container does not come back up, +delete the file again and restart the container (see Rollback in section 5). The cache volume +is not affected either way. Once the runner is back in service, the next v1.11.5 publish job +should log `BuildKit GC config is active` instead of the migration warning. ### Option B (fallback): remove only the container and let CI recreate it @@ -100,7 +116,9 @@ docker exec gitea-dind docker rm -f buildx_buildkit_multiarch0 The next publish job runs `buildx create --buildkitd-config`. That creates a new container, which mounts the **existing** named volume `buildx_buildkit_multiarch0_state`, so the cache is kept. Start a single publish job first, so that parallel jobs do not race to create the -container. Use this option only if Option A cannot be verified. +container. That first job **must use `@v1.11.5` or later**. A consumer still on v1.11.4 or older +recreates the container *without* the config. Use this option only if Option A cannot be +verified. ## 3. Verification @@ -110,9 +128,9 @@ Of the commands below, only the running daemon's GC policy proves that GC is con ```bash # Authoritative: the GC policy the running buildkitd uses docker exec gitea-dind docker exec buildx_buildkit_multiarch0 buildctl debug workers -v -# expected: GC Policy rules showing Reserved Space 10GB / Max Used Space 20GB / Min Free Space 50GB -# (some versions print these as 9.313GiB / 18.63GiB / 46.57GiB). Without the config you see -# percentage-derived defaults instead. +# expected: GC Policy rules with Reserved Space / Max Used Space / Min Free Space of +# 10GiB / 20GiB / 50GiB (or 9.313GiB / 18.63GiB / 46.57GiB, depending on the version). +# Without the config you see percentage-derived defaults instead. # The same data appears in every publish job log ("GC Policy" section of `docker buildx inspect multiarch`). # The file that buildkitd loads at startup (shows it exists, not that it is active)