Skip to content

Commit

Permalink
Merge pull request #5314 from flouthoc/heredoc-show-summary
Browse files Browse the repository at this point in the history
build, heredoc: show `heredoc` summary in build output
  • Loading branch information
openshift-merge-bot[bot] committed Feb 7, 2024
2 parents 4e9d2d5 + 533aac2 commit db73170
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
19 changes: 18 additions & 1 deletion imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,24 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string,
}
logrus.Debugf("Parsed Step: %+v", *step)
if !s.executor.quiet {
s.log("%s", step.Original)
logMsg := step.Original
if len(step.Heredocs) > 0 {
summarizeHeredoc := func(doc string) string {
doc = strings.TrimSpace(doc)
lines := strings.Split(strings.ReplaceAll(doc, "\r\n", "\n"), "\n")
summary := lines[0]
if len(lines) > 1 {
summary += "..."
}
return summary
}

for _, doc := range node.Heredocs {
heredocContent := summarizeHeredoc(doc.Content)
logMsg = logMsg + " (" + heredocContent + ")"
}
}
s.log("%s", logMsg)
}

// Check if there's a --from if the step command is COPY.
Expand Down
14 changes: 11 additions & 3 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,19 @@ _EOF
FROM alpine
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"
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"
assert "$output" !~ "Cache burst second line"

cat > $contextdir/Dockerfile << _EOF
FROM alpine
Expand All @@ -324,7 +325,7 @@ _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"
expect_output --substring "Cache burst add diff"
}

@test "bud build with heredoc content" {
Expand All @@ -344,6 +345,13 @@ _EOF
expect_output --substring "this is the output of test7 part3"
expect_output --substring "this is the output of test8 part1"
expect_output --substring "this is the output of test8 part2"

# verify that build output contains summary of heredoc content
expect_output --substring 'RUN <<EOF \(echo "print first line from heredoc"...)'
expect_output --substring 'RUN <<EOF \(echo "Heredoc writing first file" >> /file1...)'
expect_output --substring 'RUN python3 <<EOF \(with open\("/file2", "w") as f:...)'
expect_output --substring 'ADD <<EOF /index.html \(\(your index page goes here))'
expect_output --substring 'COPY <<robots.txt <<humans.txt /test/ \(\(robots content)) \(\(humans content))'
}

@test "bud build with heredoc content which is a bash file" {
Expand Down

0 comments on commit db73170

Please sign in to comment.