Skip to content

Commit 01ea275

Browse files
committed
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 <flouthoc.git@gmail.com>
1 parent 3bc7653 commit 01ea275

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

imagebuildah/stage_executor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,7 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string,
14921492
return "", nil, false, err
14931493
}
14941494
for _, mountPoint := range stageMountPoints {
1495-
if mountPoint.DidExecute {
1495+
if mountPoint.DidExecute && mountPoint.IsStage {
14961496
avoidLookingCache = true
14971497
}
14981498
}

tests/bud.bats

+52
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,34 @@ _EOF
943943

944944
}
945945

946+
@test "build-test use image from cache with --mount and burst when image is changed" {
947+
_prefetch alpine
948+
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
949+
mkdir -p $contextdir
950+
951+
cat > $contextdir/Containerfile << _EOF
952+
FROM alpine
953+
RUN touch firstfile
954+
_EOF
955+
956+
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile
957+
958+
cat > $contextdir/Containerfile2 << _EOF
959+
FROM alpine
960+
961+
RUN --mount=type=bind,source=.,target=/build,from=source ls /build
962+
963+
_EOF
964+
965+
run_buildah build $WITH_POLICY_JSON --layers -t source2 -f $contextdir/Containerfile2
966+
expect_output --substring "firstfile"
967+
968+
# Building again must use cache
969+
run_buildah build $WITH_POLICY_JSON --layers -t source2 -f $contextdir/Containerfile2
970+
expect_output --substring "Using cache"
971+
assert "$output" !~ "firstfile"
972+
}
973+
946974
# Verify: https://github.com/containers/buildah/issues/4572
947975
@test "build-test verify no dangling containers are left" {
948976
_prefetch alpine busybox
@@ -1515,6 +1543,30 @@ _EOF
15151543
expect_output --substring "world"
15161544
}
15171545

1546+
@test "build-with-additional-build-context must use cache if built with layers" {
1547+
_prefetch alpine
1548+
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
1549+
mkdir -p $contextdir
1550+
echo world > $contextdir/hello
1551+
1552+
cat > $contextdir/Containerfile2 << _EOF
1553+
FROM alpine as some-stage
1554+
RUN echo some_text
1555+
1556+
# hello should get copied since we are giving priority to additional context
1557+
FROM alpine
1558+
RUN --mount=type=bind,from=some-stage,target=/test,z cat /test/hello
1559+
_EOF
1560+
1561+
# Additional context for RUN --mount is file on host
1562+
run_buildah build $WITH_POLICY_JSON --layers --build-context some-stage=$contextdir -t test -f $contextdir/Containerfile2
1563+
expect_output --substring "world"
1564+
1565+
run_buildah build $WITH_POLICY_JSON --layers --build-context some-stage=$contextdir -t test -f $contextdir/Containerfile2
1566+
expect_output --substring "Using cache"
1567+
assert "$output" !~ "world"
1568+
}
1569+
15181570
# Test usage of RUN --mount=from=<name> with additional context is URL and mount source is relative using src
15191571
@test "build-with-additional-build-context and RUN --mount=from=, additional-context is URL and mounted from subdir" {
15201572
_prefetch alpine

0 commit comments

Comments
 (0)