infracost comment: Post Cost Estimates on a PR
infracost comment github posts the cost diff as a pull request comment and can update it in place on each push.
The payoff of infracost diff is a comment reviewers actually see. The comment command takes the diff JSON and writes it to the PR, updating the same comment so the thread does not fill with duplicates.
What it does
infracost comment github takes a saved diff JSON and posts it as a PR comment using a GitHub token. The --behavior flag controls whether it updates the existing comment, deletes and reposts, or always adds a new one. Equivalent subcommands exist for GitLab, Azure DevOps, and Bitbucket.
Common usage
infracost diff --path . --compare-to base.json \
--format json --out-file infracost.json
infracost comment github \
--path infracost.json \
--repo "$GITHUB_REPOSITORY" \
--pull-request "$PR_NUMBER" \
--github-token "$GITHUB_TOKEN" \
--behavior updateOptions
| Flag | What it does |
|---|---|
| --path <file> | The diff or breakdown JSON to render |
| --repo / --pull-request | Target repository and PR number |
| --github-token <token> | Token with permission to comment |
| --behavior <mode> | update, new, delete-and-new, or hide-and-new |
| --tag <name> | Label to identify the comment for updates |
In CI
Use --behavior update so each push edits the one comment instead of spamming the PR. The token needs pull-requests: write (the default GITHUB_TOKEN works once you grant that permission in the workflow). Generate the JSON with infracost diff ... --format json first; the comment command renders that file, it does not re-price.
Common errors in CI
"Resource not accessible by integration" or a 403 means the token lacks pull-requests: write; add the permission. "could not load ... infracost.json" means the diff step did not write the file or the path is wrong. On pull_request events from forks the default token is read-only, so the comment fails; use pull_request_target carefully or a PAT.