Variables in Gitlab can be set in several places:
- In group settings
- In the project settings
- Inside .gitlab-ci.yml
At the same time, variables in the group and project settings can be set as a βfileβ or βregular variableβ and check the βprotectedβ and βmaskβ checkboxes.
Let's start with simple inheritance and get progressively more complex.
A final list of priority levels can be found at the end of the document.
Inheritance with groups [source]
Variables from groups are inherited, with the rule that the closer the group is located to the project, the more important its value is.
Groups with variables
.gitlab-ci.yml
image: busybox:latest
variables:
GIT_STRATEGY: none
echo:
stage: test
script:
- echo $MSG
Pyline result
$ echo $MSG
B
If the variable had not been specified in group B, then we would have seen the value of A.
Variable inheritance inside .gitlab-ci.yml [source]
Everything is quite simple here: you can set a global variable, or you can overwrite it inside a job.
Groups with variables
.gitlab-ci.yml
Now let's create 2 jobs, in one of them explicitly specify $MSG.
image: busybox:latest
variables:
GIT_STRATEGY: none
MSG: "Custom in global .gitlab-ci.yml"
echo:
stage: test
script:
- echo $MSG
echo with var:
stage: test
variables:
MSG: "Custom in job .gitlab-ci.yml"
script:
- echo $MSG
Pyline result
- threw out:
$ echo $MSG Custom in global .gitlab-ci.yml Job succeeded
- echo with vars:
$ echo $MSG Custom in job .gitlab-ci.yml Job succeeded
Inheritance with groups and inside .gitlab-ci.yml [source]
Let's try to combine the previous 2 examples. Group variables take precedence over variables inside .gitlab-ci.yml.
Groups with variables
.gitlab-ci.yml
image: busybox:latest
variables:
GIT_STRATEGY: none
MSG: "Custom in global .gitlab-ci.yml"
echo:
stage: test
script:
- echo $MSG
echo with var:
stage: test
variables:
MSG: "Custom in job .gitlab-ci.yml"
script:
- echo $MSG
Pyline result
- threw out:
$ echo $MSG Y Job succeeded
- echo with vars:
$ echo $MSG Y Job succeeded
Inheritance with specifying variables in the project settings [source]
Variables in project settings ALWAYS have the highest priority! And the variables specified inside .gitlab-ci.yml do not play any role.
Groups with variables
Group variables have lower priority.
.gitlab-ci.yml
Let's use the file from the previous example. Here again, there are variables specified inside .gitlab-ci.yml, but variables inside groups still take precedence over them.
image: busybox:latest
variables:
GIT_STRATEGY: none
MSG: "Custom in global .gitlab-ci.yml"
echo:
stage: test
script:
- echo $MSG
echo with var:
stage: test
variables:
MSG: "Custom in job .gitlab-ci.yml"
script:
- echo $MSG
Pyline result
- threw out:
$ echo $MSG project-3 Job succeeded
- echo with vars:
$ echo $MSG project-3 Job succeeded
Inheritance with an empty value [source]
An empty value is also a value
An empty value is not Null
Groups with variables
.gitlab-ci.yml
image: busybox:latest
variables:
GIT_STRATEGY: none
MSG: "Custom in global .gitlab-ci.yml"
echo:
stage: test
script:
- echo $MSG
echo with var:
stage: test
variables:
MSG: "Custom in job .gitlab-ci.yml"
script:
- echo $MSG
Pyline result
- threw out:
$ echo $MSG Job succeeded
- echo with vars:
$ echo $MSG Job succeeded
Inheritance with Inclusion and Groups [source]
Here we will try to include project-2 in project-3
Groups in this case have priority.
Groups with variables
.gitlab-ci.yml
And set the variable globally in .gitlab-ci.yml
variables:
MSG: "With include .gitlab-ci.yml"
include:
- project: how-is-gitlab-ci-inherit-environment-variables/z/y/project-3
file: '.gitlab-ci.yml'
Pyline result
- threw out:
$ echo $MSG B Job succeeded
- echo with vars:
$ echo $MSG B Job succeeded
Inheritance with inclusion [source]
Here we will try to include project-2 in project-3.
With the condition that: neither the groups nor the project itself have any variables.
Groups with variables
.gitlab-ci.yml
Same as previous example
variables:
MSG: "With include .gitlab-ci.yml"
include:
- project: how-is-gitlab-ci-inherit-environment-variables/z/y/project-3
file: '.gitlab-ci.yml'
Pyline result
- threw out:
$ echo $MSG With include .gitlab-ci.yml Job succeeded
- echo with vars:
$ echo $MSG Custom in job .gitlab-ci.yml Job succeeded
We get the following Priorities:
- Variables in project settings
- Variables in groups
- Variables strictly specified inside the job (including included files)
- Global variables inside .gitlab-ci.yml
- Global variables inside included files
Conclusion
The most not obvious point is that the rule βthe closer the variable is to the code, the more important it isβ works first for groups, and then the same rule for variables inside .gitlab-ci.yml, but only with the condition that the variables in the groups are not set .
Next, an important point is to understand that the global space for the main and included .gitlab-ci.yml is the same. And that file in which there is an include has a priority.
Source: habr.com