Automating Jenkins Job Creation with Job DSL, XML Conversion, and Jenkins Core API
This guide explains how to automate the creation of Jenkins pipeline jobs by generating Job DSL templates, converting them to XML via an online playground, and applying the Jenkins Core API or Script Console to create or update jobs programmatically.
In large‑scale Jenkins environments, manually creating projects becomes cumbersome; this article shows how to automate Jenkins job creation using Job DSL templates, converting DSL to XML, and applying the Jenkins Core API.
Step 1: Generate a Job DSL template – Install the Job DSL plugin , then write DSL scripts to define pipeline jobs. The DSL example below creates a pipeline job named test-schdule-service with description, parameters, and SCM configuration.
pipelineJob("test-schdule-service") {
description("this is my first job")
keepDependencies(false)
parameters {
choiceParam("test", [1, 2, 3], "")
}
definition {
cpsScm {
scm {
git {
remote {
github("https://gitlab.com/xxx/xxx.git", "https")
credentials("24982560-17fc-4589-819b-bc5bea89da77")
}
branch("*/master")
}
}
scriptPath("Jenkinsfile")
}
}
disabled(false)
}Step 2: Convert DSL to XML – Use the online Playground at http://job-dsl.herokuapp.com/ to transform the DSL script into the corresponding Jenkins job XML configuration.
Step 3: Create or update the job via Jenkins Core API – The following Groovy script can be run from the Jenkins Script Console; it reads the generated XML and creates the job if it does not exist or updates it when the configuration has changed.
import javax.xml.transform.stream.StreamSource
import jenkins.model.Jenkins
void createOrUpdateJob(String name, String xml) {
def j = Jenkins.instance
String fullName = name
if (name.contains('/')) {
j = j.getItemByFullName(name.tokenize('/')[0..-2].join('/'))
name = name.tokenize('/')[-1]
}
Jenkins.checkGoodName(name)
if (j.getItem(name) == null) {
println "Created job \"${fullName}\"."
j.createProjectFromXML(name, new ByteArrayInputStream(xml.getBytes()))
j.save()
} else if (j.getItem(name).configFile.asString().trim() != xml.trim()) {
j.getItem(name).updateByXml(new StreamSource(new ByteArrayInputStream(xml.getBytes())))
j.getItem(name).save()
println "Job \"${fullName}\" already exists. Updated using XML."
} else {
println "Nothing changed. Job \"${fullName}\" already exists."
}
}
String xmlData = """
<flow-definition>
<actions/>
<description>this is my first job</description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<hudson.model.ChoiceParameterDefinition>
<choices class='java.util.Arrays$ArrayList'>
<a class='string-array'>
<string>1</string>
<string>2</string>
<string>3</string>
</a>
</choices>
<name>test</name>
<description/>
</hudson.model.ChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
<com.coravy.hudson.plugins.github.GithubProjectProperty>
<projectUrl>https://gitlab.com/xxx/xxx.git</projectUrl>
</com.coravy.hudson.plugins.github.GithubProjectProperty>
</properties>
<triggers/>
<definition class='org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition'>
<scriptPath>Jenkinsfile</scriptPath>
<lightweight>false</lightweight>
<scm class='hudson.plugins.git.GitSCM'>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>https://gitlab.com/xxx/xxx.git</url>
<credentialsId>24982560-17fc-4589-819b-bc5bea89da77</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<configVersion>2</configVersion>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<gitTool>Default</gitTool>
<browser class='hudson.plugins.git.browser.GithubWeb'>
<url>https://gitlab.com/xxx/xxx.git</url>
</browser>
</scm>
</definition>
<disabled>false</disabled>
</flow-definition>
"""
String itemName = "my-first-pipeline"
createOrUpdateJob(itemName, xmlData)Step 4: Run the script in the Jenkins Script Console – Paste the Groovy code into Manage Jenkins → Script Console and execute; the job will be created or updated, as shown in the screenshots.
About the author – Ze Yang is a DevOps practitioner focusing on enterprise‑level DevOps operations and development, sharing practical Linux and DevOps techniques through courses and hands‑on tutorials.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
