This message was deleted.
# general
b
This message was deleted.
πŸ™Œ 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