Gitlab-CI如何继承环境变量?

Gitlab变量可以在几个地方设置:


  1. 在群组设定中
  2. 在项目设置中
  3. 内.gitlab-ci.yml

在这种情况下,可以将组和项目的设置中的变量设置为“文件”或“普通变量”,然后选中“保护”和“掩码”框。



让我们从简单的继承开始,并将逐渐变得更复杂。


优先级的最终列表可在文档末尾找到。


[]


, , .



变量组


.gitlab-ci.yml


image: busybox:latest
variables:
  GIT_STRATEGY: none

echo:
  stage: test
  script:
    - echo $MSG


$ echo $MSG
B

B, .




.gitlab-ci.yml []


: , .


c



.gitlab-ci.yml


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


  • echo:


    $ echo $MSG
    Custom in global .gitlab-ci.yml
    Job succeeded

  • echo with vars:


    $ echo $MSG
    Custom in job .gitlab-ci.yml
    Job succeeded




.gitlab-ci.yml []


2 . .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


  • echo:


    $ echo $MSG
    Y
    Job succeeded

  • echo with vars:


    $ echo $MSG
    Y
    Job succeeded




[]


! , .gitlab-ci.yml .


c


.


.gitlab-ci.yml


. , , .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


  • echo:


    $ echo $MSG
    project-3
    Job succeeded

  • echo with vars:


    $ echo $MSG
    project-3
    Job succeeded




[]



– Null


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


  • echo:


    $ echo $MSG
    Job succeeded

  • echo with vars:


    $ echo $MSG
    Job succeeded




[]


project-2 project-3
.


c



.gitlab-ci.yml


.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'


  • echo:


    $ echo $MSG
    B
    Job succeeded

  • echo with vars:


    $ echo $MSG
    B
    Job succeeded




[]


project-2 project-3.
C : , .


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'


  • echo:


    $ echo $MSG
    With include .gitlab-ci.yml
    Job succeeded

  • echo with vars:


    $ echo $MSG
    Custom in job .gitlab-ci.yml
    Job succeeded


:


  1. ( )
  2. .gitlab-ci.yml


最明显的一点是,规则“对变量越接近代码,它越重要”首先适用于组,然后对.gitlab-ci.yml中的变量适用相同的规则,但前提是未定义组中的变量。
此外,重要的一点是要了解核心和排除的.gitlab-ci.yml的全局空间是通用的。并且其中包含发生的文件优先。


All Articles