diff --git a/imagebuildah/stage_executor.go b/imagebuildah/stage_executor.go index 617117a57a..2c05693b1a 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 @@ -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