简体   繁体   中英

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="multiple-scms@0.5">
        <scms>
            <hudson.plugins.git.GitSCM plugin="git@2.4.0"/><!-- ...-->
            <hudson.plugins.git.GitSCM plugin="git@2.4.0"/><!-- ...-->
        </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="globalenvvar@1.0"/><!-- ...-->            
        <hudson.plugins.parameterizedtrigger.BuildTrigger plugin="parameterized-trigger@2.28"/><!-- ...-->
        <hudson.plugins.templateproject.ProxyPublisher plugin="template-project@1.5"/><!-- ...-->
    </publishers>
    <buildWrappers>
        <hudson.plugins.timestamper.TimestamperBuildWrapper plugin="timestamper@1.7.2"/>
    </buildWrappers>
</project>

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), eg "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)
}

I came across the same issue while converting a freestylejob to DSL, I came across a plugin for which I was not able to find the plugin, and so I create this : https://github.com/saurabh-sp-tripathi/xml2groovy-jenkins-DSL-configure-block

This should basically could be a gist but I would let it now what it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM