Skip to content

Commit 8fd8abc

Browse files
Merge pull request #6041 from flouthoc/heredoc-buildarg
stage_executor: history should include heredoc summary correctly
2 parents ac000a6 + 4dd300b commit 8fd8abc

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

imagebuildah/stage_executor.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -1943,17 +1943,20 @@ func (s *StageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
19431943
if len(node.Original) > 4 {
19441944
shArg = node.Original[4:]
19451945
}
1946-
if buildArgs != "" {
1947-
return "|" + strconv.Itoa(len(strings.Split(buildArgs, " "))) + " " + buildArgs + " /bin/sh -c " + shArg + appendCheckSum, nil
1948-
}
1949-
result := "/bin/sh -c " + shArg
1946+
1947+
heredoc := ""
1948+
result := ""
19501949
if len(node.Heredocs) > 0 {
19511950
for _, doc := range node.Heredocs {
19521951
heredocContent := strings.TrimSpace(doc.Content)
1953-
result = result + "\n" + heredocContent
1952+
heredoc = heredoc + "\n" + heredocContent
19541953
}
19551954
}
1956-
return result + appendCheckSum, nil
1955+
if buildArgs != "" {
1956+
result = result + "|" + strconv.Itoa(len(strings.Split(buildArgs, " "))) + " " + buildArgs + " "
1957+
}
1958+
result = result + "/bin/sh -c " + shArg + heredoc + appendCheckSum
1959+
return result, nil
19571960
case "ADD", "COPY":
19581961
destination := node
19591962
for destination.Next != nil {

tests/bud.bats

+38
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,44 @@ _EOF
542542
expect_output --substring "Cache burst add diff"
543543
}
544544

545+
@test "bud --layers should not hit cache if heredoc is changed - with ARG" {
546+
_prefetch alpine
547+
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
548+
mkdir -p $contextdir
549+
550+
cat > $contextdir/Dockerfile << _EOF
551+
FROM alpine
552+
ARG key=value
553+
RUN <<EOF
554+
echo "Cache burst" >> /hello
555+
echo "Cache burst second line" >> /hello
556+
EOF
557+
RUN cat hello
558+
_EOF
559+
560+
# on first run since there is no cache so `Cache burst` must be printed
561+
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
562+
expect_output --substring "Cache burst second line"
563+
564+
# on second run since there is cache so `Cache burst` should not be printed
565+
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
566+
# output should not contain cache burst
567+
assert "$output" !~ "Cache burst second line"
568+
569+
cat > $contextdir/Dockerfile << _EOF
570+
FROM alpine
571+
ARG key=value
572+
RUN <<EOF
573+
echo "Cache burst add diff" >> /hello
574+
EOF
575+
RUN cat hello
576+
_EOF
577+
578+
# on third run since we have changed heredoc so `Cache burst` must be printed.
579+
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
580+
expect_output --substring "Cache burst add diff"
581+
}
582+
545583
@test "bud build with heredoc content" {
546584
_prefetch alpine
547585
run_buildah build -t heredoc $WITH_POLICY_JSON -f $BUDFILES/heredoc/Containerfile .

0 commit comments

Comments
 (0)