-1

I have a huge .gitlab-ci.yml file that I am trying to turn into the "main" ci file by moving some parts to another files which will then be called with the use of the "include" keyword.

.gitlab-ci.yml file:

image: golang:latest

stages:
  - validate

include: /.gitlab/ci/_common.gitlab-ci.yml

validate:
  stage: validate
  extends: .gitlab-tf-backend
  before_script:
    - cd ${TF_ROOT}
    - terraform init -backend=false
    - mv $CI_PROJECT_DIR/deployment.zip .
  script:
    - terraform validate
    - terraform fmt -check=true
  only:
    - branches

.gitlab/ci/_common.gitlab-ci.yml file:

variables:
  GITLAB_API_ENDPOINT: https://gitlab.com/api/v4
  TF_ROOT: ${CI_PROJECT_DIR}/terraform
  PLAN: plan.tfplan
  JSON_PLAN_FILE: tfplan.json
  GIT_DEPTH: 10

.gitlab-tf-backend: &gitlab-tf-backend
   - export TF_ADDRESS=${GITLAB_API_ENDPOINT}/projects/${CI_PROJECT_ID}/terraform/state/${STATE_NAME}
   - export TF_HTTP_ADDRESS=${TF_ADDRESS}
   - export TF_HTTP_LOCK_ADDRESS=${TF_ADDRESS}/lock
   - export TF_HTTP_LOCK_METHOD=POST
   - export TF_HTTP_UNLOCK_ADDRESS=${TF_ADDRESS}/lock
   - export TF_HTTP_UNLOCK_METHOD=DELETE
   - export TF_HTTP_USERNAME=${USERNAME}
   - export TF_HTTP_PASSWORD=${GITLAB_TOKEN}
   - export TF_HTTP_RETRY_WAIT_MIN=5
   - echo "Using HTTP Backend at $TF_HTTP_ADDRESS"
   - terraform --version
   - terraform init

My file is correctly found by gitlab. It seems that my hidden job ".gitlab-tf-backend" is not recognized and cannot be called.

Full error is:

This GitLab CI configuration is invalid: validate: invalid base hashes in `extends` (.gitlab-tf-backend)

How could I properly call my '.gitlab-tf-backend' hidden job from my main .gitlab-ci.yml file ?

1
  • 1
    You're mixing YAML anchors and extend keyword, what you do would be of if your main script was including the hidden job with `script:\n<<: &gitlab-tf-backend. Now, make yourself a favor and use the gitlab-terraform image which takes care of all that for you. See gitlab documentation for more details – Tensibai Dec 2 '20 at 15:40
1

Okay, found it.

It seems the .gitlab-ci.yml hidden job needs to have a "script:" to solve the problem. See below:

.gitlab-tf-backend: &gitlab-tf-backend
      script:
       - export TF_ADDRESS=${GITLAB_API_ENDPOINT}/projects/${CI_PROJECT_ID}/terraform/state/${STATE_NAME}
       - export TF_HTTP_ADDRESS=${TF_ADDRESS}
       - export TF_HTTP_LOCK_ADDRESS=${TF_ADDRESS}/lock
       - export TF_HTTP_LOCK_METHOD=POST
       - export TF_HTTP_UNLOCK_ADDRESS=${TF_ADDRESS}/lock
       - export TF_HTTP_UNLOCK_METHOD=DELETE
       - export TF_HTTP_USERNAME=${USERNAME}
       - export TF_HTTP_PASSWORD=${GITLAB_TOKEN}
       - export TF_HTTP_RETRY_WAIT_MIN=5
       - echo "Using HTTP Backend at $TF_HTTP_ADDRESS"
       - terraform --version
       - terraform init

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.