Variabel Gitlab dapat diatur di beberapa tempat:
- Dalam pengaturan grup
- Dalam pengaturan proyek
- Di dalam .gitlab-ci.yml
Dalam hal ini, variabel dalam pengaturan grup dan proyek dapat ditetapkan sebagai "file" atau "variabel biasa" dan centang kotak "protected" dan "mask".
![](https://habrastorage.org/webt/iu/2f/d2/iu2fd2ums7tmrkyqb4hrbhuenzg.png)
Mari kita mulai dengan warisan sederhana dan secara bertahap akan menjadi lebih kompleks.
Daftar akhir tingkat prioritas dapat ditemukan di akhir dokumen.
, , .
![Grup dengan Variabel](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
Poin yang paling jelas adalah bahwa aturan "semakin dekat variabel ke kode, semakin penting itu" bekerja pertama untuk grup, dan kemudian aturan yang sama untuk variabel di dalam .gitlab-ci.yml, tetapi hanya dengan syarat bahwa variabel dalam kelompok tidak didefinisikan .
Selanjutnya, tempat yang penting adalah pemahaman bahwa ruang global untuk inti dan .gitlab-ci.yml yang dikecualikan adalah umum. Dan file di mana inklusi terjadi diutamakan.