gitlabgitlab-cicicd

Extend from Variables seems to have no effect


An overview of my Gitlab CICD setup:

.gitlab-ci.yml:

include:
  - foo/.basic-gitlab.yml
  - foo/.gitlab-ci-core.yml

.basic-gitlab.yml

.base_variables: &base_variables
  INSTANCE_TYPE: "g5.2xlarge"

.even_request: &even_request
  variables:
    <<: *base_variables
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
      when: always
  tags:
    - FooCiCd

.gitlab-ci-core.yml .variables_more: &variables_more REPO: BLA

.core: &core
  variables:
    <<: *variables_more
    extends: .base_variables
  rules: !reference [.even_request, rules]
  tags: !reference [.even_request, tags]

job_stage_1:
  <<: *core
  stage: .pre
  script:
    - echo ${REPO}
    - echo ${INSTANCE_TYPE}

The above script sort of explains what we got. The problem here is that job_stage_1 seems to print an empty string for echo ${INSTANCE_TYPE} which looks like that the extend functionality has not worked as I thought it should have. Does anybody see where the problem is?


Solution

  • you should extend .even_request to inherit its variables, rules, and tags to .core to ensures that the INSTANCE_TYPE variable is available in .core and subsequently in job_stage_1

    .variables_more: &variables_more
      REPO: BLA
    
    .core: &core
      extends: .even_request
      variables:
        <<: *variables_more
      rules: !reference [.even_request, rules]
      tags: !reference [.even_request, tags]
    
    job_stage_1:
      extends: .core
      stage: .pre
      script:
        - echo ${REPO}
        - echo ${INSTANCE_TYPE}