### Install tfcmt using package managers or direct download
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Demonstrates various methods for installing the tfcmt CLI tool, including Homebrew, aqua package manager, and downloading directly from GitHub Releases. Ensure you have the necessary package managers or tools installed.
```bash
# Install via Homebrew
brew install tfcmt
# Or via Homebrew Cask
brew install --cask suzuki-shunsuke/tfcmt/tfcmt
# Install via aqua
aqua g -i suzuki-shunsuke/tfcmt
# Download from GitHub Releases
gh release download -R suzuki-shunsuke/tfcmt v4.14.0
```
--------------------------------
### Terraform Configuration Example
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/getting-started.md
A minimal Terraform configuration snippet demonstrating a resource that can be commented on.
```hcl
# comment out
```
--------------------------------
### Setup Terraform and tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/getting-started.md
Initializes Terraform, validates the configuration, and generates a plan. It also shows how to set the GitHub token for authentication.
```sh
git clone https://github.com/suzuki-shunsuke/tfcmt
cd tfcmt/examples/getting-started
terraform init
terraform validate
terraform plan
```
```sh
export GITHUB_TOKEN=xxx # your personal access token
```
```sh
PR_NUMBER=70
OWNER=suzuki-shunsuke
REPO=tfcmt
```
--------------------------------
### Full tfcmt Configuration Example
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/config.md
A complete YAML configuration example for tfcmt, demonstrating how to define templates for results, warnings, and errors, and how to apply them to terraform plan and destroy operations.
```YAML
---
repo_owner: suzuki-shunsuke
repo_name: tfcmt
templates:
changed_result: |
{{if .ChangedResult}}
Change Result (Click me)
{{wrapCode .ChangedResult}}
{{end}}
change_outside_terraform: |
{{if .ChangeOutsideTerraform}}
:warning: Note: Objects have changed outside of Terraform
{{wrapCode .ChangeOutsideTerraform}}
{{end}}
warning: |
{{if .Warning}}
## :warning: Warnings :warning:
{{wrapCode .Warning}}
{{end}}
error_message: |
{{if .ErrorMessages}}
## :warning: Errors
{{range .ErrorMessages}}
* {{. -}}
{{- end}}{{end}}
terraform:
plan:
template: |
{{template "plan_title" .}}
{{if .Link}}[CI link]({{.Link}}){{end}}
{{template "result" .}}
{{template "updated_resources" .}}
{{template "changed_result" .}}
{{template "change_outside_terraform" .}}
{{template "warning" .}}
{{template "error_message" .}}
when_destroy:
template: |
{{template "plan_title" .}}
{{if .Link}}[CI link]({{.Link}}){{end}}
{{template "deletion_warning" .}}
{{template "result" .}}
{{template "updated_resources" .}}
{{template "changed_result" .}}
{{template "change_outside_terraform" .}}
{{template "warning" .}}
{{template "error_message" .}}
```
--------------------------------
### Install tfcmt with Homebrew
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/install.md
Installs the tfcmt tool using the Homebrew package manager. This is a straightforward method for macOS and Linux users.
```shell
brew install tfcmt
```
```shell
brew install --cask suzuki-shunsuke/tfcmt/tfcmt
```
--------------------------------
### Install tfcmt with aqua
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/install.md
Installs the tfcmt tool using the aqua package manager. This method is useful for managing multiple tools and their versions.
```shell
aqua g -i suzuki-shunsuke/tfcmt
```
--------------------------------
### Version Option and Command in tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
tfcmt includes a `--version` command-line option and a dedicated `version` command to display the tool's version information. This provides a clear and consistent way to check the installed version.
```console
$ tfcmt --version
tfcmt version 0.1.0
$ tfcmt version
tfcmt version 0.1.0
```
--------------------------------
### Configure tfcmt in GitHub Actions
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Provides a YAML configuration for a GitHub Actions workflow that installs tfcmt and executes plan/apply steps with sensitive data masking.
```yaml
name: Terraform
on: pull_request:
branches: [main]
jobs:
plan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install tfcmt
run: brew install tfcmt
- name: Terraform Plan
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TFCMT_MASKS: 'env:TF_VAR_api_key,env:TF_VAR_db_password'
run: tfcmt plan -patch -- terraform plan -no-color
```
--------------------------------
### Configure Label Colors in tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
tfcmt allows automatic configuration of label colors, eliminating the need for manual setup. This is particularly beneficial for monorepo environments.
--------------------------------
### Validate tfcmt Configuration with ajv-cli
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/config.md
This snippet demonstrates how to use the ajv-cli tool to validate a tfcmt.yaml configuration file against its JSON schema. It requires ajv-cli to be installed and the tfcmt JSON schema file.
```sh
ajv --spec=draft2020 -s json-schema/tfcmt.json -d tfcmt.yaml
```
--------------------------------
### Skip Comment with CLI Option
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/skip-no-changes.md
This example demonstrates how to use the `-skip-no-changes` command-line flag with `tfcmt plan` to avoid posting comments when no Terraform changes are detected. This is useful for keeping pull request discussions clean.
```shell
tfcmt plan -skip-no-changes -- terraform plan
```
--------------------------------
### Use Environment Variables for GitHub Enterprise API URLs
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/github-enterprise.md
Starting from tfcmt v4.12.0, the tool can automatically pick up GitHub Enterprise API URLs from environment variables. This is particularly useful in CI/CD environments like GitHub Actions, where these variables are often pre-defined.
```bash
# For GitHub Enterprise, set these environment variables:
export GITHUB_API_URL="https://example.com/api/v3"
export GITHUB_GRAPHQL_URL="https://example.com/api/graphql"
```
--------------------------------
### Skip Comment with Configuration
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/skip-no-changes.md
This example shows how to configure `tfcmt` to disable comments when there are no changes by setting `disable_comment: true` within the `when_no_changes` block in the `tfcmt.yaml` configuration file. This provides a persistent way to manage comment behavior.
```yaml
terraform:
plan:
when_no_changes:
disable_comment: true
```
--------------------------------
### Masking a Terraform Variable with tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/mask-sensitive-data.md
This example shows how to mask a specific Terraform variable, TF_VAR_gh_token, using the TFCMT_MASKS environment variable before running `tfcmt plan`. This prevents the sensitive token from appearing in the plan output.
```shell
export TFCMT_MASKS=env:TF_VAR_gh_token # Mask the environment variable TF_VAR_gh_token
```
--------------------------------
### Patch tfcmt plan comments in Monorepos
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/plan-patch.md
This example shows how to use the `-patch` option with the `target` variable in `tfcmt plan` for monorepos. This allows you to specify which comments to update by targeting specific root modules or tfstates, helping to manage comments in complex project structures.
```sh
cd /path/to/root-modules/dev
tfcmt -var 'target:dev' plan -patch -- terraform plan -no-color
cd /path/to/root-modules/prd
tfcmt -var 'target:prd' plan -patch -- terraform plan -no-color
```
--------------------------------
### Configure YAML Language Server Schema for tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/config.md
These examples show how to configure the yaml-language-server to use the tfcmt JSON schema for input complementation (autocompletion and validation) within YAML files. You can either use the latest schema from the main branch or pin it to a specific version.
```yaml
# yaml-language-server: $schema=https://raw.githubusercontent.com/suzuki-shunsuke/tfcmt/main/json-schema/tfcmt.json
```
```yaml
# yaml-language-server: $schema=https://raw.githubusercontent.com/suzuki-shunsuke/tfcmt/v4.14.1/json-schema/tfcmt.json
```
--------------------------------
### Terraform Version Support >= v0.15 Message Handling
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
This section addresses changes in Terraform's output message for plans with no changes starting from version v0.15. tfcmt has been updated to support both the older and newer message formats, ensuring compatibility.
```text
AS IS
```
No changes. Infrastructure is up-to-date.
```
TO BE
```
No changes. Your infrastructure matches the configuration.
```
tfcmt supports both messages.
```
--------------------------------
### Execute Terragrunt run-all with tfcmt using tfwrapper.sh
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/terragrunt.md
Demonstrates how to execute 'terragrunt plan --all' or 'terragrunt run-all plan' while integrating with tfcmt using a custom Terraform wrapper script. This ensures that tfcmt can correctly parse and report on the plans for each individual module managed by Terragrunt.
```shell
# Latest Terragrunt (>= 0.85.0)
terragrunt plan --all --tf-path ""
# Old Terragrunt (< 0.85.0)
terragrunt run-all plan --terragrunt-tfpath ""
```
--------------------------------
### Configure tfcmt using environment variables
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Explains how to configure tfcmt using environment variables, which is useful for CI/CD pipelines. Covers essential variables like `GITHUB_TOKEN`, repository details (`TFCMT_REPO_OWNER`, `TFCMT_REPO_NAME`, `TFCMT_PR_NUMBER`), and optional settings like `TFCMT_CONFIG` and `TFCMT_DISABLE_LABEL`. Also includes specific variables for Google Cloud Build integration.
```bash
# Required: GitHub access token
export GITHUB_TOKEN="ghp_xxxxxxxxxxxx"
# Or use tfcmt-specific variable (v4.8.0+)
export TFCMT_GITHUB_TOKEN="ghp_xxxxxxxxxxxx"
# Repository configuration
export TFCMT_REPO_OWNER="myorg"
export TFCMT_REPO_NAME="myrepo"
export TFCMT_PR_NUMBER=123
export TFCMT_SHA="abc123def"
# Optional: specify config file path
export TFCMT_CONFIG="/path/to/tfcmt.yaml"
# Optional: disable labels globally (v4.10.0+)
export TFCMT_DISABLE_LABEL=true
# For Google Cloud Build support
export GOOGLE_CLOUD_BUILD=true
export COMMIT_SHA="abc123"
export BUILD_ID="build-123"
export PROJECT_ID="my-project"
export _PR_NUMBER=123
export _REGION="us-central1"
# Run tfcmt without explicit flags
tfcmt plan -- terraform plan
```
--------------------------------
### Simplify CI and Repository Configuration
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
Demonstrates how to omit explicit CI and repository owner/name fields in the configuration file by allowing tfcmt to infer them from environment variables.
```yaml
notifier:
github:
token: $GITHUB_TOKEN
```
--------------------------------
### Verify tfcmt binaries with Cosign
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/install.md
Verifies downloaded tfcmt binaries from GitHub Releases using Cosign. This process involves downloading assets, verifying a checksum file's signature and certificate, and then checking the checksums.
```shell
aqua g -i sigstore/cosign
gh release download -R suzuki-shunsuke/tfcmt v4.14.0
cosign verify-blob \
--signature tfcmt_4.14.0_checksums.txt.sig \
--certificate tfcmt_4.14.0_checksums.txt.pem \
--certificate-identity-regexp 'https://github\.com/suzuki-shunsuke/go-release-workflow/\.github/workflows/release\.yaml@.*' \
--certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
tfcmt_4.14.0_checksums.txt
cat tfcmt_4.14.0_checksums.txt | sha256sum -c --ignore-missing
```
--------------------------------
### tfcmt apply
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/usage.md
Runs `terraform apply` and posts a comment to GitHub.
```APIDOC
## tfcmt apply
### Description
Run terraform apply and post a comment to GitHub commit, pull request, or issue.
### Method
N/A (CLI Tool)
### Endpoint
N/A (CLI Tool)
### Parameters
#### Command Options
- **--help, -h** (boolean) - show help
### Usage Example
```bash
tfcmt apply -- terraform apply
```
```
--------------------------------
### CI Command-Line Options in tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
tfcmt provides command-line options for CI-related parameters such as owner, repository, pull request number, SHA, and build URL. This contrasts with tools that rely solely on environment variables, enabling tfcmt to function in diverse CI platforms.
```console
$ tfcmt -owner suzuki-shunsuke -repo tfcmt -pr 3 -- terraform plan
```
--------------------------------
### tfcmt plan
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/usage.md
Runs `terraform plan` and posts a comment to GitHub.
```APIDOC
## tfcmt plan
### Description
Run terraform plan and post a comment to GitHub commit, pull request, or issue.
### Method
N/A (CLI Tool)
### Endpoint
N/A (CLI Tool)
### Parameters
#### Command Options
- **--patch** (boolean) - update an existing comment instead of creating a new comment.
- **--skip-no-changes** (boolean) - If there is no change tfcmt updates a label but doesn't post a comment.
- **--ignore-warning** (boolean) - If skip-no-changes is enabled, comment is posted even if there is a warning.
- **--disable-label** (boolean) - Disable to add or update a label
- **--help, -h** (boolean) - show help
### Usage Example
```bash
tfcmt plan -- terraform plan
```
```
--------------------------------
### Configure Custom Terraform Plan and Apply Templates
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Defines custom templates for plan and apply operations using Go template syntax. These templates allow for conditional formatting based on exit codes, resource changes, and custom variables.
```yaml
templates:
plan_title: "## {{if eq .ExitCode 1}}:x: {{end}}Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}}"
apply_title: "## :{{if eq .ExitCode 0}}white_check_mark{{else}}x{{end}}: Apply Result{{if .Vars.target}} ({{.Vars.target}}){{end}}"
result: "{{if .Result}}{{ .Result }}
{{end}}"
deletion_warning: |
{{if .HasDestroy}}
### :warning: Resource Deletion will happen :warning:
This plan contains resource delete operation. Please check the plan result very carefully!
{{end}}
```
--------------------------------
### tfcmt plan: Post Terraform plan results to GitHub PRs
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Shows how to use the `tfcmt plan` command to post Terraform plan outputs as comments on GitHub pull requests. It covers basic usage, auto-detection on CI platforms, updating existing comments, skipping no-change notifications, disabling labels, monorepo support, outputting to files, and enabling debug mode. Requires GitHub credentials and a Terraform plan.
```bash
# Basic usage - posts plan result to PR
tfcmt --owner "myorg" --repo "myrepo" --pr 123 plan -- terraform plan
# On supported CI platforms, parameters are auto-detected
tfcmt plan -- terraform plan
# Use -patch to update existing comment instead of creating new one
tfcmt plan -patch -- terraform plan -no-color
# Skip posting comment if there are no changes
tfcmt plan -skip-no-changes -- terraform plan
# Disable automatic labeling
tfcmt plan --disable-label -- terraform plan
# Monorepo support with target variable
tfcmt -var "target:production" plan -- terraform plan
# Output to local file instead of posting to GitHub
tfcmt --output plan.md plan -- terraform plan
# Debug mode for troubleshooting
tfcmt -log-level=debug plan -patch -- terraform plan -no-color
```
--------------------------------
### Configure Google Cloud Build Environment
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/environment-variable.md
Enables tfcmt support for Google Cloud Build by setting the required environment variable. This configuration allows tfcmt to integrate with Cloud Build's substitution variables.
```shell
GOOGLE_CLOUD_BUILD=true
```
--------------------------------
### Display Warning Variable
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/config.md
Displays a warning header and content if the Warning variable is populated. This ensures that critical issues are highlighted in the output.
```Go Template
{{if .Warning}}
## :warning: Warnings :warning:
{{wrapCode .Warning}}
{{end}}
```
--------------------------------
### Manage Monorepo Terraform States
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Shows how to use the -var target flag to differentiate between multiple Terraform states within a single repository.
```bash
cd infrastructure/production
tfcmt -var "target:production" plan -patch -- terraform plan
cd infrastructure/staging
tfcmt -var "target:staging" plan -patch -- terraform plan
```
--------------------------------
### tfcmt apply: Post Terraform apply results to GitHub PRs
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Demonstrates the `tfcmt apply` command for posting Terraform apply outcomes as comments on GitHub pull requests. Includes basic usage, auto-detection on CI platforms, monorepo support, and outputting results to a local file. Requires GitHub credentials and a successful Terraform apply.
```bash
# Basic usage - posts apply result to PR
tfcmt --owner "myorg" --repo "myrepo" --pr 123 apply -- terraform apply -auto-approve
# On supported CI platforms
tfcmt apply -- terraform apply -auto-approve
# With monorepo target
tfcmt -var "target:staging" apply -- terraform apply -auto-approve
# Output to local file
tfcmt --output apply.md apply -- terraform apply -auto-approve
```
--------------------------------
### Wrapper Script for Terragrunt run-all with tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/terragrunt.md
This script acts as a wrapper for the Terraform executable, enabling tfcmt to process the output from 'terragrunt run-all' commands. It identifies the current module's target and passes it to tfcmt for plan and apply operations, ensuring per-module reporting. The script requires appropriate permissions to be executable.
```shell
#!/bin/bash
set -euo pipefail
command=$1
base_dir=$(git rev-parse --show-toplevel) # Please fix if necessary
target=${PWD#"$base_dir"/}
if [ "$command" == "plan" ]; then
tfcmt -var "target:${target}" plan -- terraform "$@"
elif [ "$command" == "apply" ]; then
tfcmt -var "target:${target}" apply -- terraform "$@"
else
terraform "$@"
fi
```
--------------------------------
### Execute tfcmt with CI environment variables
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/getting-started.md
When running on supported CI platforms, tfcmt automatically detects repository and PR information, simplifying the command execution.
```shell
tfcmt plan -- terraform plan
```
--------------------------------
### Verify tfcmt binaries with GitHub CLI
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/install.md
Verifies downloaded tfcmt binaries from GitHub Releases using the GitHub CLI's attestation verification feature. This confirms the artifact was built by a trusted workflow.
```shell
aqua g -i cli/cli
gh release download -R suzuki-shunsuke/tfcmt v4.14.0 -p tfcmt_darwin_arm64.tar.gz
gh attestation verify tfcmt_darwin_arm64.tar.gz \
-R suzuki-shunsuke/tfcmt \
--signer-workflow suzuki-shunsuke/go-release-workflow/.github/workflows/release.yaml
```
--------------------------------
### Integrate tfcmt with Terragrunt
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Provides a wrapper script to bridge Terragrunt run-all commands with tfcmt for multi-module deployments.
```bash
#!/bin/bash
set -euo pipefail
command=$1
base_dir=$(git rev-parse --show-toplevel)
target=${PWD#"$base_dir"/}
if [ "$command" == "plan" ]; then
tfcmt -var "target:${target}" plan -- terraform "$@"
elif [ "$command" == "apply" ]; then
tfcmt -var "target:${target}" apply -- terraform "$@"
else
terraform "$@"
fi
```
--------------------------------
### Verify tfcmt binaries with slsa-verifier
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/install.md
Verifies downloaded tfcmt binaries from GitHub Releases using slsa-verifier. This checks the artifact's integrity against its provenance data.
```shell
aqua g -i slsa-framework/slsa-verifier
gh release download -R suzuki-shunsuke/tfcmt v4.14.0
slsa-verifier verify-artifact tfcmt_darwin_arm64.tar.gz \
--provenance-path multiple.intoto.jsonl \
--source-uri github.com/suzuki-shunsuke/tfcmt \
--source-tag v4.14.0
```
--------------------------------
### Customize tfcmt behavior with a YAML configuration file
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Illustrates how to create and use a `tfcmt.yaml` file to customize tfcmt's behavior, such as setting repository details, GitHub Enterprise configurations, enabling comment patching, and specifying embedded variable names for metadata. This provides a declarative way to manage tfcmt settings.
```yaml
# yaml-language-server: $schema=https://raw.githubusercontent.com/suzuki-shunsuke/tfcmt/main/json-schema/tfcmt.json
# Repository configuration (optional if using environment variables)
repo_owner: myorg
repo_name: myrepo
# GitHub Enterprise configuration (optional)
ghe_base_url: https://github.example.com
ghe_graphql_endpoint: https://github.example.com/api/graphql
# Update existing comments instead of creating new ones
plan_patch: true
# Embed variables in comment metadata for github-comment hide
embedded_var_names:
- target
```
--------------------------------
### Run Terraform Plan with tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/usage.md
Executes a terraform plan command and posts the result as a comment on a GitHub pull request or commit. It supports flags like --patch to update existing comments and --skip-no-changes to manage label updates without posting.
```bash
tfcmt [] plan [-patch] [-skip-no-changes] -- terraform plan []
```
--------------------------------
### Configuration for Deletion Warning Comment
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
This YAML configuration snippet defines how deletion warnings are handled. It specifies the label, label color, and a template for comments when a plan involves resource deletion. The 'when_destroy' block ensures a clear warning is posted.
```yaml
when_destroy:
label: "destroy"
label_color: "d93f0b" # red
template: |
## Plan Result
[CI link]( {{ .Link }} )
This plan contains **resource deletion**. Please check the plan result very carefully!
{{if .Result}}
{{ .Result }}
{{end}}
Details (Click me)
{{ .CombinedOutput }}
```
--------------------------------
### Configure GitHub Enterprise URL in tfcmt.yaml
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/github-enterprise.md
This snippet shows how to specify the base URL and GraphQL endpoint for GitHub Enterprise in the `tfcmt.yaml` configuration file. This is necessary for tfcmt to communicate with a self-hosted GitHub Enterprise instance.
```yaml
ghe_base_url: "https://example.com"
ghe_graphql_endpoint: "https://example.com/api/graphql"
```
--------------------------------
### Enable Shell Completion
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Commands to enable auto-completion for tfcmt in various shell environments.
```bash
source <(tfcmt completion bash)
source <(tfcmt completion zsh)
tfcmt completion fish > ~/.config/fish/completions/tfcmt.fish
tfcmt completion powershell > tfcmt.ps1
```
--------------------------------
### tfcmt version
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/usage.md
Shows the version of the tfcmt CLI.
```APIDOC
## tfcmt version
### Description
Show version.
### Method
N/A (CLI Tool)
### Endpoint
N/A (CLI Tool)
### Parameters
#### Command Options
- **--json, -j** (boolean) - Output version in JSON format
- **--help, -h** (boolean) - show help
```
--------------------------------
### Configure Log Level and Output Structured Logs in tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
Users can configure the log level for tfcmt using the `--log-level` option or a `log.level` setting in a configuration file. The tool outputs structured logs using the `logrus` library, aiding in debugging and monitoring.
```console
$ tfcmt --log-level debug plan -- terraform plan
```
```yaml
---
log:
level: debug
```
--------------------------------
### Post Terraform Plan to Pull Request with tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/getting-started.md
Uses the `tfcmt plan` command to post the output of `terraform plan` as a comment on a GitHub pull request. It supports automatic labeling based on the plan's outcome (no changes, add/update, destroy).
```sh
tfcmt -owner "$OWNER" -repo "$REPO" -pr "$PR_NUMBER" plan -- terraform plan
```
--------------------------------
### Enable Patching for tfcmt plan
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/plan-patch.md
This snippet demonstrates how to enable the patch functionality for `tfcmt plan` using both command-line flags and configuration files. The command-line option `-patch` takes precedence over the `plan_patch` configuration. To disable patching when `plan_patch` is true, use `-patch=false`.
```sh
tfcmt plan -patch -- terraform plan -no-color
```
```yaml
plan_patch: true
```
```sh
tfcmt plan -patch=false -- terraform plan -no-color
```
--------------------------------
### Troubleshoot tfcmt plan patching with debug logs
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/plan-patch.md
If the comment patching in `tfcmt plan` is not working as expected, enable debug logging by setting the `-log-level=debug` flag. This will provide detailed output to help diagnose and resolve any issues.
```sh
tfcmt -log-level=debug plan -patch -- terraform plan -no-color
```
--------------------------------
### Run Terraform Apply with tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/usage.md
Executes a terraform apply command and posts the execution result as a comment on a GitHub pull request or commit. This command wraps standard terraform apply operations to provide automated feedback.
```bash
tfcmt [] apply -- terraform apply []
```
--------------------------------
### tfcmt completion
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/usage.md
Outputs shell completion script for various shells.
```APIDOC
## tfcmt completion
### Description
Output shell completion script for bash, zsh, fish, or Powershell.
### Method
N/A (CLI Tool)
### Endpoint
N/A (CLI Tool)
### Parameters
#### Command Options
- **--help, -h** (boolean) - show help
### Usage Example
```bash
# .bashrc
source <(tfcmt completion bash)
# .zshrc
source <(tfcmt completion zsh)
# fish
tfcmt completion fish > ~/.config/fish/completions/tfcmt.fish
# Powershell
# Output the script to path/to/autocomplete/tfcmt.ps1 an run it.
```
```
--------------------------------
### Template Variable and Option Removal: Message and Title
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
The '--message', '--destroy-warning-message' options and '.Message' template variable, along with '--title', '--destroy-warning-title' options and '.Title' template variable, have been removed. They are replaced by the more general '-var' option and '.Vars' template variable for greater flexibility.
```go
We introduced more general option `-var` and template variable `.Vars`, so the `--message` and `--destroy-warning-message` options aren't needed.
We introduced more general option `-var` and template variable `.Vars`, so the `--title` and `--destroy-warning-title` options aren't needed.
```
--------------------------------
### tfcmt Global Options
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/usage.md
Global options that can be used with any tfcmt command.
```APIDOC
## tfcmt Global Options
### Description
Global options that can be used with any tfcmt command.
### Method
N/A (CLI Tool)
### Endpoint
N/A (CLI Tool)
### Parameters
#### Global Options
- **--owner** (string) - GitHub Repository owner name
- **--repo** (string) - GitHub Repository name
- **--sha** (string) - commit SHA (revision)
- **--build-url** (string) - build url
- **--log-level** (string) - log level
- **--pr** (int) - pull request number
- **--config** (string) - config path
- **--var** (string) - template variables. The format of value is ':'.
- **--output** (string) - specify file to output result instead of posting a comment
- **--help, -h** (boolean) - show help
- **--version, -v** (boolean) - print the version
```
--------------------------------
### Display Terraform Resource Changes in Templates
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
This snippet demonstrates how to use template variables like CreatedResources, UpdatedResources, DeletedResources, and ReplacedResources to dynamically list changes in a Terraform plan report.
```text
{{if .CreatedResources}}
* Create
{{- range .CreatedResources}}
* {{.}}
{{- end}}{{end}}{{if .UpdatedResources}}
* Update
{{- range .UpdatedResources}}
* {{.}}
{{- end}}{{end}}{{if .DeletedResources}}
* Delete
{{- range .DeletedResources}}
* {{.}}
{{- end}}{{end}}{{if .ReplacedResources}}
* Replace
{{- range .ReplacedResources}}
* {{.}}
{{- end}}{{end}}
```
--------------------------------
### Post Terraform Apply to Pull Request with tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/getting-started.md
Uses the `tfcmt apply` command to post the result of `terraform apply` as a comment on a GitHub pull request, providing a summary of the applied changes.
```sh
tfcmt -owner "$OWNER" -repo "$REPO" -pr "$PR_NUMBER" apply -- terraform apply -auto-approve
```
--------------------------------
### Configure tfcmt Masking with Environment Variables
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/mask-sensitive-data.md
This snippet demonstrates how to configure tfcmt to mask sensitive data by setting environment variables. It shows how to specify masks using 'env' or 'regexp' types and how to change the default separator.
```shell
export TFCMT_MASKS='env:GITHUB_TOKEN,env:DATADOG_API_KEY,regexp:ghp_[^ ]+'
tfcmt plan -- terraform plan
```
```shell
export TFCMT_MASKS_SEPARATOR=/
export TFCMT_MASKS='env:GITHUB_TOKEN/env:DATADOG_API_KEY/regexp:ghp_[^ ]+'
```
--------------------------------
### Configure target variable for monorepo support
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/getting-started.md
Use the -var flag to specify a target identifier, allowing tfcmt to distinguish between multiple Terraform states within a single repository.
```shell
tfcmt -owner "$OWNER" -repo "$REPO" -pr "$PR_NUMBER" -var "target:foo" plan -- terraform plan
```
--------------------------------
### Configure Parse Error Templates
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
This YAML configuration defines custom templates for handling Terraform plan or apply failures, allowing users to output the combined error output in a GitHub comment.
```yaml
terraform:
plan:
when_parse_error:
template: |
## Plan Result [CI link]( {{ .Link }} )
:warning: It failed to parse the result. :warning:
Details (Click me)
{{ .CombinedOutput }}
apply:
when_parse_error:
template: |
## Apply Result [CI link]( {{ .Link }} )
:warning: It failed to parse the result. :warning:
Details (Click me)
{{ .CombinedOutput }}
```
--------------------------------
### Configure Templates and Use Built-in Templates in tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
This feature allows users to configure custom templates and leverage built-in templates for Terraform plan and apply results. The configuration is done via a YAML file, specifying template titles and content. Built-in templates like 'plan_title', 'apply_title', 'result', 'updated_resources', and 'deletion_warning' can be overridden.
```yaml
templates:
title: "## Plan Result ({{.Vars.target}})"
terraform:
plan:
template: |
{{template "title" .}}
```
--------------------------------
### Manage Comments with github-comment
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Instructions for using github-comment to hide outdated tfcmt PR comments.
```bash
aqua g -i suzuki-shunsuke/github-comment
github-comment hide
```
--------------------------------
### Mask Sensitive Terraform Variables
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Demonstrates how to use the TFCMT_MASKS environment variable to redact sensitive information from Terraform output logs.
```bash
export TF_VAR_gh_token="ghp_actual_secret_token"
export TFCMT_MASKS='env:TF_VAR_gh_token'
tfcmt plan -- terraform plan
```
--------------------------------
### Generate Shell Completion for tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/usage.md
Outputs shell completion scripts for various shells including bash, zsh, fish, and Powershell. This enables command-line auto-completion for tfcmt commands.
```bash
# .bashrc
source <(tfcmt completion bash)
# .zshrc
source <(tfcmt completion zsh)
# fish
tfcmt completion fish > ~/.config/fish/completions/tfcmt.fish
```
--------------------------------
### Mask Sensitive Data in Terraform Output
Source: https://context7.com/suzuki-shunsuke/tfcmt/llms.txt
Demonstrates how to use the TFCMT_MASKS environment variable to prevent sensitive data like tokens and keys from appearing in GitHub comments. Supports both direct environment variable names and regex pattern matching.
```bash
# Mask environment variables by name
export TFCMT_MASKS='env:GITHUB_TOKEN,env:DATADOG_API_KEY,env:AWS_SECRET_ACCESS_KEY'
tfcmt plan -- terraform plan
# Mask using regex patterns
export TFCMT_MASKS='regexp:ghp_[^ ]+,regexp:sk-[a-zA-Z0-9]+'
tfcmt plan -- terraform plan
# Change separator
export TFCMT_MASKS_SEPARATOR='/'
export TFCMT_MASKS='env:GITHUB_TOKEN/env:API_KEY/regexp:secret_[a-z]+'
tfcmt plan -- terraform plan
```
--------------------------------
### Define Repository Metadata in tfcmt.yaml
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/environment-variable.md
Sets the repository owner and name within the tfcmt configuration file. This is required when using tfcmt with platforms like Google Cloud Build to identify the target repository.
```yaml
repo_owner: suzuki-shunsuke
repo_name: tfcmt
```
--------------------------------
### Default tfcmt Configuration (YAML)
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/config.md
This YAML configuration defines default templates for Terraform plan and apply actions within tfcmt. It includes templates for titles, results, resource changes, warnings, and error messages, along with specific configurations for different plan outcomes (add/update, destroy, no changes, plan error, parse error).
```yaml
embedded_var_names: []
templates:
plan_title: "## {{if eq .ExitCode 1}}:x: {{end}}Plan Result{{if .Vars.target}} ({{.Vars.target}}){{end}}"
apply_title: "## :{{if eq .ExitCode 0}}white_check_mark{{else}}x{{end}}: Apply Result{{if .Vars.target}} ({{.Vars.target}}){{end}}"
result: "{{if .Result}}{{ .Result }}
{{end}}"
updated_resources: |
{{if .CreatedResources}}
* Create
{{- range .CreatedResources}}
* {{.}}
{{- end}}{{end}}{{if .UpdatedResources}}
* Update
{{- range .UpdatedResources}}
* {{.}}
{{- end}}{{end}}{{if .DeletedResources}}
* Delete
{{- range .DeletedResources}}
* {{.}}
{{- end}}{{end}}{{if .ReplacedResources}}
* Replace
{{- range .ReplacedResources}}
* {{.}}
{{- end}}{{end}}{{if .ImportedResources}}
* Import
{{- range .ImportedResources}}
* {{.}}
{{- end}}{{end}}{{if .MovedResources}}
* Move
{{- range .MovedResources}}
* {{.Before}} => {{.After}}
{{- end}}{{end}}
deletion_warning: |
{{if .HasDestroy}}
### :warning: Resource Deletion will happen :warning:
This plan contains resource delete operation. Please check the plan result very carefully!
{{end}}
changed_result: |
{{if .ChangedResult}}
Change Result (Click me)
{{wrapCode .ChangedResult}}
{{end}}
change_outside_terraform: |
{{if .ChangeOutsideTerraform}}
:information_source: Objects have changed outside of Terraform
_This feature was introduced from [Terraform v0.15.4](https://github.com/hashicorp/terraform/releases/tag/v0.15.4)._
{{wrapCode .ChangeOutsideTerraform}}
{{end}}
warning: |
{{if .Warning}}
## :warning: Warnings :warning:
{{wrapCode .Warning}}
{{end}}
error_messages: |
{{if .ErrorMessages}}
## :warning: Errors
{{range .ErrorMessages}}
* {{. -}}
{{- end}}{{end}}
guide_apply_failure: ""
guide_apply_parse_error: ""
terraform:
plan:
disable_label: false
ignore_warning: false # tfcmt >= v4.14.0
template: |
{{template "plan_title" .}}
{{if .Link}}[CI link]({{.Link}}){{end}}
{{template "deletion_warning" .}}
{{template "result" .}}
{{template "updated_resources" .}}
{{template "changed_result" .}}
{{template "change_outside_terraform" .}}
{{template "warning" .}}
{{template "error_messages" .}}
when_add_or_update_only:
label: "{{if .Vars.target}}{{.Vars.target}} /{{end}}add-or-update"
label_color: 1d76db # blue
# disable_label: false
when_destroy:
label: "{{if .Vars.target}}{{.Vars.target}} /{{end}}destroy"
label_color: d93f0b # red
# disable_label: false
when_no_changes:
label: "{{if .Vars.target}}{{.Vars.target}} /{{end}}no-changes"
label_color: 0e8a16 # green
# disable_label: false
# disable_comment: false
when_plan_error:
label:
label_color:
# disable_label: false
when_parse_error:
template: |
{{template "plan_title" .}}
{{if .Link}}[CI link]({{.Link}}){{end}}
It failed to parse the result.
Details (Click me)
{{wrapCode .CombinedOutput}}
apply:
template: |
{{template "apply_title" .}}
{{if .Link}}[CI link]({{.Link}}){{end}}
{{if ne .ExitCode 0}}{{template "guide_apply_failure" .}}{{end}}
{{template "result" .}}
Details (Click me)
{{wrapCode .CombinedOutput}}
{{template "error_messages" .}}
when_parse_error:
template: |
{{template "apply_title" .}}
{{if .Link}}[CI link]({{.Link}}){{end}}
{{template "guide_apply_parse_error" .}}
It failed to parse the result.
Details (Click me)
{{wrapCode .CombinedOutput}}
```
--------------------------------
### Display ChangeOutsideTerraform Variable
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/config.md
Displays a warning note for objects changed outside of Terraform. It uses a collapsible details element to present the data clearly.
```Go Template
{{if .ChangeOutsideTerraform}}
:warning: Note: Objects have changed outside of Terraform
{{wrapCode .ChangeOutsideTerraform}}
{{end}}
```
--------------------------------
### Terraform Configuration for Sensitive Data
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/mask-sensitive-data.md
This is a Terraform configuration file that defines a google_cloudbuild_trigger resource. It includes a variable for a GitHub access token, which is intended to be sensitive and masked.
```hcl
resource "google_cloudbuild_trigger" "filename_trigger" {
location = "us-central1"
trigger_template {
branch_name = "main"
repo_name = "my-repo"
}
substitutions = {
_GH_TOKEN = var.gh_token # Secret
}
filename = "cloudbuild.yaml"
}
variable "gh_token" {
type = string
description = "GitHub Access token"
}
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "5.13.0"
}
}
}
```
--------------------------------
### Update Command Usage: tfnotify to tfcmt
Source: https://github.com/suzuki-shunsuke/tfcmt/blob/main/website/docs/compared-with-tfnotify.md
This snippet shows the transition from the older 'tfnotify' command usage to the new 'tfcmt' command usage. The change allows tfcmt to better handle standard error output and exit codes from Terraform commands.
```bash
AS IS
```
terraform plan | tfnotify plan
terraform apply | tfnotify apply
```
TO BE
```
tfcmt plan -- terraform plan
tfcmt apply -- terraform apply
```
```