A CLI command to parse Terraform execution result and notify it to GitHub
A CLI command to parse Terraform execution result and notify it to GitHub
Tfnotify is a Go-based template engine that creates comments on Terraform operations. It pipes commands directly and captures operation outputs, including exit codes and error details and offers to post them as a Github Comment, with AI summaries and analysis based on custom templates.
There are commands such as plan and apply on Terraform command, but many developers think they would like to check if the execution of those commands succeeded.
Terraform commands are often executed via CI like Cloudbuild CI/Github Actions, but in that case you need to go to the CI page to check it.
This is very troublesome. It is very efficient if you can check it with the supported CI.
Grab the binary from GitHub Releases (Recommended)
or
$ go get -u github.com/mercari/tfnotifyTfnotify supports the following platforms:
tfnotify is just CLI command. So you can run it from your local after grabbing the binary.
Basically tfnotify waits for the input from Stdin. So tfnotify needs to pipe the output of Terraform command like the following:
$ tfnotify planFor plan command, you also need to specify plan as the argument of tfnotify. In the case of apply, you need to do apply. Currently supported commands can be checked with tfnotify --help.
When running tfnotify, you can specify the configuration path via --config option (if it's omitted, it defaults to {.,}tfnotify.y{,a}ml).
The example settings of GitHub and GitHub Enterprise. Incidentally, there is no need to replace TOKEN string such as $GITHUB_TOKEN with the actual token. Instead, it must be defined as environment variables in CI settings.
template of Go can be used for template. The templates can be used in tfnotify.yaml are as follows:
| Placeholder | Usage |
|---|---|
{{ .Title }} |
Like ## Plan result |
{{ .Message }} |
A string that can be set from CLI with --message option |
{{ .Result }} |
Matched result by parsing like Plan: 1 to add or No changes |
{{ .Body }} |
The entire of Terraform execution result |
{{ .Link }} |
The link of the build page on CI |
On GitHub, tfnotify can also put a warning message if the plan result contains resource deletion (optional).
For GitHub
…If you would like to let tfnotify warn the resource deletion, add when_destroy configuration as below.
---
# ...
terraform:
# ...
plan:
template: |
{{ .Title }} [CI link]( {{ .Link }} )
{{ .Message }}
{{if .Result}}
{{ .Result }}
{{end}}
Details (Click me)
{{ .Body }}
when_destroy:
template: |
## :warning: WARNING: Resource Deletion will happen :warning:
This plan contains **resource deletion**. Please check the plan result very carefully!
# ...You can also let tfnotify add a label to PRs depending on the terraform plan output result. Currently, this feature is for Github labels only.
---
# ...
terraform:
# ...
plan:
template: |
{{ .Title }} [CI link]( {{ .Link }} )
{{ .Message }}
{{if .Result}}
{{ .Result }}
{{end}}
Details (Click me)
{{ .Body }}
when_add_or_update_only:
label: "add-or-update"
when_destroy:
label: "destroy"
when_no_changes:
label: "no-changes"
when_plan_error:
label: "error"
# ...Sometimes you may want not to HTML-escape Terraform command outputs.
For example, when you use code block to print command output, it's better to use raw characters instead of character references (e.g. -/+ -> -/+, " -> ").
You can disable HTML escape by adding use_raw_output: true configuration.
With this configuration, Terraform doesn't HTML-escape any Terraform output.
---
# ...
terraform:
use_raw_output: true
# ...
plan:
template: |
{{ .Title }} [CI link]( {{ .Link }} )
{{ .Message }}
{{if .Result}}
```
{{ .Result }}
```
{{end}}
Details (Click me)
```
{{ .Body }}
```
# ...For GitHub Enterprise
…COMMIT_SHABUILD_IDPROJECT_ID_PR_NUMBER_REGIONterraform plan: Pull requestterraform apply: Push to branchPlease read the CLA below carefully before submitting your contribution.
Licensed under the MIT License. See LICENSE for details.
Copyright © 2018 Mercari, Inc.
This project includes substantial portions of code from suzuki-shunsuke/tfcmt.
Copyright © 2020 Shunsuke Suzuki
No open issues yet, or sync has not completed.