devnull_dz
devnull_dz

Reputation: 3

Gitlab-ci rules to run job only when triggerd by specific branch

let's say we have two projects A and B on GitLab, and I want a rule that allows a downstream job defined in project B to only run when it's triggered by a trigger in project A.

Project A gitlab-ci.yml:

stages:
  - build

build_container:
  stage: build
  trigger:
    project: B

Project B gitlab-ci.yml:

build:
  stages:
    - build
  rules: only run this  job when it's triggered by project A
  script: commands to build something

Upvotes: 0

Views: 5508

Answers (1)

JFWenisch
JFWenisch

Reputation: 420

If you use trigger, all variables defined in the yaml of project A are passed to your project B. You could check this variable then within an advanced only statement in project B. https://docs.gitlab.com/ee/ci/jobs/job_control.html#only-variables--except-variables-examples)

project A

stages:
  - build

build_container:
  stage: build
  variables:
    UPSTREAM_PROJECT: A
  trigger:
    project: B

project B

stages:
  - build

build:
  stage: build
  script: 
   - // do something
  only:
    variables:
      - $UPSTREAM_PROJECT == "A"

Besides using onlyto verify if the variable is existing, you could also manually check the variable within the script

build:
      stage: build
      script: 
       - |
       if [ "$UPSTREAM_PROJECT" == "A" ]; then
       // do something
       fi
       

Upvotes: 3

Related Questions