Skip to content

Renders Terraform fmt,plan,validate output in Markdown, so any CI/CD can post as opinionated GitHub PR comment

License

Notifications You must be signed in to change notification settings

mloskot/terraform-github-pr-commenter

Repository files navigation

terraform-github-pr-commenter

Bash script to render Terraform command output as GitHub Pull Request comment ready to be posted from any CI/CD agent that can be authenticated with GitHub, for example, Azure Pipelines or GitHub Actions.

fmt

See screenshot/README.md for more visual samples of posted GitHub comments.

Requirements

  • Bash
  • iconv or konwert to convert the Terraform fancy outputs to plain and easily escapable ASCII. The conversion especially helps to avoid Azure Pipelines task.setvariable tripping over fancy Unicode characters (e.g. box drawing).
  • Terraform command output saved in <00N>_<title>.<command>.{log,txt} files (see description below).

Usage: CLI

Usage: ./terraform-pr-comment.sh [arguments]
  -v,--verbose                Advertise detailed steps and actions (pass first for arguments logging)
  -c,--command <name>         Terraform command: fmt, plan, validate
  -p,--logs-path <path>       Location where to look for log files with Terraform command output
  -b,--build-number <number>  Build number or identifier provided by CI/CD service
  -u,--build-url <url>        Build results URL provided by CI/CD service
  -d,--disable-outer-details  Disable outer HTML <details> section
  -h,--help                   Displays this message

Running the script from command line is useful for testing only. The script on its own does not post any comments to GitHub.

Usage: CI/CD

Description

The script collects Terraform command outputs from log files in given location, combines them and renders in Markdown as content for GitHub Pull Request comment, and returns via exported environment variable.

The script renders single comment titled with given build number and command.

The script can read multiple log files from number of runs of the same Terraform command - one run per component of layer of user's infrastructure Each run is rendered as separate section with its own sub-title.

The rendered content can be posted by Azure Pipeline using GitHubComment@0 task or by GitHub Actions using GitHub Script method github.issues.createComment.

Log file name format is <00N>_<title>.<command>.{log,txt} where

  • <00N> part controls order in which files are read
  • <title> part is used as heading of section for given log
  • <command> used in the comment title together with given build number

Notice, that unlike other solutions like terraform-pr-commenter, this script does not search and delete any previous comment it posted. This script always posts a new comment for new build result. It is a very simple script.

Preparing Logs

TODO: Explain how to prepare log files as valid input for this script.

Credits

Thank you all! ~Mateusz Łoskot