Terraform template included in triggered job missing plan.cache and plan.json

When a parent project kicks off pipeline in job:

testapss-use1 cluster:
  trigger:
    project: gitops/infra/eks-clusters/testapss-use1
    strategy: depend
testqa-use1 cluster:
  trigger:
    project: gitops/infra/eks-clusters/testqa-use1
    strategy: depend

Which includes a project:

include:
  project: gitops/infra/eks
  file: pipeline.yml

that pipeline.yml contains:

include:
  - template: Terraform.latest.gitlab-ci.yml

stages:
  - test
  - validate
  - build
  - deploy
  - cleanup

variables:
  TF_IN_AUTOMATION: "true"

destroy:
  extends: [.terraform:destroy]
  needs: []
  rules:
    - when: manual

The build job doesn’t create a plan.cache or plan.json:

$ gitlab-terraform plan-json
Saving cache for successful job
00:00
Creating cache -protected...
WARNING: processPath: artifact path is not a subpath of project directory: /.terraform/ 
Archive is up to date!                             
Created cache
Uploading artifacts for successful job
00:01
Uploading artifacts...
WARNING: processPath: artifact path is not a subpath of project directory: /plan.cache 
ERROR: No files to upload                          
Uploading artifacts...
WARNING: processPath: artifact path is not a subpath of project directory: /plan.json 
ERROR: No files to upload                          
Cleaning up project directory and file based variables
00:00
Job succeeded

So the deploy job on that triggered pipeline fails:

Terraform has been successfully initialized!
╷
│ Error: Failed to load "plan.cache" as a plan file
│ 
│ Error: stat plan.cache: no such file or directory
╵
Cleaning up project directory and file based variables

Do you by any chance define a TF_ROOT variable in the parent pipeline? It might be caused by how variables are inherited by child pipelines. But then the in job output it says /.terraform/ which would indicate the TF_ROOT variable is empty. Might be some problem with predefined variables in multi-project pipelines and includes as the variable is defined here if you are using Gitlab.com.

I suppose overriding TF_ROOT variable in the pipeline.yaml should do the trick.

variables:
  TF_IN_AUTOMATION: "true"
  TF_ROOT: "."
``

@balonik does not work. thanks (It’s also messing up where it’s uploading the artifacts to in object storage, so it’s larger than just TF template).

1 Like