fix(docker-publish): explicit builder setup and daemon-side GC verification

Replace the chained inspect||create||inspect with explicit steps:
register builder metadata without --bootstrap (create errors fail the
job), bootstrap with a single logged retry for concurrent creation, and
verify the GC policy reported by the running BuildKit daemon. A builder
created by this job without the expected policy fails the job; an
existing unmigrated builder gets a warning pointing to the manual
migration. The temporary buildkitd config is removed on exit. Existing
containers and state volumes are never recreated or removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dresberandClaude Opus 5.5 committed 2026-10-24 12:42:55 +00:00
1 parent 4535438a1c
commit de2e6fd3bd
2 files changed
+77 -24

No files matched your search

+46 -11
View File
@@ -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 }}