This PR: <https://github.com/infracost/infracost/i...
# general
j
This PR: https://github.com/infracost/infracost/issues/1567#event-6526198147 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: https://github.com/lzysh/terraform-google-project/pull/60/files https://github.com/lzysh/terraform-google-cloud-sql/pull/26/files Am I missing something here?
πŸ™Œ 1
b
@jolly-pilot-68525 Hey! You can use
breakdown
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-1113943100
πŸ‘ 1
w
Adding to what @busy-agent-35515 said, here’s a full example:
Copy code
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 πŸ™‚
πŸ‘ 1
j
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
infracost/actions/comment@v1.1.2
vs. the infracost comment command?
b
None, the comment action used to have a separate compost tool for commenting before we added a dedicated
comment
command to the CLI.
infracost comment
is a way to go πŸ‘
πŸ‘ 1
w
I tweaked the GH Actions readme to clarify the above.
πŸ™Œ 1