Wie erbt Gitlab-CI Umgebungsvariablen?

Gitlab-Variablen können an mehreren Stellen festgelegt werden:


  1. In Gruppeneinstellungen
  2. In den Projekteinstellungen
  3. Innerhalb von .gitlab-ci.yml

In diesem Fall können die Variablen in den Einstellungen der Gruppen und des Projekts als "Datei" oder "normale Variable" festgelegt werden und die Kontrollkästchen "geschützt" und "Maske" aktivieren.



Beginnen wir mit der einfachen Vererbung und werden allmählich komplexer.


Eine endgültige Liste der Prioritätsstufen finden Sie am Ende des Dokuments.


[]


, , .



Gruppen mit Variablen


.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


Der offensichtlichste Punkt ist, dass die Regel "Je näher die Variable am Code ist, desto wichtiger ist sie" zuerst für Gruppen und dann für Variablen in .gitlab-ci.yml gilt, jedoch nur unter der Bedingung, dass die Variablen in den Gruppen nicht definiert sind .
Ein wichtiger Ort ist außerdem das Verständnis, dass der globale Raum für den Kern und das ausgeschlossene .gitlab-ci.yml gemeinsam ist. Und die Datei, in der die Aufnahme erfolgt, hat Vorrang.


All Articles