2
votes

I have set up an automation through github/jenkins to post the output of terraform plan for the repo through jenkins as a comment to the pull request in github. The entire orchestration works great except for the fact that the output of terraform plan is not that human readable and doesn't provide you in this kind of automation a way as it looks when you run it in a terminal. I used several ways like using terraform show for the plan file, then grabbing that to a custom file and posting that as a comment in GitHub PR. In every case the output contains some binary characters.

i even used the terraform-plan-parser https://github.com/lifeomic/terraform-plan-parser but that doesn't work for terraform 0.12 and relates to the below issue :- https://github.com/lifeomic/terraform-plan-parser/issues/31

What's the best way to retrieve the output of any terraform plan in automation so that it can be referenced however that needs to be to inspect before the apply is done. Looks to me it only works great in a terminal.

Any help or suggestions here will be greatly appreciated as always.

2

2 Answers

9
votes

By default Terraform uses terminal escape sequences to highlight parts of the output with simple formatting such as colors or a bold typeface.

In order to reproduce that result exactly in the context of GitHub would require translating the terminal escape sequences into a form that GitHub is able to render.

Unfortunately GitHub comments are written in GitHub-flavored Markdown, which doesn't support any direct way to create colored text similar to Terraform's plan output at the time when I'm write this. Therefore I know of no easy way to reproduce the text formatting from the Terraform plan output in a GitHub comment.

If you run terraform plan with the -no-color option then it will skip the terminal escape sequences and produce plain-text output that you could include in a preformatted text block in your Markdown comment. However, that output will therefore not include the text formatting you normally see in your terminal.


If you are willing to write some custom formatting code to present the Terraform plan in a different format for your GitHub comments, you can obtain a JSON representation of the plan by saving the plan to disk and then reading it with terraform show:

terraform plan -out=tfplan
terraform show -json tfplan

This will produce a JSON representation of the plan that you could parse in a program of your own design and emit whatever result format you want. This will, however, be considerably more work than just interpreting the terminal escape sequences from Terraform's normal output, because it's a JSON representation of the data that Terraform uses to produce the plan rendering, not of the plan rendering itself.

0
votes