Search code examples
gitlab-ci

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


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?


Solution

  • 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"