Reputation: 73
I am using Gitlab terraform & the yaml file is as below for deploy stage,
build:
extends: .terraform:build
script:
- cd "${TF_ROOT}"
- gitlab-terraform plan --var-file=local.tfvars --var-file=common.tfvars
- gitlab-terraform plan-json --var-file=local.tfvars --var-file=common.tfvars
deploy:
extends: .terraform:deploy
script:
- cd "${TF_ROOT}"
- gitlab-terraform apply --var-file=local.tfvars --var-file=common.tfvars
environment:
name: $TF_STATE_NAME
First time it deployed perfectly, However my Gitlab log shows the following error now for the second time,
│ Error: Can't set variables when applying a saved plan
The -var and -var-file options cannot be used when applying a saved plan
file, because a saved plan includes the variable values that were set when
it was created.
ERROR: Job failed: exit code 1
I am looking through the internet but no help till now.
Can someone please suggest me the terraform command I should use to avoid this error ?
Upvotes: 0
Views: 2704
Reputation: 457
Setting the TF_CLI_ARGS
variable in your CI environment (i.e. your .gitlab-ci.yml
file), will make terraform commands run in that environment with those args included.
So for example, setting the below variables in your pipeline file..
variables:
TF_CLI_ARGS: "-var-file=myvars.tfvars"
TF_CLI_ARGS_plan: "-var-file=plan.tfvars"
..will make sure that all terraform commands are executed with -var-file=myvars.tfvars
appended, and the terraform plan
command will run with -var-file=myvars.tfvars -var-file=plan.tfvars
appended.
As the gitlab-terraform
tool is just a wrapper around existing terraform commands, setting this variable should work with that lib.
Upvotes: 0
Reputation: 1879
I had the same issue as OP and I'm just posting their solution from a comment they made.
Remove any -var-file
arguments after gitlab-terraform apply
to fix the error. Here's an example of a working job:
deploy:
extends: .terraform:deploy
dependencies:
- build
script:
- gitlab-terraform apply
environment:
name: $TF_STATE_NAME
action: start
PS The whole script
section can be left out in the example above since it the same as in the base job.
Upvotes: 0