Check out GitLab 14.3 (September 2021)
Use variables in other variables
CI/CD pipeline execution scenarios can depend on expanding variables declared in a pipeline or using GitLab predefined variables within another variable declaration.
In 14.3, we are enabling the “variables inside other variables” feature on GitLab SaaS.
Now you can define a variable and use it in another variable definition within the same pipeline.
You can also use GitLab predefined variables inside of another variable declaration.
This feature simplifies your pipeline definition and eliminates pipeline management issues caused by the duplicating of variable data.
Note - for GitLab self-managed users the feature is disabled by default.
To use this feature, your GitLab administrator will need to enable the feature flag.
(demo -- video)
![demo](https://cdn.statically.io/img/i.sstatic.net/AZLU4.png)
See [Documentation]https://gitlab.com/gitlab-org/gitlab/-/blob/ee8cb300efb6f917eab6d865c9d430eb62077b44/doc/ci/variables/index.md#use-variables-in-other-variables) and Issue.
Q4 2023: the documentation now refers to Use CI/CD variables in other variables
Use CI/CD variables in other variables
You can use variables inside other variables:
job:
variables:
FLAGS: '-al'
LS_CMD: 'ls "$FLAGS"'
script:
- 'eval "$LS_CMD"' # Executes 'ls -al'
Use the $
character in CI/CD variables.
If you do not want the $
character interpreted as the start of another variable, use $$
instead:
job:
variables:
FLAGS: '-al'
LS_CMD: 'ls "$FLAGS" $$TMP_DIR'
script:
- 'eval "$LS_CMD"' # Executes 'ls -al $TMP_DIR'
Prevent CI/CD variable expansion
Introduced in GitLab 15.7.
Expanded variables treat values with the $
character as a reference to another variable.
CI/CD variables are expanded by default. To treat variables with a $ character as raw strings, disable variable expansion for the variable
dba
asks in the comments:
Does this include or exclude using globally defined variables?
dba's own answer:
Global variables can be reused, but they need the local_var: ${global_var}
syntax with recursive expansion (independent of the shell).
CI_COMMIT_REF_NAME=production
, would you like to have${production}
in.env
?