Gitlab-ci 仅在由 specific 分支触发时才对 运行 作业进行规则
Gitlab-ci rules to run job only when triggerd by specific branch
假设我们在 GitLab 上有两个项目 A 和 B,我想要一个规则,允许项目 B 中定义的下游作业仅在被项目 A 中的触发器触发时 运行。
项目A gitlab-ci.yml:
stages:
- build
build_container:
stage: build
trigger:
project: B
项目 B gitlab-ci.yml:
build:
stages:
- build
rules: only run this job when it's triggered by project A
script: commands to build something
如果您使用触发器,项目 A 的 yaml 中定义的所有变量都会传递给您的项目 B。您可以在项目 B 的高级 only
语句中检查此变量。https://docs.gitlab.com/ee/ci/jobs/job_control.html#only-variables--except-variables-examples )
项目 A
stages:
- build
build_container:
stage: build
variables:
UPSTREAM_PROJECT: A
trigger:
project: B
项目 B
stages:
- build
build:
stage: build
script:
- // do something
only:
variables:
- $UPSTREAM_PROJECT == "A"
除了使用only
验证变量是否存在外,您还可以在脚本中手动检查变量
build:
stage: build
script:
- |
if [ "$UPSTREAM_PROJECT" == "A" ]; then
// do something
fi
假设我们在 GitLab 上有两个项目 A 和 B,我想要一个规则,允许项目 B 中定义的下游作业仅在被项目 A 中的触发器触发时 运行。
项目A gitlab-ci.yml:
stages:
- build
build_container:
stage: build
trigger:
project: B
项目 B gitlab-ci.yml:
build:
stages:
- build
rules: only run this job when it's triggered by project A
script: commands to build something
如果您使用触发器,项目 A 的 yaml 中定义的所有变量都会传递给您的项目 B。您可以在项目 B 的高级 only
语句中检查此变量。https://docs.gitlab.com/ee/ci/jobs/job_control.html#only-variables--except-variables-examples )
项目 A
stages:
- build
build_container:
stage: build
variables:
UPSTREAM_PROJECT: A
trigger:
project: B
项目 B
stages:
- build
build:
stage: build
script:
- // do something
only:
variables:
- $UPSTREAM_PROJECT == "A"
除了使用only
验证变量是否存在外,您还可以在脚本中手动检查变量
build:
stage: build
script:
- |
if [ "$UPSTREAM_PROJECT" == "A" ]; then
// do something
fi