From f1cd5ae4a82a0f00aa4f752883a45ea63a5130d1 Mon Sep 17 00:00:00 2001 From: flouthoc Date: Sun, 18 Aug 2024 15:45:16 -0700 Subject: [PATCH 1/2] imagebuildah: additionalContext is not a local built stage This line looks like a typo in past commit as `additionalContext` which is found is not a local built stage so set `IsStage` to `false`. Signed-off-by: flouthoc --- imagebuildah/stage_executor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imagebuildah/stage_executor.go b/imagebuildah/stage_executor.go index 617117a57a..a5606069f0 100644 --- a/imagebuildah/stage_executor.go +++ b/imagebuildah/stage_executor.go @@ -694,7 +694,7 @@ func (s *StageExecutor) runStageMountPoints(mountList []string) (map[string]inte mountPoint = additionalBuildContext.DownloadedCache } } - stageMountPoints[from] = internal.StageMountDetails{IsStage: true, DidExecute: true, MountPoint: mountPoint} + stageMountPoints[from] = internal.StageMountDetails{IsStage: false, DidExecute: true, MountPoint: mountPoint} break } // If the source's name corresponds to the From d0988936b4b463f1a5aabe549746c96387db01de Mon Sep 17 00:00:00 2001 From: flouthoc Date: Sun, 18 Aug 2024 15:49:05 -0700 Subject: [PATCH 2/2] stage_executor: set avoidLookingCache only if mounting stage set `avoidLookingCache` to `true` if `--mount` is using a freshly built stage and not for `additional-build-context`. Signed-off-by: flouthoc --- imagebuildah/stage_executor.go | 2 +- tests/bud.bats | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/imagebuildah/stage_executor.go b/imagebuildah/stage_executor.go index a5606069f0..2c05693b1a 100644 --- a/imagebuildah/stage_executor.go +++ b/imagebuildah/stage_executor.go @@ -1492,7 +1492,7 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string, return "", nil, false, err } for _, mountPoint := range stageMountPoints { - if mountPoint.DidExecute { + if mountPoint.DidExecute && mountPoint.IsStage { avoidLookingCache = true } } diff --git a/tests/bud.bats b/tests/bud.bats index 36a271e2d8..933fbb864a 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -943,6 +943,34 @@ _EOF } +@test "build-test use image from cache with --mount and burst when image is changed" { + _prefetch alpine + local contextdir=${TEST_SCRATCH_DIR}/bud/platform + mkdir -p $contextdir + + cat > $contextdir/Containerfile << _EOF +FROM alpine +RUN touch firstfile +_EOF + + run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile + + cat > $contextdir/Containerfile2 << _EOF +FROM alpine + +RUN --mount=type=bind,source=.,target=/build,from=source ls /build + +_EOF + + run_buildah build $WITH_POLICY_JSON --layers -t source2 -f $contextdir/Containerfile2 + expect_output --substring "firstfile" + + # Building again must use cache + run_buildah build $WITH_POLICY_JSON --layers -t source2 -f $contextdir/Containerfile2 + expect_output --substring "Using cache" + assert "$output" !~ "firstfile" +} + # Verify: https://github.com/containers/buildah/issues/4572 @test "build-test verify no dangling containers are left" { _prefetch alpine busybox @@ -1515,6 +1543,30 @@ _EOF expect_output --substring "world" } +@test "build-with-additional-build-context must use cache if built with layers" { + _prefetch alpine + local contextdir=${TEST_SCRATCH_DIR}/bud/platform + mkdir -p $contextdir + echo world > $contextdir/hello + + cat > $contextdir/Containerfile2 << _EOF +FROM alpine as some-stage +RUN echo some_text + +# hello should get copied since we are giving priority to additional context +FROM alpine +RUN --mount=type=bind,from=some-stage,target=/test,z cat /test/hello +_EOF + + # Additional context for RUN --mount is file on host + run_buildah build $WITH_POLICY_JSON --layers --build-context some-stage=$contextdir -t test -f $contextdir/Containerfile2 + expect_output --substring "world" + + run_buildah build $WITH_POLICY_JSON --layers --build-context some-stage=$contextdir -t test -f $contextdir/Containerfile2 + expect_output --substring "Using cache" + assert "$output" !~ "world" +} + # Test usage of RUN --mount=from= with additional context is URL and mount source is relative using src @test "build-with-additional-build-context and RUN --mount=from=, additional-context is URL and mounted from subdir" { _prefetch alpine