Operations 7 min read

Automating Jenkins Job Creation with Job DSL, XML Conversion, and API

This guide explains how to automate Jenkins project creation at scale by using the Job DSL plugin to generate DSL scripts, converting them to XML via a Playground tool, and creating or updating jobs through the Jenkins Core API or Script Console, with full code examples.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Automating Jenkins Job Creation with Job DSL, XML Conversion, and API

Creating projects at scale in Jenkins is a challenge; this article shows how to automate Jenkins project creation using templates.

After installing the Job DSL plugin, you can either create projects directly with DSL or convert DSL to XML and create projects via the Jenkins API. The first method is more straightforward, but this guide focuses on the second method.

1. Generate template using Job DSL API

We need to install the Job DSL plugin first, then create the project using DSL.

URL: https://jenkinsci.github.io/job-dsl-plugin/

For example, using the official example, this defines a pipeline job with description, parameters, and Jenkinsfile location.

pipelineJob("test-schdule-service") {</code><code>  description("this is my first job")</code><code>  keepDependencies(false)</code><code>  parameters {</code><code>    choiceParam("test", [1, 2, 3], "")</code><code>  }</code><code>  definition {</code><code>    cpsScm {</code><code>      scm {</code><code>        git {</code><code>          remote {</code><code>            github("https://gitlab.com/xxx/xxx.git", "https")</code><code>            credentials("24982560-17fc-4589-819b-bc5bea89da77")</code><code>          }</code><code>          branch("*/master")</code><code>        }</code><code>      }</code><code>      scriptPath("Jenkinsfile")</code><code>    }</code><code>  }</code><code>  disabled(false)</code><code>}

2. Convert DSL → XML via Playground

URL: http://job-dsl.herokuapp.com/

3. Create project via Jenkins Core API

import javax.xml.transform.stream.StreamSource</code><code>import jenkins.model.Jenkins</code><code></code><code>//创建项目</code><code>void createOrUpdateJob(String name, String xml) {</code><code>    def j = Jenkins.instance</code><code>    String fullName = name</code><code>    if(name.contains('/')) {</code><code>        j = j.getItemByFullName(name.tokenize('/')[0..-2])</code><code>        name = name.tokenize('/')[ -1]</code><code>    }</code><code>    Jenkins.checkGoodName(name)</code><code>    if(j.getItem(name) == null) {</code><code>        println "Created job \"${fullName}\"."</code><code>        j.createProjectFromXML(name, new ByteArrayInputStream(xml.getBytes()))</code><code>        j.save()</code><code>    } else if(j.getItem(name).configFile.asString().trim() != xml.trim()) {</code><code>        j.getItem(name).updateByXml(new StreamSource(new ByteArrayInputStream(xml.getBytes())))</code><code>        j.getItem(name).save()</code><code>        println "Job \"${fullName}\" already exists.  Updated using XML."</code><code>    } else {</code><code>        println "Nothing changed.  Job \"${fullName}\" already exists."</code><code>    }</code><code>}</code><code></code><code>try {</code><code>    //just by trying to access properties should throw an exception</code><code>    itemName == null</code><code>    xmlData == null</code><code>    isPropertiesSet = true</code><code>} catch(MissingPropertyException e) {</code><code>    println 'ERROR Can\'t create job.'</code><code>    println 'ERROR Missing properties: itemName, xmlData'</code><code>    return</code><code>}</code><code></code><code>String xmlData = """<!-- 1. test-schdule-service -->
<flow-definition>
    <actions></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></description>
                </hudson.model.ChoiceParameterDefinition>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
        <com.coravy.hudson.plugins.github.GithubProjectProperty>
            <projectUrl>https://github.com/https://gitlab.com/xxx/xxx.git/</projectUrl>
        </com.coravy.hudson.plugins.github.GithubProjectProperty>
    </properties>
    <triggers></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://github.com/https://gitlab.com/xxx/xxx.git.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://github.com/https://gitlab.com/xxx/xxx.git/</url>
            </browser>
        </scm>
    </definition>
    <disabled>false</disabled>
</flow-definition>
"""</code><code>String itemName = "my-first-pipeline"</code><code></code><code>createOrUpdateJob(itemName, xmlData)

4. Run via Jenkins Script Console

Creation completed.

---

Follow the link to the latest tutorial for more details.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdAutomationGroovyJenkinsJob DSL
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.