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

Jenkins: How To Trigger Another Pipeline From Current Job? - 1 Secret To Do It!
Share this post and Earn Free Points!

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.

Introduction

Jenkins

Jenkins" is an open-source automation server that is used to automate parts of the software development process. It is a popular tool for continuous integration and delivery, allowing developers to automate the build, test, and deployment of their applications.

With Jenkins", you can create a pipeline of tasks that are automatically triggered based on certain events, such as code commits or the completion of a test suite. Jenkins" can also be configured to send notifications about the status of the pipeline, so that you can stay informed about the progress of your builds and deployments.

To use Jenkins", you will need to install it on a server and set up a pipeline for your project. This typically involves creating a configuration file that defines the steps in the pipeline and specifying the triggers that will cause the pipeline to be executed. Jenkins" provides a web-based user" interface that allows you to view the status of your pipelines, as well as configure and manage your builds and deployments.

Jenkins" is a powerful and flexible tool that is widely used in the software development industry. It can be used to automate a wide range of tasks, including building, testing, and deploying applications, as well as performing other types of automation.

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 can trigger another pipeline from the current job in Jenkins" by using the build step in the pipeline script. This step allows you to specify the name of the job that you want to trigger, as well as any parameters that you want to pass to the triggered job.

Here’s an example of how you might use the build step to trigger another pipeline from the current job:

pipeline {
    stages {
        stage('Trigger Other Pipeline') {
            steps {
                build job: 'other-pipeline', parameters: [
                    string(name: 'param1', value: 'value1'),
                    string(name: 'param2', value: 'value2')
                ]
            }
        }
    }
}

In this example, the build step will trigger the pipeline job named “other-pipeline”, and pass it two string parameters named “param1” and “param2” with the specified values.

You can also use the build step to trigger a job that is not a pipeline job. In this case, the triggered job will be executed as a standalone build, rather than as part of a pipeline.

Keep in mind that the build step will block the execution of the current pipeline until the triggered job has completed. If you want to trigger a job asynchronously and continue with the execution of the current pipeline, you can use the trigger step instead of the build step.

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,
jenkins trigger another job,jenkins pipeline trigger another job,jenkins trigger another job with parameters,jenkins call another job with parameters,jenkins pipeline call another job,jenkinsfile trigger another job,
jenkins pipeline call another pipeline
Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 1375

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?