You are currently viewing [SOLVED] Jenkins: How To Trigger Another Pipeline From Current Job? – 1 Cool Secret To Do It!
Could You Please Share This Post? I Appreciate It And Thank YOU! :) Have A Nice Day!
4.9
(1372)

In this tutorial I will show you how to trigger another pipeline from current job. In other words we can say: Jenkins run seed 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.

What Is The Seed Job?

The seed job is the normal job, but it’s executed from another job as a child process, which from executor side is treat as normal execution of another job.

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:

[SOLVED] Jenkins: How To Trigger Another Pipeline From Current Job? - 1 Cool Secret To Do It!

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.

Jenkins Trigger Another Job If Success

Note that the next job will only be triggered if the basic job is successful. You can take advantage of many other post conditions that Jenkins out of the box gives us, like:

  • always
  • changed
  • aborted
  • failure
  • success
  • unstable
  • unsuccessful
  • cleanup
  • fixed
  • regression

See the full list with description on official Jenkins site.

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')]
        }
    }
      
}

Jenkins Run Seed Job – 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).

# Script
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].

# Script
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
Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!

BigData-ETL: image 7YOU MIGHT ALSO LIKE

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 1372

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

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?