Skip to content

Commit

Permalink
stage_executor,layers: burst cache if heredoc content is changed
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
flouthoc committed Jan 11, 2024
1 parent a17eb95 commit 14bcb82
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
9 changes: 8 additions & 1 deletion imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,14 @@ func (s *StageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
if buildArgs != "" {
return "|" + strconv.Itoa(len(strings.Split(buildArgs, " "))) + " " + buildArgs + " /bin/sh -c " + node.Original[4:]
}
return "/bin/sh -c " + node.Original[4:]
result := "/bin/sh -c " + node.Original[4:]
if len(node.Heredocs) > 0 {
for _, doc := range node.Heredocs {
heredocContent := strings.TrimSpace(doc.Content)
result = result + "\n" + heredocContent
}
}
return result
case "ADD", "COPY":
destination := node
for destination.Next != nil {
Expand Down
34 changes: 34 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,40 @@ _EOF
run_buildah 1 run myctr ls -l subdir/
}

@test "bud --layers should not hit cache if heredoc is changed" {
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir

cat > $contextdir/Dockerfile << _EOF
FROM alpine
RUN <<EOF
echo "Cache burst" >> /hello
EOF
RUN cat hello
_EOF

# on first run since there is no cache so `Cache burst` must be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
expect_output --substring "Cache burst"

# on second run since there is cache so `Cache burst` should not be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
# output should not contain cache burst
assert "$output" !~ "Cache burst"

cat > $contextdir/Dockerfile << _EOF
FROM alpine
RUN <<EOF
echo "Cache burst add diff" >> /hello
EOF
RUN cat hello
_EOF

# on third run since we have changed heredoc so `Cache burst` must be printed.
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
expect_output --substring "Cache burst"
}

@test "bud build with heredoc content" {
run_buildah build -t heredoc $WITH_POLICY_JSON -f $BUDFILES/heredoc/Containerfile .
expect_output --substring "print first line from heredoc"
Expand Down

0 comments on commit 14bcb82

Please sign in to comment.