Skip to content

stage_executor: history should include heredoc summary correctly #6041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1943,17 +1943,20 @@ func (s *StageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
if len(node.Original) > 4 {
shArg = node.Original[4:]
}
if buildArgs != "" {
return "|" + strconv.Itoa(len(strings.Split(buildArgs, " "))) + " " + buildArgs + " /bin/sh -c " + shArg + appendCheckSum, nil
}
result := "/bin/sh -c " + shArg

heredoc := ""
result := ""
if len(node.Heredocs) > 0 {
for _, doc := range node.Heredocs {
heredocContent := strings.TrimSpace(doc.Content)
result = result + "\n" + heredocContent
heredoc = heredoc + "\n" + heredocContent
}
}
return result + appendCheckSum, nil
if buildArgs != "" {
result = result + "|" + strconv.Itoa(len(strings.Split(buildArgs, " "))) + " " + buildArgs + " "
}
result = result + "/bin/sh -c " + shArg + heredoc + appendCheckSum
return result, nil
case "ADD", "COPY":
destination := node
for destination.Next != nil {
Expand Down
38 changes: 38 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,44 @@ _EOF
expect_output --substring "Cache burst add diff"
}

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

cat > $contextdir/Dockerfile << _EOF
FROM alpine
ARG key=value
RUN <<EOF
echo "Cache burst" >> /hello
echo "Cache burst second line" >> /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 second line"

# 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 second line"

cat > $contextdir/Dockerfile << _EOF
FROM alpine
ARG key=value
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 add diff"
}

@test "bud build with heredoc content" {
_prefetch alpine
run_buildah build -t heredoc $WITH_POLICY_JSON -f $BUDFILES/heredoc/Containerfile .
Expand Down