背景是我有多个项目,希望其中一个项目被触发的时候,联动另外一个项目自动打包。然后我就看文档尝试操作了一下,所以有本文。
官方文档参考:https://gitlab.cn/docs/14.5/jh/ci/pipelines/multi_project_pipelines.html
不知道是不是我使用的方法不对,用上面的文档去操作有报错:(
可以使用gitlab提供的trigger方法,通过curl调用接口,触发下游的打包任务,比如:
curl --request POST \
--form "token=$TRIGGER_TOKEN" \
--form "ref=$DOWNSTREAM_BRANCH" \
--form "variables[UPSTREAM_VERSION]=$BUILD_VERSION" \
--form "variables[PIPELINE_SOURCE]=upstream" \
"https://gitlab.yeshen.org/api/v4/projects/$DOWNSTREAM_PROJECT_ID/trigger/pipeline"
注意点有
-
project在gitlab的项目上有显示,比如 https://gitlab.yeshen.org/yeshen-awesome/yeshen-awesome-sdk,id就是 3
-
使用 variables 传递自定义参数到下游仓库,比如 --form “variables[PIPELINE_SOURCE]=upstream”
-
下游仓库需要提供token,token可以在 下游项目的 Settings > CI/CD > Pipeline triggers 中生成。
即在yeshen-awesome项目中生成一个token,在这里: https://gitlab.yeshen.org/yeshen-awesome/yeshen-awesome-sdk/-/settings/ci_cd Pipeline triggers
-
上游仓库需配置token,上游项目的 CI/CD 变量 DOWNSTREAM_TRIGGER_TOKEN,在 Settings > CI/CD > Variables 中配置。
即在testingsdk中配置token,在这里:https://gitlab.yeshen.org/yeshen-upstream/yeshen-upstream-client/-/settings/ci_cd Variables
一份参考的配置可以是如下
上游配置
stages:- relase- triggertrigger_downstream:stage: triggertags:- yeshen-pcvariables:DOWNSTREAM_PROJECT_ID: "3"DOWNSTREAM_BRANCH: "branch-13"TRIGGER_TOKEN: $DOWNSTREAM_TRIGGER_TOKENscript:- |curl --request POST \--form "token=$TRIGGER_TOKEN" \--form "ref=$DOWNSTREAM_BRANCH" \--form "variables[UPSTREAM_VERSION]=$BUILD_VERSION" \--form "variables[PIPELINE_SOURCE]=upstream" \"https://gitlab.yeshen.org/api/v4/projects/$DOWNSTREAM_PROJECT_ID/trigger/pipeline"rules:- if: $CI_COMMIT_TAGneeds:- job: relase_job artifacts: true
下游配置
stages:- testingtesting_job:stage: testingimage: yeshen/android_build:1.0.3tags:- yeshen-pcscript:- echo "version_ $UPSTREAM_VERSION"- echo "Start building the sdk...testing"- echo "finish build"rules:- if: $PIPELINE_SOURCE == "upstream"