buildkite / terminal-to-html

Converts arbitrary shell output (with ANSI) into beautifully rendered HTML
http://buildkite.github.io/terminal-to-html
MIT License
642 stars 45 forks source link

terminal-to-html imposes odd formatting on Terraform plans #139

Closed mdb closed 3 months ago

mdb commented 3 months ago

Thanks for your work on terminal-to-html! I'd like to surface ansi-color-encoded Terraform plans as Buildkite annotations, and am using the terraform-to-html CLI to do so via bash similar to the following, where plan.log is the output of a terraform plan invocation:

echo '<details><summary>Terraform plan</summary>' > plan.html
echo '<div><code><pre class="term">' >> plan.html
cat plan.log | terminal-to-html >> plan.html
echo '</pre></code></div></details>' >> plan.html
cat plan.html | head -c 1048576 | buildkite-agent annotate --style 'info' --context "ctx-plan-$project"

However, the plan formatting gets a bit mangled and over-styled, and looks like the following example within the annotation (note the inconsistent text size/styling, as well as odd indentation). Ideally, the annotation would depict the plan output just as it appears in the Buildkite step output (and how it renders in my terminal when I invoke terraform plan).

image

Thanks!

yob commented 3 months ago

@mdb Buildkite prints our own terraform plans as a build annotation, so I had a look to see what we do that seems to work.

It's done via a ruby script so the syntax is slightly different, but :

summary = capture!("terraform", "show", "plan.out")

# Terraform 0.15 introduced a whole lot of useless noisy output about
# drift which has been ignored before the actual plan output. Throw
# away the noise. There's a big horizontal rule of `-----` in the middle.
summary = summary.split(/^─{10,}(?:\e\[0m)?\n/).last.strip

annotation = <<~MARKDOWN
  Terraform determines the following changes need to be made:

  \```term
  #{summary}
  \```
MARKDOWN

system!("buildkite-agent", "annotate", "--style", "warning", stdin_data: annotation)

Could you use the term code block as a workaround, maybe with using terraform show as well?

DrJosh9000 commented 3 months ago

Hi @mdb, if @yob's suggestion doesn't work for you, we'd be interested in taking a look at the job or at least the raw annotation data. You can email support@buildkite.com with the details, and refer to this issue, so we can see the specifics of your case.

mdb commented 3 months ago

Hi @mdb, if @yob's suggestion doesn't work for you, we'd be interested in taking a look at the job or at least the raw annotation data. You can email support@buildkite.com with the details, and refer to this issue, so we can see the specifics of your case.

Unfortunately, @yob 's suggestion doesn't work, as I'd like to also include a <details> HTML dropdown in the annotation, yet it seems Buildkite fails to properly render markdown inside annotations that include a mix of markdown code fences and HTML.

However, after lotsa lotsa lotsa trial/error, I was able to accomplish what I'm after via echo -e 😄 :

html_plan="$(terminal-to-html < plan.log)"
plan_annotation=$(echo -e "<h4>Terraform plan</h4> \
  <details>
    <summary>Terraform Plan</summary> \
    <pre class=\"term\"> \
      <code> \
        ${html_plan} \
      </code> \
    </pre> \
  </details>")

buildkite-agent annotate \
  "${plan_annotation}" \
  --style "info" \
  --context "plan"