Jenkins xml configuration to Groovy-based Jenkins Job DSL

Arthur picture Arthur · Dec 21, 2015 · Viewed 8.5k times · Source

Could somebody give me a useful link, where I can find information about converting the complex xml configuration for Jenkins jobs?

Here is a Jenkins job example:

<project>
    <actions/>
    <description>Description</description>
    <logRotator class="hudson.tasks.LogRotator">
        <!-- ...-->
    </logRotator>
    <keepDependencies>false</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty/><!-- ...-->
    </properties>
    <scm class="org.jenkinsci.plugins.multiplescms.MultiSCM" plugin="[email protected]">
        <scms>
            <hudson.plugins.git.GitSCM plugin="[email protected]"/><!-- ...-->
            <hudson.plugins.git.GitSCM plugin="[email protected]"/><!-- ...-->
        </scms>
    </scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <jdk>Default</jdk>
    <triggers>
        <hudson.triggers.TimerTrigger/><!-- ...-->
    </triggers>
    <concurrentBuild>false</concurrentBuild>
    <customWorkspace>$HUDSON_WD/$REVISION/checkout</customWorkspace>
    <builders/>
    <publishers>
        <hudson.plugins.globalenvvar.GlobalEnvironmentVariablePublisher plugin="[email protected]"/><!-- ...-->            
        <hudson.plugins.parameterizedtrigger.BuildTrigger plugin="[email protected]"/><!-- ...-->
        <hudson.plugins.templateproject.ProxyPublisher plugin="[email protected]"/><!-- ...-->
    </publishers>
    <buildWrappers>
        <hudson.plugins.timestamper.TimestamperBuildWrapper plugin="[email protected]"/>
    </buildWrappers>
</project>

Answer

Steve Campbell picture Steve Campbell · Dec 21, 2015

In my experience, it is an entirely manual process of re-writing. Reference material is at https://jenkinsci.github.io/job-dsl-plugin/#.

Many elements in the xml are defaulted, so much of the xml can be skipped. It is only necessary to convert the xml element-by-element if the DSL does not directly support the plugin or feature of the plugin that you have configured.

Conversion process is as follows:

  1. Go through each configured property (via the Jenkins GUI), e.g. "Discard old builds".
  2. Determine if the DSL has native support for that element. If so, rewrite it in the DSL. For example, logRotator provides the "Discard old builds" functionality.
  3. If not directly supported by the DSL, you have to manually use configure to output the xml. This is quite tricky and to be avoided if at all possible.

If you're not sure which plugin is providing the job element, you can often see the plugin name in the help-text for that element (click on the little question-mark icon). Otherwise, the xml element often contains the plugin name.

Also good to know is that the job elements are broken out in the same way in the DSL as they are on the Configure screen in Jenkins. So if it is a Trigger, then you can find it in the DSL under triggers.

Simple example (I know, yours is much more complex):

freeStyleJob("Arthur's Example") {
  description('Description')
  logRotator(30)
}