top of page
Writer's pictureSumit Raj

LetsDevOps: How to trigger pipeline from another pipeline in GitLab.

Introduction

In this article we will discuss how to trigger pipeline from another pipeline. It is also called as triggering the downstream pipeline.


Prerequisites

To proceed further you must know the basic of GitLab YAML. If you are new you can follow my GitLab tutorial with Demo.


Option to Trigger

  • Parent-Child pipeline

  • Multi-Project Pipeline

What is Parent-Child pipeline

A parent pipeline is a pipeline that triggers a downstream pipeline in the same project. The downstream pipeline is called a child pipeline.



What is Multi-Project Pipeline

A pipeline in one project can trigger downstream pipelines in another project, called multi-project pipelines


Scenarios

  1. If you want to trigger pipeline from another pipeline.

  2. Lets assume you have multiple Project in single GitLab Repository and for each module you want to setup separate pipeline.

  3. Triggering pipeline from Another GitLab Project.

  4. Assume you want to build your project for multiple Platform like Windows/Mac OS/Linux.



Keyword to know

Here are some keywords which are useful for setup.


trigger --> This to be used with include keyword
include --> Helps to define the child pipeline
project --> Keyword for defining the GitLab Project
rules --> Define Rule to trigger the pipeline on Condition

Scenario 1:

If you want to trigger pipeline from another pipeline.

Technique: Parent-Child

Keyword to use: trigger & include


Parent Pipeline

Syntax to add in your parent Pipeline.


trigger-pipeline:
  stage: build
  trigger:
    include: <Path from Repo to Child>/.gitlab-ci.yml

Example:

stages:
  - build
build-job:
  stage: build
  script:
    - echo "I am building the Job"
trigger-pipeline:
  stage: build
  trigger:
    include: <ChildFolder>/.gitlab-ci.yml

Child Pipeline

Example

BuildNugetPackage/.gitlab-ci.yml

stages:
 - unitest
run-unitest:
    stage: unitest
    script:
      - echo "I am in unittest"

Step 1: Create parent pipeline in root of repo --> .gitlab-ci.yml.


Step 2: Add reference of your child yml in parent yml file.


trigger-pipeline:
  stage: build
  trigger:
    include: <Path from Root Repo>/.gitlab-ci.yml


Demo Run


Scenario 2:

Assume I have two different Project in same repository and I want to trigger the project pipeline when there is change in specific Project.

Technique: Parent-Child

Keyword to use: trigger & include


Step 1: Create parent pipeline in root of repo --> .gitlab-ci.yml


Step 2: Add the Job with trigger syntax with the path of the Sub Project.



stages:
  - triggers
trigger_srstest:
  stage: triggers
  trigger:
    include: srstest/.gitlab-ci.yml
  rules:
    - changes:
        - srstest/**/*
trigger_srstest2:
  stage: triggers
  trigger:
    include: srstest2/.gitlab-ci.yml
  rules:
    - changes:
        - srstest2/**/*


Step 2: In the above example I have included for two Project. You can include multiple like adding the below syntax.


trigger_srstest3:
  stage: triggers
  trigger:
    include: srstest3/.gitlab-ci.yml
  rules:
    - changes:
        - srstest3/**/*

Step 3: Make sure the path under the include is pointing to correct yml file.


Demo Run


Scenario 3:

Triggering pipeline from Another GitLab Project.

Technique: Multi-Project

Keyword to use: trigger & project


Step 1: Get the Project name and group of which you want to trigger the pipeline.


In my case


Group Name: srsgrouptest [4]

Project Name: srstest-trigger [5]


Step 2: Update the .gitlab-ci.yml in the another Project with the reference of Step 1.


trigger-multproject:  
   stage: build  
   trigger:    
     project: <GroupName>/<Project Name>


Example:



stages:
  - build
build-job:
  stage: build
  script:
    - echo "I am building the Job"
trigger-multproject:
  stage: build
  trigger:
    project: srsgrouptest/srstest-trigger

Demo Run


Scenario 3:

You can try and if any issue write to me inbox.


Demo



Reference


You can always modify your rules for trigger condition. More detail can be found here.



You you are new to GitLab you can follow this tutorial.







1 Comment


Sathish Kumar
Sathish Kumar
Nov 24, 2023

Hi Sumit, In multip-project-pipeline, i have stored sensitive variables via settings->ci/cd->variables in the qa repository. I'm running automation scripts from dev repo i have written the following code in .gitlab-ci.yml file in dev repo,

include:

project: 'xxxx/xxx'

file: './gitlab-ci.yml'

ref: 'main'

gitlab-ci.yml file available in qa repo is working fine except variables. Variables stored in qa repo is not retrieved in dev repo.

Like
bottom of page