[CI] Use YML variables outside script environment

Description

Currently variables can only be used in the script environment. It would simplify my setup quite a bit if it would be possible to use variables in other parts of the job description. For example in dependencies.

A valid .gitlab-ci.yml file (shortened) now mak look like this:

stages:
    - build
    - test

.build:linux_job: &linux_build
    script:
        - mkdir build_${release}_${arch}
        - cd build_${release}_${arch}
        - cmake .. -DCMAKE_BUILD_TYPE=Release
        - make -j
    artifacts:
        paths:
            - build_${release}_${arch}/*
        expire_in: 1 days

.test:linux_job: &linux_test
    script:
        - cd build_${release}_${arch}
        - make test

build:linux_trusty_amd64:
    stage: build
    tags:
        - ubuntu_amd64
    variables:
        release: trusty
        arch: amd64
    <<: *linux_build

test:linux_trusty_amd64:
    stage: test
    tags:
        - ubuntu_amd64
    variables:
        release: trusty
        arch: amd64
    dependencies:
        - build:linux_trusty_amd64
    <<: *linux_test

To minimize code duplication I'd like to use the variables release and arch not just as environment variables for the scripts but also for example in the dependencies section. This would allow to move the dependencies section into the template:

stages:
    - build
    - test

.build:linux_job: &linux_build
    script:
        - mkdir build_${release}_${arch}
        - cd build_${release}_${arch}
        - schroot -c Ubuntu_${release}_${arch}_ribuilder -- cmake .. -DCMAKE_BUILD_TYPE=Release
        - schroot -c Ubuntu_${release}_${arch}_ribuilder -- make -j
    artifacts:
        paths:
            - build_${release}_${arch}/*
        expire_in: 1 days

.test:linux_job: &linux_test
    script:
        - cd build_${release}_${arch}
        - schroot -c Ubuntu_${release}_${arch}_ribuilder -- make test
    dependencies:
        - build:linux_${release}_${arch}

build:linux_trusty_amd64:
    stage: build
    tags:
        - ubuntu_amd64
    variables:
        release: trusty
        arch: amd64
    <<: *linux_build

test:linux_trusty_amd64:
    stage: test
    tags:
        - ubuntu_amd64
    variables:
        release: trusty
        arch: amd64
    <<: *linux_test

Proposal

Make variables available outside the script environment

Links / references

issue gitlab-ce#24152 would probably also benefit from this change

Edited Aug 20, 2020 by Jackie Porter
Assignee Loading
Time tracking Loading