Gitlab变量可以在几个地方设置:
- 在群组设定中
- 在项目设置中
- 内.gitlab-ci.yml
在这种情况下,可以将组和项目的设置中的变量设置为“文件”或“普通变量”,然后选中“保护”和“掩码”框。
![](https://habrastorage.org/webt/iu/2f/d2/iu2fd2ums7tmrkyqb4hrbhuenzg.png)
让我们从简单的继承开始,并将逐渐变得更复杂。
优先级的最终列表可在文档末尾找到。
, , .
![变量组](https://habrastorage.org/webt/4b/wl/qj/4bwlqj-g_cztown5ks5znmiqgl8.png)
image: busybox:latest
variables:
GIT_STRATEGY: none
echo:
stage: test
script:
- echo $MSG
$ echo $MSG
B
B, .
.gitlab-ci.yml []
: , .
c
![](https://habrastorage.org/webt/ze/em/ih/zeemihn1rerz65eb_hgmejkx0iw.png)
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
![](https://habrastorage.org/webt/wv/ke/3o/wvke3o9zni565rug1kegmkorxha.png)
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
.
![](https://habrastorage.org/webt/by/ni/zb/bynizbno7obalix_sxmgiz0dggm.png)
. , , .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
![](https://habrastorage.org/webt/vs/mv/gq/vsmvgqrc4q9eh_zpevibj3tqa8u.png)
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
![](https://habrastorage.org/webt/lc/vg/19/lcvg19w1dkgdqlkennmeta1awcu.png)
.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
![](https://habrastorage.org/webt/mp/bv/e1/mpbve1krliedbl7j3wb3md2ecjk.png)
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的全局空间是通用的。并且其中包含发生的文件优先。