1. Overview

Jenkins is an automation server that provides developers with the ability to build, test, and deploy applications automatically. The Jenkins server executes jobs, which can be triggered manually or automatically. Also, we can run these jobs simultaneously or in a specific order.

In this tutorial, we’ll walk through the process of creating a freestyle job and a pipeline job to trigger another job.

2. Job Setup

In Jenkins, we can create jobs to run code coverage tests, integration tests, and deploy applications. To execute a set of tasks, Jenkins provides different types of jobs. Furthermore, depending on the task, we may have to trigger an internal job.

Here, we’ll build two jobs, a parent pipeline job, and a child freestyle job. We’ll run the parent job manually from the Jenkins UI, whereas the child job will be triggered internally by the parent job.

Let’s look at the child’s job first.

2.1. Create a Freestyle Child Job

Before we start, let’s build a demo setup first. We’ll use the freestyle job type for creating the child job. The freestyle build jobs are easy to use and have many pre-made options. Furthermore, to create a freestyle job, we need to follow these steps:

  • Click on New Item in the Jenkins dashboard

  • Set a “Job Name” as childJob

  • Select the “Job Type” as “Freestyle project”

  • Add echo “childJob” command in the execute shell of the build step

  • Save the job

Everything should look similar to this:

Save the job

As a result of the above steps, we are now able to create a simple freestyle job that can run independently or can be triggered by another job.

2.2. Create a Pipeline Parent Job

A Jenkins pipeline is a series of interrelated events or jobs that produce continuous delivery in our software development workflow. Here, we will create a pipeline job that will internally call the childJob we just created. Let’s look at the Groovy script for the pipeline job:

pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                echo "parentJob"
            }
        }
        stage('triggerChildJob') {
            steps {
                build job: "childJob", wait: true
            }
        }
    }
}

The above script contains two stages, build and triggerChildJob, in the pipeline job. The first stage simply executes the echo command, while the second stage will trigger the childJob internally.

Let’s now create a pipeline job using the above Groovy script:

  • Click on New Item in the Jenkins dashboard

  • Set the “Job Name” as parentJob

  • Select the “Job Type” as Pipeline project

  • Add the Groovy script as explained above and save the job

As a result:

Groovy script

We can now trigger a freestyle job from a Jenkins pipeline job. Note that the parent job build will take place in stages, as shown in the image below:

freestyle job

Here in the above output, we can see both stages build, and triggerChildJob ran successfully.

3. Conclusion

In this article, we demonstrated how to create a job that internally triggers another job in Jenkins. As a first step, we created a sample freestyle Jenkins job and then created a pipeline job to internally trigger it.

We can find the script of the pipeline job over on GitHub.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!