Skip to content

Commit

Permalink
stage_executor: set avoidLookingCache only if mounting stage
Browse files Browse the repository at this point in the history
set `avoidLookingCache` to `true` if `--mount` is using a freshly built
stage and not for `additional-build-context`.

Signed-off-by: flouthoc <[email protected]>
  • Loading branch information
flouthoc committed Aug 20, 2024
1 parent f1cd5ae commit d098893
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
52 changes: 52 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=<name> 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
Expand Down

0 comments on commit d098893

Please sign in to comment.