Jenkins: 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!

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

Jenkins

You probably have installed Jenkins or have access to Jenkins instance. I case if you don’t have I am reffering 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:

  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:

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 the Jenkins

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.

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 parametes

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

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

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments