Skip to content

Commit

Permalink
build, heredoc: show heredoc summary in build output
Browse files Browse the repository at this point in the history
Buildah must show summary of heredoc content in build output so its easy
for developers to understand which heredoc got executed, this is similar
to what buildkit does for heredoc content.

See: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/dockerfile2llb/convert.go#L1853

Sample output of buildah

```console
STEP 1/5: FROM docker.io/library/python:latest
STEP 2/5: RUN <<EOF (echo "Hello" >> /hello...)
STEP 3/5: RUN python3 <<EOF (with open("/hello", "w") as f:...)
STEP 4/5: RUN ls -a
```

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
  • Loading branch information
flouthoc committed Feb 1, 2024
1 parent b850c71 commit 1dcfcbf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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
7 changes: 7 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,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 1dcfcbf

Please sign in to comment.