Jenkins job as code

Create Jenkins seedjob using Job DSL Script

Sourav Atta
5 min readMay 17, 2021

In organizations, where the number of Jenkins jobs keeps on increasing day by day, maintaining and creating these Jenkins jobs becomes tedious. We need to copy jobs to create new ones but many times, these jobs differ from the original template and thus it becomes very difficult to maintain consistency between these jobs.

Jenkins provides an excellent plugin Job DSL which helps to solve the above problem. It provides the benefit of creating jobs in a programmatic way i.e. Jenkins job as code.

What is Seedjob ?

A seedjob is a Jenkins job that uses Job DSL script and builds additional jobs. It is a standard freestyle job that includes all the functionalities of a Jenkins job.

Create SeedJob using Job DSL

To create seedjob, we need to follow the below steps:

Step 1: Installing Job DSL Plugin

  1. Navigate to Manage Jenkins → Manage Plugins.
  2. Click on Available and in the Search, type ‘Job DSL’
  3. Check the checkbox next to ‘Job DSL’ and then click on Install without restart.
Job DSL Plugin

You are redirected to a new page, where you can check the status of the installation of the plugin. Wait till the Success of both Job DSL and Loading plugin extensions.

You’ve successfully installed the Job DSL Plugin and ready to use Job DSL to create seedjobs.

Step 2: Creating a SeedJob

We will create a seedjob that will create a pipeline job that prints a greeting message based on the input name. Ex. “Hello!! Sourav”

Job DSL Plugin provides a number of API methods that can be used to configure different aspects of a job like build triggers, build parameters, setting git repository, etc. You can access all these API Methods from the Jenkins Job DSL Plugin page.

Jenkins Job DSL API

Before creating the job, let's create the DSL Script required to create the pipeline job the prints greeting message.
Referring to the Job DSL API methods, to create the pipeline job we need the following API methods:

  • pipelineJob(String name) : create or updates a pipeline job
  • parameters {} : allows to parameterize the job
  • stringParam(String parameterName, String defaultValue = null, String description = null) : defines a simple text parameter, where users can enter a string value
  • definition {} : adds a workflow definition
  • cps {} : defines a Groovy CPS DSL definition
  • script(String script) : sets the workflow DSL script. Can be used to write the pipeline script or include the Jenfinsfile

Putting these all methods together, the DSL script will be:

pipelineJob("greetingJob") {

parameters {
stringParam('name', "", 'name of the person')
}
definition {
cps {
script('''
pipeline {
agent any
stages {
stage('Greet') {
steps {
echo "Hello!! ${name}"
}
}
}
}
'''.stripIndent())
sandbox()
}
}
}

Now, let’s create a seedjob that will generate a new pipeline job based on the above DSL script.

  • On Jenkins Dashboard, click on New Item. Type seedJob in Enter an item name textbox and select Freestyle project. Click on OK.
  • In the follow-up screen, scroll down to the section Build and click on Add build step. From the dropdown list, select Process job dsl
  • Click on the radio button Use the provided DSL script and paste the above DSL script in the text area.
  • Click on Apply and Save. Now, the seedjob has been successfully created.

Step 3: Running the SeedJob

Now, we will run the seejob created in the above step which will generate a job named greetingJob

  • On the Jenkins Dashboard, select the job seedJob and then click on Build now button on the left side.
  • You can see the job runs successfully and on refreshing the page, there is a new section Generated items, that will list the greetingJob that you have mentioned in the DSL script.
  • Click on the greetingJob under the Generated items, you’ll be redirected to the greetingJob job page.
  • Click on Build with Parameters on the left hand side of the page and type any name in the text area. Ex. Sourav
  • Click on Build button and your pipeline will run successfully and it should print Hello!! Sourav
  • A build has been listed under the Build history. Hover over the #1 build, click on the drop-down arrow, and select Console Output.

You can now view the logs and console output of the build. You can verify the output of the echo command.

Conclusion

This tutorial focuses on Jenkins job as code and we learn to create seedjob using the Jenkins Job DSL Plugin. Try it out by creating a Pipeline Job (as above) or a Freestyle Job with some more complex build steps.

Don’t forget to refer the Job DSL API methods which can also be accessible from your Jenkins installation at http://your-jenkins-installation/plugin/job-dsl/api-viewer/index.html

Let me know in the comments if you are facing any problem with the above example or need help with the DSL script.

--

--

Sourav Atta

DevOps Engineer | Tool Agnostic | Very much into learning old-school IT concepts | Strategically lazy