broad-zoo-34077
04/30/2022, 2:00 AMbusy-agent-35515
04/30/2022, 7:48 AMbreakdown
command instead of diff
with --format json --out-file infracost-diff.json
flags to save the diff json file - here. Then you can use it in infracost comment --path infracost-diff.json ...
. Here's an example how it can look like in a PR comment: https://github.com/vdmgolub/infracost-test/pull/7#issuecomment-1113943100white-airport-8778
name: Terraform directory
on: [pull_request]
jobs:
terraform-directory:
name: Terraform directory
runs-on: ubuntu-latest
steps:
# Checkout the branch you want Infracost to compare costs against. This example is using the
# target PR branch.
- name: Checkout base branch
uses: actions/checkout@v2
with:
ref: '${{ github.event.pull_request.base.ref }}'
- name: Setup Infracost
uses: infracost/actions/setup@v1
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
# Generate an Infracost output JSON from the comparison branch, so that Infracost can compare the cost difference.
- name: Generate Infracost cost snapshot
run: |
infracost breakdown --terraform-parse-hcl --path examples/terraform-directory/code \
--format json \
--out-file /tmp/prior.json
# Checkout the PR branch with your infrastructure changes.
- uses: actions/checkout@v2
- name: Run Infracost
run: |
infracost breakdown --terraform-parse-hcl --path examples/terraform-directory/code \
--format json \
--compare-to /tmp/prior.json \
--out-file /tmp/infracost.json
- name: Post Infracost comment
run: |
# Posts a comment to the PR using the 'update' behavior.
# This creates a single comment and updates it. The "quietest" option.
# The other valid behaviors are:
# delete-and-new - Delete previous comments and create a new one.
# hide-and-new - Minimize previous comments and create a new one.
# new - Create a new cost estimate comment on every push.
# See <https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests> for other options.
infracost comment github --path /tmp/infracost.json \
--repo $GITHUB_REPOSITORY \
--github-token ${{github.token}} \
--pull-request ${{github.event.pull_request.number}} \
--behavior update
Notice how the first checkout is for ${{ github.event.pull_request.base.ref }}
then an Infracost JSON file is produced, followed by a second checkout of the pull request branch thatβs used to generate a second Infracost JSON (comparing it with the first file populates the prior state inside the infracost JSON).
Finally infracost comment
is used to post the comment using the second JSON file that contains the prior and current snapshots.
We realize that this is complicated/confusing, we have ideas to simplify the interface a lot in future versions πjolly-pilot-68525
04/30/2022, 12:26 PMjolly-pilot-68525
04/30/2022, 12:26 PMjolly-pilot-68525
04/30/2022, 12:27 PMinfracost/actions/comment@v1.1.2
vs. the infracost comment command?busy-agent-35515
04/30/2022, 12:46 PMcomment
command to the CLI. infracost comment
is a way to go πwhite-airport-8778