0

I have a repo A(upstream) and a repo B(downstream). After some jobs complete in repo A; I want to trigger some jobs in repo B. I was able to achieve that.

trigger_repo_B:
  stage: trigger_repo_B
  trigger:
    project: test/repo_B

What I havent been able to figure out is - how do I go about triggering repo B jobs for a non-existent branch in repo B. For example I can trigger jobs in repo B for a specific branch C if C exists but if C does not exist the pipeline is in a pending state. I want to be able to create a branch in B and then run the jobs in B if the branch C does not exist.

trigger_repo_B:
  stage: trigger_repo_B
  trigger:
    project: test/repo_B
    branch: C

Any ideas? The only way I could think of it working is to do a before_script where I clone the repo and create a branch before triggering the pipeline in B

LateNightDev
  • 185
  • 1
  • 2
  • 10

1 Answers1

1

Instead of listing the branch name in the trigger config, instead pass a variable where the value is the branch you want to create. Then in your downstream pipeline, you could add a stage and job to run before everything else that checks to see if the variable exists. If not, just exit 0 and let the pipeline continue as usual. But if it is set, create the branch, then continue.

That could look something like this:

#repo_A .gitlab-ci.yml
stages:
  - trigger_repo_B

trigger:
stage: trigger_repo_B
variables:
  BRANCH_TO_CREATE: branch_name
trigger:
  - project: test/repo_B
#repo_B .gitlab-ci.yml
stages:
  - prep_work
  - build

Create Branch: 
stage: prep_work
script:
  - if [ -z ${BRANCH_TO_CREATE+x} ]; then git checkout -b $BRANCH_TO_CREATE; fi
  # see https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash to explain the conditional above

Build:
stage: build
script:
  - ...
Adam Marshall
  • 6,369
  • 1
  • 29
  • 45