Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins xml configuration to Groovy-based Jenkins Job DSL

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>
like image 916
Arthur Avatar asked Dec 21 '15 18:12

Arthur


People also ask

How do I get Jenkins job in config XML?

One can view a Jenkins job configuration as XML in the browser by appending config. xml to the end of the URL. For example, if you have the following URL to a Jenkins job https://jenkins.mycompany.com/job/myjob , append config. xml : https://jenkins.mycompany.com/job/myjob/config.xml .

Is Jenkins DSL Groovy?

Jenkinsfiles, using a Domain Specific Language (DSL) based on the Groovy programming language, are persistent files that model delivery pipelines 'as code'.

What is job DSL in Jenkins?

Job DSL was one of the first popular plugins for Jenkins which allows managing configuration as code and many other plugins dealing with this aspect have been created since then, most notably the Jenkins Pipeline and Configuration as Code plugins.


1 Answers

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)
}
like image 149
Steve Campbell Avatar answered Nov 15 '22 10:11

Steve Campbell