Personal Programming Notes

To err is human; to debug, divine.

Job DSL Plugin in Jenkins

How Job DSL plugin works and why it is losing its dominance to Jenkins Pipeline.

Job DSL

Minimal Job DSL
1
2
3
4
5
6
7
8
9
job('Demo') {
    description("Starting pipeline")
    logRotator {
        daysToKeep(15)
    }
    steps {
        shell('echo "Hello World"')
    }
}

To illustrate the idea. Obviously, lots of work need to be done to make sure the final generated config.xml is valid, including escaping special characters.

Simple implementation of Job DSL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class GroovyDsl {

    def description(String description) {
        print '<description>'
        print description
        println '</description>'
    }

    def logRotator(Closure inner) {
        println '<logRotator>'
        inner()
        println '</logRotator>'
    }

    def steps(Closure inner) {
        println '<builders>'
        inner()
        println '</builders>'
    }

    static void job(String name, Closure closure) {
        GroovyDsl body = new GroovyDsl()

        println "Generating a Freestyle job $name"
        println "Save the following into config.xml file"
        println '<project>'
        body.with(closure)
        println '</project>'
    }

}

def daysToKeep(int num) {
    println "<daysToKeep>$num</daysToKeep>"
    println '<numToKeep>-1</numToKeep>'
    println '<artifactDaysToKeep>-1</artifactDaysToKeep>'
    println '<artifactNumToKeep>-1</artifactNumToKeep>'
}

def shell(String cmd) {
    println '<hudson.tasks.Shell>'
    println '<command>'
    println cmd
    println '</command>'
    println '</hudson.tasks.Shell>'
}

GroovyDsl.job('Demo') {
    description("Starting pipeline")
    logRotator {
        daysToKeep(15)
    }
    steps {
        shell('echo "Hello World"')
    }
}
Output of the above script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Generating a Freestyle job Demo
Save the following into config.xml file
<project>
<description>Starting pipeline</description>
<logRotator>
<daysToKeep>15</daysToKeep>
<numToKeep>-1</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</logRotator>
<builders>
<hudson.tasks.Shell>
<command>
echo "Hello World"
</command>
</hudson.tasks.Shell>
</builders>
</project>

Compared with the Jenkins config.xml of the Freestyle job generated by the same Job DSL, we can see that there is not much different.

Output of config.xml in Jenkins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<project>
<actions/>
<description>Starting pipeline</description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>echo "Hello World"</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
<logRotator>
<daysToKeep>15</daysToKeep>
<numToKeep>-1</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</logRotator>
</project>