Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab: extend `!reference` with extra values in `variables`

How can one use !reference in variables block and then extend the list of entries?

For example, I want to be able to do something like

variables:
  !reference [.common_variables, variables]
  FOO: "bar"

At the moment I get an error in the GitLab linter:

This GitLab CI configuration is invalid: (): did not find expected key while parsing a block mapping at line 1 column 1

P.S. Note that this question is specifically about !reference.

  • Yaml anchors are out of scope of this question, as I want to place .common_variables into a separate file that I include in my pipeline.
  • extends: mechanism is out of scope of this question. It does work, but it has a weird behaviour that one also gets an extends variable in the environment, see GitLab: use `extends` in `variables`
like image 701
Mischa Lisovyi Avatar asked Mar 17 '26 15:03

Mischa Lisovyi


1 Answers

variables: !reference [.common_variables, variables]

Is the correct way to reference, however gitlab yml structure will not support additional definitions after the !reference keyword.

My advice would be to have a more specific definition in your variables file that uses anchors to share the common varaibles.

e.g:

in 'variables.gitlab-ci.yml':

.common_variables: &common_variables
     COMMON_VAR: value

.variables:Example-job
    variables:
        <<: *common_variables
        FOO: "bar"

in '.gitlab.ci.yml':

Example-job:
    variables: !reference [".variables:Example-job", "variables"]
    script:
        ...
like image 184
Master Noob Avatar answered Mar 19 '26 12:03

Master Noob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!