How does Gitlab-CI inherit environment variables?

Gitlab variables can be set in several places:


  1. In group settings
  2. In the project settings
  3. Inside .gitlab-ci.yml

In this case, the variables in the settings of the groups and the project can be set as "file" or "ordinary variable" and check the "protected" and "mask" boxes.



Let's start with simple inheritance and will gradually become more complex.


A final list of priority levels can be found at the end of the document.


[]


, , .



Groups with Variables


.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


The most obvious point is that the rule "the closer the variable 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 defined .
Further, an important place is the understanding that the global space for the core and the excluded .gitlab-ci.yml is common. And the file in which the inclusion occurs takes precedence.


All Articles