Skip to content

Commit 14bcb82

Browse files
committed
stage_executor,layers: burst cache if heredoc content is changed
When using buildah with `--layers` then buildah must correctly burst layer cache if `heredoc` content is changed. Following is achieved via properly adding `heredoc` content to the history of the built image. Closes: #5225 Signed-off-by: flouthoc <flouthoc.git@gmail.com>
1 parent a17eb95 commit 14bcb82

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

imagebuildah/stage_executor.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,14 @@ func (s *StageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
17251725
if buildArgs != "" {
17261726
return "|" + strconv.Itoa(len(strings.Split(buildArgs, " "))) + " " + buildArgs + " /bin/sh -c " + node.Original[4:]
17271727
}
1728-
return "/bin/sh -c " + node.Original[4:]
1728+
result := "/bin/sh -c " + node.Original[4:]
1729+
if len(node.Heredocs) > 0 {
1730+
for _, doc := range node.Heredocs {
1731+
heredocContent := strings.TrimSpace(doc.Content)
1732+
result = result + "\n" + heredocContent
1733+
}
1734+
}
1735+
return result
17291736
case "ADD", "COPY":
17301737
destination := node
17311738
for destination.Next != nil {

tests/bud.bats

+34
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,40 @@ _EOF
293293
run_buildah 1 run myctr ls -l subdir/
294294
}
295295

296+
@test "bud --layers should not hit cache if heredoc is changed" {
297+
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
298+
mkdir -p $contextdir
299+
300+
cat > $contextdir/Dockerfile << _EOF
301+
FROM alpine
302+
RUN <<EOF
303+
echo "Cache burst" >> /hello
304+
EOF
305+
RUN cat hello
306+
_EOF
307+
308+
# on first run since there is no cache so `Cache burst` must be printed
309+
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
310+
expect_output --substring "Cache burst"
311+
312+
# on second run since there is cache so `Cache burst` should not be printed
313+
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
314+
# output should not contain cache burst
315+
assert "$output" !~ "Cache burst"
316+
317+
cat > $contextdir/Dockerfile << _EOF
318+
FROM alpine
319+
RUN <<EOF
320+
echo "Cache burst add diff" >> /hello
321+
EOF
322+
RUN cat hello
323+
_EOF
324+
325+
# on third run since we have changed heredoc so `Cache burst` must be printed.
326+
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
327+
expect_output --substring "Cache burst"
328+
}
329+
296330
@test "bud build with heredoc content" {
297331
run_buildah build -t heredoc $WITH_POLICY_JSON -f $BUDFILES/heredoc/Containerfile .
298332
expect_output --substring "print first line from heredoc"

0 commit comments

Comments
 (0)