Jenkins Job Management via Job DSL Pipeline

Tossaporn Jiw
2 min readSep 16, 2021

Manual editing each Jenkins job in accordance with changes from the code repository can be very painful for CI/CD processes. Here we will explore how to make it more seamless with Job DSL, a plug-in for Jenkins to enable scripted pipeline deployment.

First off, let’s install a plug-in Job DSL so we can run a pipeline on the master node as admin, which is mandatory for the script approval. Always restart your Jenkins before moving on to the next step for the best experience.

Once restarted, begin by creating a pipeline job named pipeline-deployment (literally pipeline deployment). Now we are going to write Jenkinsfile which then calls Job DSL script.

Jenkinsfile stages should look similar to the sample below;

stages {
stage('Import Job DSL') {
options { timeout (time: 1, unit: 'MINUTES') }
steps {
checkout scm // Import your Job DSL script i.e. yourJobDSL
}
}
stage('Run Job DSL') {
options { timeout (time: 1, unit: 'MINUTES') }
steps {
script {
def inputFile = readFile 'yourJobDSL' // Parse Job DSL
jobDsl scriptText: inputFile // Run Job DSL
}
}
}
}

For Job DSL script, here is the sample script;

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
def targetJobs = ['job-1', 'job-2', 'job-3']def template = jenkins.getItemByFullName('job-template')targetJobs.each { j ->
jenkins.copy(template, j)
println "Created ${j}"
}

where job-template is the common/standardized pipeline that contains fundamental configuration for the other pipelines

After running deployment-pipeline (after script approval), you should see 3 new jobs appearing in the home directory.

Here we demonstrate how to seamlessly create multiple jobs from Job DSL as a configuration file.

--

--