Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using config file produces misleading comment with module paths from clone of main branch and the real module paths #2740

Open
Tomasz-Kluczkowski opened this issue Nov 9, 2023 · 6 comments

Comments

@Tomasz-Kluczkowski
Copy link

Tomasz-Kluczkowski commented Nov 9, 2023

Since config file can accept anything between 1 and infinite amount of projects it should work fine for just 1 project too.
This is not the case.

I am testing infracost for a variety of projects (terraform and terragrunt) and building a common ci-job template in gitlab.

For this issue we are considering a simple terraform project, with 1 folder terraform where the code is , with no multiple environments.

Consider this config file:

version: 0.1

projects:
  - path: ./terraform
    name: azure-infrastructure-costs

and running using this code (as per instructions here: https://gitlab.com/infracost/infracost-gitlab-ci/-/tree/master/examples/multi-project-config-file):

  script:
    - git clone $CI_REPOSITORY_URL --branch=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME --single-branch /tmp/base

    - |
      cd /tmp/base
      infracost breakdown --config-file=${CONFIG_FILE} \
                          --format=json \
                          --out-file=infracost-base.json

    - |
      cd -
      infracost diff --config-file=${CONFIG_FILE} \
                     --compare-to=/tmp/base/infracost-base.json \
                     --format=json \
                     --out-file=infracost.json

    - |
      infracost comment gitlab --path=infracost.json \
                               --repo=$CI_PROJECT_PATH \
                               --merge-request=$CI_MERGE_REQUEST_IID \
                               --gitlab-server-url=$CI_SERVER_URL \
                               --gitlab-token=$GITLAB_API_TOKEN \
                               --behavior=update

Produces the following comment:

Infracost report

💰 Monthly cost will not change

Project Module path Cost change New monthly cost
azure-infrastructure-costs ../../../../tmp/base/terraform -£11 £0
azure-infrastructure-costs terraform +£11 £11

Where I would expect, that similarly to if I use --path=./terraform without config file, it will figure out there is only 1 project and provide result just for it.

We can see that it correctly states: Monthly cost will not change, but it does it for 2 projects:

  • ../../../../tmp/base/terraform which is the path for the cloned code from the main branch
  • terraform which is the path of the current branch.

Am I doing something wrong or using config file for 1 project was never considered but should?

@Tomasz-Kluczkowski
Copy link
Author

Additionally I just tested this setup for a multi terraform project repo, and the result is similar to when using only one project - the reports are shown for base modules for main branch and for real modules.

💰 Monthly cost will not change

Project Module path Cost change New monthly cost
management ../../../tmp/base/management/terraform -£224 £0
management management/terraform +£224 £224
playground ../../../tmp/base/playground/terraform -£30 £0
playground playground/terraform +£30 £30

@Tomasz-Kluczkowski
Copy link
Author

So in conclusion, am I doing something wrong?

@Tomasz-Kluczkowski Tomasz-Kluczkowski changed the title Using config file with singular project produces misleading comment Using config file produces misleading comment with module paths from clone of main branch and the real module paths Nov 9, 2023
@aliscott
Copy link
Member

@Tomasz-Kluczkowski what is your CONFIG_FILE variable set to? Also does it work if you copy the config file into /tmp/base before running infracost breakdown, e.g.:

    - |
      cp infracost.yml /tmp/base
      cd /tmp/base
      infracost breakdown --config-file=${CONFIG_FILE} \
                          --format=json \
                          --out-file=infracost-base.json

@Tomasz-Kluczkowski
Copy link
Author

Tomasz-Kluczkowski commented Nov 10, 2023

Hi,
I am running in gitlab. In my repo the .infracost.yml is in the root of the project.

CONFIG_FILE: ${CI_PROJECT_DIR}/.infracost.yml

@Tomasz-Kluczkowski
Copy link
Author

I will try running with your instructions and report back.

@Tomasz-Kluczkowski
Copy link
Author

ok I changed the code to be:

      cp .infracost.yml /tmp/base
      cd /tmp/base
      infracost breakdown --config-file=.infracost.yml \
                          --format=json \
                          --out-file=infracost-base.json

    - |
      cd -
      infracost diff --config-file=.infracost.yml \
                     --compare-to=/tmp/base/infracost-base.json \
                     --format=json \
                     --out-file=infracost.json

Which produced expected results for no changes to resources:

Infracost report

💰 Monthly cost will not change

This comment will be updated when code changes.

And also when resources were changed:

💰 Monthly cost will increase by £9 📈

Project Cost change New monthly cost
playground +£9 (+31%) £40

So I think your solution is correct @aliscott . Should then infracost's documentation here: https://gitlab.com/infracost/infracost-gitlab-ci/-/tree/master/examples/multi-project-config-file be updated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants