参考文档:https://docs.gitlab.com/ee/ci/yaml/#triggerforward
今天给大家介绍一个gitlab CI/CD的关键字 - forward,该关键字是一个比较偏的功能,但同时也是一个很实用的功能,我们通过在gitlab的ci文件中使用forward关键字,可以将变量传递到下游的流水线。
文章目录
- 1. 介绍
- 1.1 forward关键字
- 1.2 yaml_variables和pipeline_variables的区别
- 2. 项目配置
- 2.1 项目信息
- 2.2 pipeline ci文件
- 2.3 project-b和project-c ci文件
- 2.4 project-a ci文件(开启pipeline trigger)
- 3. 场景使用
- 3.1 场景1: 任何参数都不传递
- 3.2 场景2: 传参数,并覆盖当前流水线的变量
- 3.3 场景3:触发项目b和c,不传入trigger参数
- 3.4 场景4:触发项目b和c,传入trigger参数
- 3.5 场景5: 触发项目b和c,传入trigger参数(验证)
1. 介绍
1.1 forward关键字
默认情况下,只有yaml定义的变量被传递给下游管道,使用forward关键字,现在可以传递它手动管道变量下游管道。
- forward:yaml_variables是一个已经存在的行为,默认为true。当为true时,将传递给yaml定义的变量
到下游管道。
- forward:pipeline_variables是一个新特性,默认为false。当为true时,手动管道变量被传递给下游管道。
1.2 yaml_variables和pipeline_variables的区别
-
pipeline_variables可以将参数传递到下级pipeline,即便是在当前的pipeline中定义好参数,也会随着传入的参数来覆盖,见场景4和5。
-
yaml_variables也可以将参数传递到下级pipeline,但是需要在当前的pipeline中定义好参数,不会随着传入的参数来覆盖,见场景4和5。
2. 项目配置
2.1 项目信息
在pipeline-trigger组(定义全局变量:GLOBAL_VAR=master,release)下有四个项目:
- project-a: 父pipeline;
- project-b(项目级变量:PROJECT_VAR=project): 子pipeline;
- project-c(无项目级变量): 子pipeline;
- centralized-ci : 存放pipeline ci的文件。
2.2 pipeline ci文件
存放在centralized-ci项目中,打印全局变量GLOBAL_VAR,项目变量PROJECT_VAR,trigger传入的变量TRIGGER_VAR):
template/common-temp.yml
image: busybox:latestbuild1:stage: buildscript:- echo "Do your build here"test1:stage: testscript:- echo "Do a test here"- echo "For example run a test suite"- echo "$GLOBAL_VAR $PROJECT_VAR $TRIGGER_VAR"deploy1:stage: deployscript:- echo "Do your deploy here"
2.3 project-b和project-c ci文件
include:- project: "cs-test-group1/kxwang/pipeline-trigger/centralized-ci"ref: mainfile: '/template/common-temp.yml'
2.4 project-a ci文件(开启pipeline trigger)
stages:- build- trigger
variables:GLOBAL_VAR: hahaPROJECT_VAR: xixiTRIGGER_VAR: hehebuild_job:stage: buildscript:- echo "$GLOBAL_VAR $PROJECT_VAR $TRIGGER_VAR"trigger_project-b:stage: triggervariables: # default variables for each jobGLOBAL_VAR: haha123PROJECT_VAR: xixi456TRIGGER_VAR: hehe789trigger:project: cs-test-group1/kxwang/pipeline-trigger/project-bbranch: master#include:# #- cs-test-group1/kxwang/pipeline-trigger/centralized-ci/template/common-temp.yml# #- cs-test-group1/kxwang/pipeline-trigger/project-b/.gitlab-ci.yml'# - project: cs-test-group1/kxwang/pipeline-trigger/project-b# ref: master# file: '.gitlab-ci.yml'strategy: dependforward:pipeline_variables: truerules:- if: '$PROJECT_LIST =~ /.*project-b.*/'trigger_project-c:stage: triggertrigger:project: cs-test-group1/kxwang/pipeline-trigger/project-cbranch: master#include:# #- cs-test-group1/kxwang/pipeline-trigger/centralized-ci/template/common-temp.yml# - project: cs-test-group1/kxwang/pipeline-trigger/project-c# ref: master# file: '.gitlab-ci.yml'strategy: dependforward:yaml_variables: true# GLOBAL_VAR: haha# PROJECT_VAR: xixi# TRIGGER_VAR: heherules:- if: '$PROJECT_LIST =~ /.*project-c.*/'
3. 场景使用
3.1 场景1: 任何参数都不传递
curl -X POST \--fail \-F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \-F ref=master \https://jihulab.com/api/v4/projects/103863/trigger/pipeline
只运行project-a的build_job项目,此时
GLOBAL_VAR继承群组级设置的变量;
PROJECT_VAR在当前pipeline中有定义,因此会打印;
TRIGGER_VAR在当前pipeline中有定义,因此会打印。
3.2 场景2: 传参数,并覆盖当前流水线的变量
curl -X POST \--fail \-F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \-F ref=master \-F variables[GLOBAL_VAR]="123" \-F variables[PROJECT_VAR]="456" \-F variables[TRIGGER_VAR]="789" \https://jihulab.com/api/v4/projects/103863/trigger/pipeline
只运行project-a的build_job项目,此时
GLOBAL_VAR继承群组级设置的变量;
PROJECT_VAR在当前pipeline中有定义,因此会打印;
TRIGGER_VAR在当前pipeline中有定义,因此会打印。
3.3 场景3:触发项目b和c,不传入trigger参数
curl -X POST \--fail \-F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \-F ref=master \-F variables[PROJECT_LIST]="project-b,project-c" \https://jihulab.com/api/v4/projects/103863/trigger/pipeline
Project-b(由于在pipeline的项目b的job段定义了变量,因此变量也继承了下来)
Project-c(由于在pipeline的全局定义了变量,因此变量也继承了下来)
3.4 场景4:触发项目b和c,传入trigger参数
curl -X POST \--fail \-F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \-F ref=master \-F variables[TRIGGER_VAR]="customer success" \-F variables[PROJECT_LIST]="project-b,project-c" \https://jihulab.com/api/v4/projects/103863/trigger/pipeline
project-b(在pipeline中定义项目b的job段定义了变量,且使用了forwaord:pipeline_variables,传入的变量可以覆盖了job中定义的variables)
project-c(由于在pipeline中未定义c的变量,而全局定义了变量,且使用了forwaord:yaml_variables,因此将pipeline全局中的变量继承传递了下来)
3.5 场景5: 触发项目b和c,传入trigger参数(验证)
调整project-a的ci文件
stages:- build- trigger#variables: # default variables for each job
# GLOBAL_VAR: haha
# PROJECT_VAR: xixi
# TRIGGER_VAR: hehebuild_job:stage: buildscript:- echo "$GLOBAL_VAR $PROJECT_VAR $TRIGGER_VAR"trigger_project-b:stage: triggervariables:GLOBAL_VAR: haha123PROJECT_VAR: xixi456TRIGGER_VAR: hehe789trigger:project: cs-test-group1/kxwang/pipeline-trigger/project-bbranch: master#include:# #- cs-test-group1/kxwang/pipeline-trigger/centralized-ci/template/common-temp.yml# #- cs-test-group1/kxwang/pipeline-trigger/project-b/.gitlab-ci.yml'# - project: cs-test-group1/kxwang/pipeline-trigger/project-b# ref: master# file: '.gitlab-ci.yml'strategy: dependforward:yaml_variables: truerules:- if: '$PROJECT_LIST =~ /.*project-b.*/'trigger_project-c:stage: triggervariables:TRIGGER_VAR: hehetrigger:project: cs-test-group1/kxwang/pipeline-trigger/project-cbranch: master#include:# #- cs-test-group1/kxwang/pipeline-trigger/centralized-ci/template/common-temp.yml# - project: cs-test-group1/kxwang/pipeline-trigger/project-c# ref: master# file: '.gitlab-ci.yml'strategy: dependforward:pipeline_variables: true# GLOBAL_VAR: haha# PROJECT_VAR: xixi# TRIGGER_VAR: heherules:- if: '$PROJECT_LIST =~ /.*project-c.*/'
再次触发
curl -X POST \--fail \-F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \-F ref=master \-F variables[TRIGGER_VAR]="customer success" \-F variables[PROJECT_LIST]="project-b,project-c" \https://jihulab.com/api/v4/projects/103863/trigger/pipeline
project-b(在pipeline中定义项目b的job段定义了变量,且使用了forwaord:yaml_variables,传入的变量不会覆盖了job中定义的variables)
project-c(由于在pipeline中未定义c的变量,而全局定义了变量,且使用了forwaord:pipeline_variables,因此将pipeline全局中的变量覆盖后传递了下来)