Gitlab and Terraform state file: why do I have two state file and why does it not modify the default one?

51 views Asked by At

Here is my simplified .gitlab-ci.yml file:

include:
 - template: Terraform/Base.gitlab-ci.yml  

variables:
 GITLAB_ACCESS_TOKEN: "$GITLAB_ACCESS_TOKEN"
 TF_STATE_NAME: "default"

stages:
 - validate
 - build
 - deploy
 - cleanup

before_script:
 - echo "${TF_STATE_NAME}"
 - echo "${GITLAB_ACCESS_TOKEN}"
 - terraform init -reconfigure -backend-config="address=https://gitlab.com/api/v4/projects/XXXXX/terraform/state/${TF_STATE_NAME}" -backend-config="lock_address=https://gitlab.com/api/v4/projects/XXXXX/terraform/state/${TF_STATE_NAME}/lock" -backend-config="unlock_address=https://gitlab.com/api/v4/projects/XXXXX/terraform/state/${TF_STATE_NAME}/lock" -backend-config="username=louisvgn" -backend-config="password=${GITLAB_ACCESS_TOKEN}" -backend-config="lock_method=POST" -backend-config="unlock_method=DELETE" -backend-config="retry_wait_min=5"

fmt:
 extends: .terraform:fmt
 needs: []

validate:
 extends: .terraform:validate
 needs: []

build:
 extends: .terraform:build

deploy:
 extends: .terraform:deploy
 script:
  - echo "${TF_STATE_NAME}"
  - echo "${GITLAB_ACCESS_TOKEN}"
 rules:
   - if: $CI_COMMIT_TITLE != "destroy"
     when: on_success
 dependencies:
   - build
 environment:
   name: $TF_STATE_NAME

cleanup:
 extends: .terraform:destroy
 environment:
   name: $TF_STATE_NAME
 rules:
   - if: $CI_COMMIT_TITLE == "destroy"
     when: on_success

In the gitlab runners’ logs, the echo "${TF_STATE_NAME}" gives me default which is expected. However, when I go to OperateTerraform states I have the following: 

enter image description here

And I can see that it is the one with the weird $TF_STATE_NAME name getting updated and not the default one. Why is that? I do not know how the former $TF_STATE_NAME appeared… I miss input, probably.

Also, where can we see the versions of a state file in gitlab? I don’t see many options here.

0

There are 0 answers