Borsi Csokas

09/21/2022, 9:22 AM
Hi everyone, new to the community, and would like to ask if anyone has been able to implement both the multi-project and the cache actions, as I'm currently trying to do that, but running into the following problem:
Error: config file does not exist at terraform/infracost_config.yml
this error does not appear with only the multi-project config file configured, but does once i try to add the caching steps to it. any clue what i'm doing wrong?
Vadim (Infracost)

Vadim (Infracost)

09/21/2022, 9:41 AM
Hello! Can you share your CI workflow (without sensitive info of course). I've never used the cache action myself, so more info would be helpful 🙂

Borsi Csokas

09/21/2022, 10:27 AM
Hi Vadim, This is the file
The purpose of this workflow is to run Terragrunt Plan for all affected components in a PR
name: Terragrunt-price

      - main

  TF_PLUGIN_CACHE_DIR: "/home/runner/cache/terragrunt"

    name: Prepare terragrunt-plans
    runs-on: [ self-hosted, stable ]

      - name: Plan - Add scripts to PATH
        run: echo "${GITHUB_WORKSPACE}/.github/workflows/scripts/" >> $GITHUB_PATH

      - name: Plan - Checkout code
        uses: actions/checkout@v2.0.0
          clean: true
          fetch-depth: 0
          ref: ${{ github.head_ref }}

      - name: Plan - Setup terra runner
        run: setup-terra-runner.sh

      - name: Setup Infracost
        uses: infracost/actions/setup@v2.0.0
          api-key: ${{ secrets.INFRACOST_API_KEY }}
          currency: EUR

      - name: Cache the Infracost baseline JSON result
        id: cache-infracost-base-json
        uses: actions/cache@v3
          path: '/tmp/infracost-base.json'
          key: infracost-base-json-${{ runner.os }}-${{ github.event.pull_request.base.sha || github.sha }}

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

      # Downloading remote Terraform modules can be slow, so we add them to the GitHub cache.
      # We skip this for pushes to the main/master branch that already have the baseline generated.
      - name: Cache .infracost/terraform_modules for target branch
        uses: actions/cache@v3
          path: terraform/**/.infracost/terraform_modules/**
          key: infracost-terraform-modules-${{ runner.os }}-${{ github.event.pull_request.base.sha || github.sha }}
          # If there's no cached record for this commit, pull in the latest cached record anyway
          # Internally infracost will downloaded any additional required modules if required
          restore-keys: infracost-terraform-modules-${{ runner.os }}-
        if: github.event_name == 'pull_request' || steps.cache-infracost-base-json.outputs.cache-hit != 'true'

      - name: Run Infracost
        run: infracost breakdown --config-file=terraform/infracost_config.yml --format=json --out-file=/tmp/infracost-base.json
        if: steps.cache-infracost-base-json.outputs.cache-hit != 'true'

          # Checkout the current PR branch so we can create a diff.
      - name: Checkout PR branch
        uses: actions/checkout@v2
          # Make sure the .infracost dir stays between runs so that downloaded modules persist
          clean: false
          if: github.event_name == 'pull_request'

          # Generate an Infracost diff and save it to a JSON file.
      - name: Generate Infracost diff
        run: infracost diff --config-file=terraform/infracost_config.yml --format=json --compare-to=/tmp/infracost-base.json --out-file=/tmp/infracost.json
        if: github.event_name == 'pull_request'

          # 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.json --repo=$GITHUB_REPOSITORY --github-token=${{github.token}} --pull-request=${{github.event.pull_request.number}} --behavior=update
        if: github.event_name == 'pull_request'
10:27 AM
Hope it makes sense 😅
Vadim (Infracost)

Vadim (Infracost)

09/21/2022, 11:22 AM
Thank you! I can't detect anything obvious from the code. My guess would be that the cache action changed the working directory and Infracost CLI is running elsewhere, OR something happened with the source code. You can try adding additional steps with
echo $(pwd)
echo $(ls)
echo $(ls terraform/)
to see what's going on. Maybe it can give a clue
11:26 AM
It's also not clear from the code which step failed: on
infracost breakdown
infracost diff

Borsi Csokas

09/21/2022, 12:31 PM
ok, think i found the problem: caching checks out the head branch, it apparently also removes the config file from the main directory 🙈 so your feedback helped a ton 😄 thank you so much, hopefully it's going to be a cakewalk from now 🙂
Vadim (Infracost)

Vadim (Infracost)

09/21/2022, 12:36 PM
Oh, so the config is in feature branch only. Glad you figured it out! Hope everything works out. It would be interesting to see how big is the speed bump for you. If you have some time, maybe you'll be able provide some feedback to Alistair? https://github.com/infracost/infracost/issues/1841#issuecomment-1198001313

Borsi Csokas

09/21/2022, 12:39 PM
yes, i should be, as soon as everything is in fact running as expected 🙂