This PR: <
# general
This PR: added in some cool support for compare-to (Thanks a lot!). I was hoping to be able to use the output of the diff to comment on a PR similar to the original method. I'm messing around in these two PRs with how I'd expect it to work: Am I missing something here?
πŸ™Œ 1
@jolly-pilot-68525 Hey! You can use
command instead of
--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:
πŸ‘ 1
Adding to what @busy-agent-35515 said, here’s a full example:
Copy code
name: Terraform directory
on: [pull_request]

    name: Terraform directory
    runs-on: ubuntu-latest

      # 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
          ref: '${{ github.event.pull_request.base.ref }}'

      - name: Setup Infracost
        uses: infracost/actions/setup@v1
          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 <> 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 πŸ™‚
πŸ‘ 1
Boom, got it!! It's not too confusing for some reason I was just thinking "diff" based on what I was trying to do.
πŸš€ 1
πŸŽ‰ 1
This is great, I'm going to put it on all my child modules.
Any drivers/tradeoffs for using the
vs. the infracost comment command?
None, the comment action used to have a separate compost tool for commenting before we added a dedicated
command to the CLI.
infracost comment
is a way to go πŸ‘
πŸ‘ 1
I tweaked the GH Actions readme to clarify the above.
πŸ™Œ 1