Is it possible to use predefined variables in user-defined variables:

Viewed 2427

I wanted to define variable for docker image name using branch name as tag, as defined in documentation https://docs.gitlab.com/ce/ci/docker/using_docker_build.html :

variables:
 CONTAINER_NAME: myimage:$CI_COMMIT_REF_NAME
 CONTAINER_PUSH_NAME: myrepo:5000/$CONTAINER_NAME

however, when I output CONTAINER_PUSH_NAME, I get:

myrepo:5000/myimage:$CI_COMMIT_REF_NAME

It means, that my own variables are expanded, but the predefined ones not, although they are available in script execution time, because when I echo $CI_COMMIT_REF_NAME, I get branch name, as expected.

Is it possible to use predefined variables as a part of my defined variables, or the documentation is misleading?

2 Answers

Check if GitLab 13.10 (March 2021, 4 years later) can help in your case:

Nested variable expansion

Sometimes you need to define a variable in the .gitlab-ci.yml file and use it in another variable definition.

We are introducing the new nested variables expansion feature.

When enabled, GitLab sorts and expands job variables recursively before sending the final output to the runner for job execution.
This specific sorting allows variables to be sent from GitLab CI/CD to the runner in a sequence that resolves correctly.

Now, you can easily use variables inside other variables in your .gitlab-ci.yml file.

https://about.gitlab.com/images/13_10/nested_variables.png -- Nested variable expansion

See Documentation and Issue.

Related