as far as I know, you can use the !reference
tag for relating to some construct used in a parent template.
The Gitlab
documentation gives following example:
file “config.yml”
.setup:
script:
- echo creating environment
file “.gitlab-ci.yml”
include:
- local: configs.yml
.teardown:
after_script:
- echo deleting environment
test:
script:
- !reference [.setup, script]
- echo running my own command
after_script:
- !reference [.teardown, after_script]
My question is: How can I use variables within the !reference
tag when the template name should be dynamically assembled? For instance something like that:
include:
- local: configs.yml
test:
variables:
USED_TEMPLATE_NAME: ".setup"
script:
- !reference [$USED_TEMPLATE_NAME, script]
- echo running my own command
Currently I got following error when trying to archieve this:
!reference ["$USED_TEMPLATE_NAME", "script"] could not be found
What am I doing wrong? Thanks for your help!
1
You can’t use variables in the !reference
tag. However, you can achieve a similar effect another way.
For example, you can conditionally include a different YAML template based on a variable. So, your !reference
tag will be static as, say, !reference [.setup, script]
but the actual contents of the .setup
key will be conditional based on a conditional include. In other words, your includes would be dynamic, not the reference, but the effect is the same: the reference refers to something different depending on the value of the variable.
include:
# all these templates will include a `.setup` key
- local: setup-blue.yml
rules:
- if: $USED_TEMPLATE_NAME == "blue"
- local: setup-green.yml
rules:
- if: $USED_TEMPLATE_NAME == "green"
- local: setup-default.yml
rules:
- if: $USED_TEMPLATE_NAME != "green" && $USED_TEMPLATE_NAME != "blue"
variables:
# you could set this here...
# or perhaps it could be set by a UI trigger,
# a CICD setting, an upstream pipeline trigger, or whatever
USED_TEMPLATE_NAME: "..."
# ...
some_job:
script:
- !reference [.setup, script]