mr.szop
mr.szop

Reputation: 15

Gitlab CI variables in jobs

I've got some trouble defining a job. I'm in an early stage of defining my pipeline and I'm trying to solve my problems with variables :). I've got a few subfolder under in gcp/live/development and I'm trying to cd them and then run terraform stuff:

.plan-development:
  variables:
    ENVIRONMENT: "development"
    TERRAFORM_DEVELOPMENT_PATH: "gcp/live/development"
    TF_ROOT: ${TERRAFORM_DEVELOPMENT_PATH}/${CI_JOB_NAME%-plan-development}
  stage: plan-development
  script:
    - cd ${TF_ROOT}
    - gitlab-terraform plan
    - gitlab-terraform plan-json
  artifacts:
    name: plan-${ENVIRONMENT}-${CI_JOB_NAME%-plan-development}
    paths:
      - ${TF_ROOT}/plan.cache
    reports:
      terraform: ${TF_ROOT}/plan.json

But according to my failing pipeline, the first script part cd ${TF_ROOT} doesn't enter the expected folder at gcp/live/development/cloud-nat.

I've got some debug outout, which tells me following:

$ cd ${TF_ROOT}
$ pwd && ls
/builds/b79ya_1j/0/devops/terraform/gcp/live/development
cloud-nat
firewall
gke
vpc
$ echo "${CI_JOB_NAME%-plan-development}"
cloud-nat

Am I missing something combing two variables (TF_ROOT: ${TERRAFORM_DEVELOPMENT_PATH}/${CI_JOB_NAME%-plan-development})?

Hope you can help me out.

Upvotes: 1

Views: 4740

Answers (1)

Tolis Gerodimos
Tolis Gerodimos

Reputation: 4400

Am I missing something combing two variables (TF_ROOT: ${TERRAFORM_DEVELOPMENT_PATH}/${CI_JOB_NAME%-plan-development})?

The issue is that Gitlab in variables section, doesn't perform bash operations, like you attempt here

${CI_JOB_NAME%-plan-development}

Gitlab literally searches for the variable named CI_JOB_NAME%-plan-development

That's why when it creates the TF_ROOT variable its value is gcp/live/development/ since it could not find the variable named CI_JOB_NAME%-plan-development

Upvotes: 2

Related Questions