Jenkins: How To Trigger Another Pipeline From Current Job? – 1 Cool Secret To Do It!

You are currently viewing Jenkins: How To Trigger Another Pipeline From Current Job? – 1 Cool Secret To Do It!
Share This Post, Help Others, And Earn My Heartfelt Appreciation! :)
4.9
(1371)

In this tutorial I will show you how to trigger another pipeline from current job. You have probably encountered the situation that you wanted to run another Jenkins job from the current pipeline. It’s very easy to achieve it! We will go step by step through the stages of the work phase, its recovery and check the logs.

Last time I wanted to run the E2E tests pipeline in Jenkins after each commit to develop branch with condition if the current pipeline will finish with success. That’s our task for this post.

Trigger Another Pipeline From Current Job

You probably have installed Jenkins or have access to Jenkins instance. I case if you don’t have I am referring you to the post Jenkins & Nexus & SonarQube: Build the DevOps environment using Docker-Compose.

You can easily isolate the Jenkins removing Nexus and Sonar services from docker-compose.yml file and stay only with Jenkins service.

Two Pipelines

We will build two pipelines. The first job will be started manually, while the second one will be started automatically from the first one:

  1. “Build_pipeline” – to build, test and upload to nexus repository our project and etc.
  2. “E2E_tests_pipeline” – to run our tests

Additionally we will pass parameter from first pipeline to second one. At the end of post I will show you the rest of out-of-the box functions like:

  • Wait for completion
  • Propagate errors
  • Quiet period

E2E_test_pipeline

We will start from the end. Firstly create E2E_test_pipeline and add one String parameter like in the picture below:

Jenkins: How to trigger another pipeline from current job? - e2e easy example!

In the section of pipeline script please put the below code:

pipeline {
    agent any 
    stages {
        stage('E2E Tests') {
            steps {
                echo "Running E2E tests..."
                echo "MY_PARAM=${env.MY_PARAM}"
            }
        }
    }
}

Build Pipeline

Now we will build our main pipeline which will trigger the e2e tests.

pipeline {
    agent any 
    stages {
        stage('Build') {
            steps {
                echo "Build step"
            }
        }
        
        stage('Test') {
            steps {
                echo "Test step"
            }
        }
        
        stage('Upload artifacts') {
            steps {
                echo "Upload artifacts step"
            }
        }
    }
    post {
        success {
            echo 'Run E2E Test pipeline!'
            build job: 'E2E_tests_pipeline', parameters: [string(name: 'MY_PARAM', value: 'value from Build pipeline')]
        }
    }
      
}

Run Pipeline

Now we are ready to run the Build pipeline and check if we achieved what we wanted 🙂

As you can see and screenshots below the first pipeline was run and at the end in Post Actions we triggered e2e tests.

Jenkins: How to trigger another pipeline from current job? - e2e easy example!

And check the E2E_tests_pipeline in another tab.

Jenkins: How to trigger another pipeline from current job? - e2e easy example!

Console logs

Let’s check the logs of job executions. Here we can see how the pipeline went through steps. To emphasize the separation I have added the echo command in each step. Please find the special-lines which I marked in the logs which indicates that job was triggered by another pipeline.

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/Build_pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Build step
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Test step
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Upload artifacts)
[Pipeline] echo
Upload artifacts step
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Run E2E Test pipeline!
[Pipeline] build (Building E2E_tests_pipeline)
Scheduling project: E2E_tests_pipeline
Starting building: E2E_tests_pipeline #3
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Console Logs From E2E_tests_pipeline

Please find that we see in the console log the value of passed parameter.

Started by upstream project "Build_pipeline" build number 11
originally caused by:
 Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/E2E_tests_pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (E2E Tests)
[Pipeline] echo
Running E2E tests...
[Pipeline] echo
MY_PARAM=value from Build pipeline
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Additional Parameters

Due to the rest of functions are ENABLED by default we can explicitly disable them. Disable “Wait for completion” setting wait flag to false:

build job: 'E2E_tests_pipeline', parameters: [string(name: 'MY_PARAM', value: 'my default value')], wait: false

Disable “Propagate errors” setting propagate flag to false. It can help you if you don’t want to propagates errors from triggered (E2E_test_pipeline) to invoking pipeline (Build_pipeline).

build job: 'E2E_tests_pipeline', parameters: [string(name: 'MY_PARAM', value: 'my default value')], propagate: false

Set “Quiet period” value. This allow you to control the delay time in seconds before the second pipeline will be built and run. In example below I set quietPeriod: 10 [seconds].

build job: 'E2E_tests_pipeline', parameters: [string(name: 'MY_PARAM', value: 'my default value')], propagate: false, quietPeriod: 10

That’s all about how to trigger another pipeline from current job! I hope that this post helped you to resolve your problem! Thanks in advance for sharing this post to other people! Let other people also get help!

Jenkins pipeline call another pipeline with parameters, current pipeline

If you enjoyed this post please add the comment below and share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 1371

No votes so far! Be the first to rate this post.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments