Gitlab变量可以在几个地方设置:
- 在群组设定中
- 在项目设置中
- 内.gitlab-ci.yml
在这种情况下,可以将组和项目的设置中的变量设置为“文件”或“普通变量”,然后选中“保护”和“掩码”框。

让我们从简单的继承开始,并将逐渐变得更复杂。
优先级的最终列表可在文档末尾找到。
, , .

image: busybox:latest
variables:
GIT_STRATEGY: none
echo:
stage: test
script:
- echo $MSG
$ echo $MSG
B
B, .
.gitlab-ci.yml []
: , .
c

2 , $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
.gitlab-ci.yml []
2 . .gitlab-ci.yml.
c

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
! , .gitlab-ci.yml .
c
.

. , , .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
–
– Null
c

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
project-2 project-3
.
c

.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'
project-2 project-3.
C : , .
c

variables:
MSG: "With include .gitlab-ci.yml"
include:
- project: how-is-gitlab-ci-inherit-environment-variables/z/y/project-3
file: '.gitlab-ci.yml'
:
- ( )
- .gitlab-ci.yml
最明显的一点是,规则“对变量越接近代码,它越重要”首先适用于组,然后对.gitlab-ci.yml中的变量适用相同的规则,但前提是未定义组中的变量。
此外,重要的一点是要了解核心和排除的.gitlab-ci.yml的全局空间是通用的。并且其中包含发生的文件优先。