hey everyone, we are running Infracost in a GitHub...
# help
b
hey everyone, we are running Infracost in a GitHub workflow, and we are seeing an error which doesn’t tell us much: > Error: template: markdown-html.tmpl89111: executing “markdown-html.tmpl” at .PastBreakdown.TotalMonthlyCost: nil pointer evaluating *output.Breakdown.TotalMonthlyCost followed by: > 2024-02-22T143920Z ERR request failed error Post “https://pricing.api.infracost.io/event”: context deadline exceeded (Client.Timeout exceeded while awaiting headers) method POST url https://pricing.api.infracost.io/event library=retryablehttp Can anyone share their thoughts about what we’re doing wrong? Note: other three jobs in the same pipeline complete successfully and they are using Infracost as well.
l
Hi @billowy-electrician-57321 can you send me the workflow code you’re using?
b
sure
Copy code
on:
  workflow_call:
    inputs:
      environment:
        required: true
        type: string
      project-directory:
        required: true
        type: string

permissions:
  id-token: write       # This is required for requesting the JWT
  contents: read        # This is required for actions/checkout
  pull-requests: write  # Required to post comments

jobs:
  infracost:
    name: Infracost
    runs-on: ubuntu-latest
    environment:
      name:  ${{ inputs.environment }}
    env:
      PROJECT_NAME : "${{ inputs.project-directory}}/${{ inputs.environment}}"
    steps:
      - name: Setup Infracost
        uses: infracost/actions/setup@v2
        # See <https://github.com/infracost/actions/tree/master/setup> for other inputs
        # If you can't use this action, see Docker images in <https://infracost.io/cicd>
        with:
          api-key: ${{ secrets.INFRACOST_API_KEY }}

      - name: Checkout base branch
        uses: actions/checkout@v4
        with:
          ref: '${{ github.event.pull_request.base.ref }}'

      # Checkout the base branch of the pull request (e.g. main/master).
      - name: Checkout base branch
        uses: actions/checkout@v3
        with:
          ref: '${{ github.event.pull_request.base.ref }}'

      # Generate Infracost JSON file as the baseline.
      - name: Generate Infracost cost estimate baseline
        run: |
          infracost breakdown --path=${{ inputs.project-directory}} \
                              --project-name="${{ env.PROJECT_NAME }}" \
                              --format=json \
                              --out-file=/tmp/infracost-base.json

      # Checkout the current PR branch so we can create a diff.
      - name: Checkout PR branch
        uses: actions/checkout@v4

      - name: Generate Infracost Diff Report
        run: |
          infracost diff --path=${{ inputs.project-directory}} \
                         --terraform-var-file envs/${{ inputs.environment}}/${{ vars.AWS_REGION }}/terraform.tfvars \
                         --project-name="${{ env.PROJECT_NAME }}" \
                         --format=json \
                         --compare-to=/tmp/infracost-base.json \
                         --out-file=/tmp/infracost-PR-diff.json

      - name: Generate Infracost Breakdown Report
        run: |
          infracost breakdown --path=${{ inputs.project-directory}}  \
                              --terraform-var-file envs/${{ inputs.environment}}/${{ vars.AWS_REGION }}/terraform.tfvars \
                              --project-name="${{ env.PROJECT_NAME }}" \
                              --show-skipped \
                              --format=html \
                              --out-file=/tmp/infracost-breakdown.html

      # 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.
      - name: Post Infracost comment
        run: |
          infracost comment github --path="/tmp/infracost-PR-diff.json" \
                                   --repo=$GITHUB_REPOSITORY \
                                   --github-token=${{github.token}} \
                                   --pull-request=${{github.event.pull_request.number}} \
                                   --behavior=update \
                                   --tag="${{ env.PROJECT_NAME }}" \
                                   --show-all-projects
      
      - name: Upload Infracost Breakdown Report as Artifact
        uses: actions/upload-artifact@v4
        with:
          name: Infracost Breakdown Report (${{ inputs.project-directory}}, ${{ inputs.environment }})
          path: /tmp/infracost-breakdown.html
l
Thanks @billowy-electrician-57321, I’m wondering if it helps if you pass in
--terraform-var-file
to the first breakdown command?
b
for the baseline? I can try that 🙂 This is shared action btw, not sure if that makes any difference
l
Yeah for the baseline.
b
I’ll try and get back to you, thank you very much for quick response 🙂
l
no problem. Let me know and if that doesn’t work in I can dive into it more.
b
no change unfortunately, we also bumped the Infracost action version to 2.1.0
l
Okay, so looking into it looks like somehow the
infracost-PR-diff.json
must be missing a past breakdown for a project. Are you able to DM me that file and the
infracost-baseline.json
file that’s causing this?
b
hey Alistair, sorry for the delay. Could it be it errors out since we had no prior cost breakdown for that particular set of resources? They are actually being created for the first time, and since they didn’t exist infracost breakdown was never created before 🤔
yup, after merging the PR and creating a new one the error went away 🙂 Thank you for your help 🙂
l
Thanks @billowy-electrician-57321, I haven’t been able to reproduce it yet but found a similar bug that might be related, so will get that out in the next release.