Multiple terraform artifact reports generated by a single job

Problem to solve

When generating more than one terraform plan and trying to send them to artifacts:reports:terraform, the CI returns an error:

PATH/TO/*/plan.tfplan.json: found 2 matching artifact files and directories 
ERROR: Uploading artifacts as "terraform" to coordinator... error  correlation_id= error=couldn't execute POST against https://GITLAB_URL/api/v4/jobs/JOB_ID/artifacts?artifact_format=raw&artifact_type=terraform: Post "https://GITLAB_URL/api/v4/jobs/JOB_ID/artifacts?artifact_format=raw&artifact_type=terraform": failed to copy content to form: only one file can be sent as raw id=RAW_ID token=TOKEN

This issue was already reported (but was automatically closed): Multiple terraform artifact reports generated by a single job (#332687) · Issues · GitLab.org / GitLab · GitLab

Should I create a new issue? It would be a nice feature, especially with the addition of environments and the several other new features concerning terraform, artifacts and CI reports in Gitlab since 2021.

Steps to reproduce

Have at least 2 terraform projects with a plan and then set the artifacts as:

  artifacts:
    public: false
    reports:
      terraform: PATH/TO/*/plan.tfplan.json

And the CI script must output the plan as json (the JQ part is to remove creds, it is indicated in the doc in Step 2 here: OpenTofu integration in merge requests | GitLab Docs ):

  script:
    - cd PATH/TO/1
    - tofu plan -out=plan.tfplan
    - tofu show -json plan.tfplan | jq -r "([.resource_changes[]?.change.actions?]|flatten)|{\"create\":(map(select(.==\"create\"))|length),\"update\":(map(select(.==\"update\"))|length),\"delete\":(map(select(.==\"delete\"))|length)}" > plan.tfplan.json
    - cd PATH/TO/2
    - tofu plan -out=plan.tfplan
    - tofu show -json plan.tfplan | jq -r "([.resource_changes[]?.change.actions?]|flatten)|{\"create\":(map(select(.==\"create\"))|length),\"update\":(map(select(.==\"update\"))|length),\"delete\":(map(select(.==\"delete\"))|length)}" > plan.tfplan.json

Configuration

Add the CI/CD configuration in .gitlab-ci.yml or where the terraform plan is done (change tofu with terraform if necessary) and trigger the pipeline.

Versions

  • Self-managed
  • GitLab.com SaaS
  • Dedicated
  • Self-hosted Runners

Versions

  • GitLab (Web: /help or self-managed system information sudo gitlab-rake gitlab:env:info): 18.3
  • GitLab Runner, if self-hosted (Web /admin/runners or CLI gitlab-runner --version): 18.1.1