Trigger multi-project build on branch, but only if branch exists
Asked Answered
S

1

6

I have projects in many git repositories, starting with the base api and then branching outwards. When the API materially changes, I want to verify that downstream projects (that use the particular API) work correctly. I hoped this workflow would be supported out of the box:

  1. Make changes on branch to base API
  2. Create same named branch on a subset of projects that may be affected by the change
  3. Execute the build triggering children with
run-A:
  stage: .post
  trigger: 
    project: a
    branch: $CI_COMMIT_BRANCH
    strategy: depend

run-B:
...

Sometimes I want to verify A, sometimes B, sometimes both or neither. If I don't have the same branch in A and B, I start getting

Downstream pipeline cannot be created; reference cannot be found

How can I get gitlab to depend on the results of other project builds, but ignore the downstream project if the branch doesn't exist?

Sonar answered 3/10, 2022 at 4:35 Comment(2)
Same issue! Did you find a solution? It seems that the variable (even pre-defined ones like in your example ($CI_COMMIT_BRANCH)) does not work? At least that is what I am seeing. Putting in a constant there i.e, branch: develop works. But, I am passing the branch name as a variable to the upstream project.Cannabis
related: gitlab.com/gitlab-org/gitlab/-/issues/231782Tobin
D
0

I don't think there is a completely native way of doing this, but you could insert a pre-job, that checks if the branches exist and only run the trigger-jobs if they do.

This is a modification of something I run, but haven't had the chance to test it in that form:

stages:
  - check_branch
  - deploy

variables:
  PROJECT1_REPO: "https://your-gitlab-domain.com/group/PROJECT1.git"
  PROJECT2_REPO: "https://your-gitlab-domain.com/group/PROJECT2.git"

# Check if the branch exists
check_project_branches:
  stage: check_branch
  script:
    - |
      if git ls-remote --heads $PROJECT1_REPO $CI_COMMIT_BRANCH | grep -q $CI_COMMIT_BRANCH; then
        echo "PROJECT1_BRANCH_EXISTS=true" >> .env
      else
        echo "PROJECT1_BRANCH_EXISTS=false" >> .env
      fi
    - |
      if git ls-remote --heads $PROJECT2_REPO $CI_COMMIT_BRANCH | grep -q $CI_COMMIT_BRANCH; then
        echo "PROJECT2_BRANCH_EXISTS=true" >> .env
      else
        echo "PROJECT2_BRANCH_EXISTS=false" >> .env
      fi
  artifacts:
    reports:
      dotenv: .env

# Deploy to PROJECT1 if the branch exists
deploy_project1:
  stage: deploy
  trigger:
    project: group/PROJECT1
    branch: $CI_COMMIT_BRANCH
    strategy: depend
  only:
    variables:
      - $PROJECT1_BRANCH_EXISTS == "true"

# Trigger PROJECT2 pipeline if the branch exists
trigger_project2_pipeline:
  stage: deploy
  trigger:
    project: group/PROJECT2
    branch: $CI_COMMIT_BRANCH
    strategy: depend
  only:
    variables:
      - $PROJECT2_BRANCH_EXISTS == "true"
Decelerate answered 29/8 at 12:14 Comment(1)
Looks legit. Sad though. It works well enough to ignore the results too (what I did); you just get an orange warning when it's not there, and let the other devs do reviews properly... When I get back to modifying some builds, I'll take this and see if I can make shared scripts work with variablesSonar

© 2022 - 2024 — McMap. All rights reserved.