5

I'm converting a Jenkins job from a manual configuration to DSL which means I'm attempting to create a DSL script which creates the job(s) as it is today.

The job is currently parameterized and one of the parameters is of the type "Build Selector for Copy Artifact". I can see in the job XML that it is the copyartifact plugin and specifically I need to use the BuildSelectorParameter.

However the Jenkins DSL API has no guidance on using this plugin to set a parameter - it only has help for using it to create a build step, which is not what I need.

I also can't find anything to do with this under the parameter options in the API.

I want to include something in the DSL seed script which will create a parameter in the generated job matching the one in the image.

parameter

If I need to use the configure block then any tips on that would be welcome to because for a beginner, the documentation on this is pretty useless.

shaneoh
  • 442
  • 2
  • 8
  • 26

2 Answers2

3

I have found no other way to setup the build selector parameter but using the configure block. This is what I used to set it up:

freeStyleJob {
    ...
    configure { project ->
        def paramDefs = project / 'properties' / 'hudson.model.ParametersDefinitionProperty' / 'parameterDefinitions'
        paramDefs << 'hudson.plugins.copyartifact.BuildSelectorParameter'(plugin: "copyartifact@1.38.1") {
            name('BUILD_SELECTOR')
            description('The build number to deploy')
            defaultSelector(class: 'hudson.plugins.copyartifact.SpecificBuildSelector') {
                buildNumber()
            }
        }
    }
}

In order to reach that, I manually created a job with the build selector parameter. And then looked for the job's XML configuration under jenkins to look at the relevant part, in my case:

<project>
    ...
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>
                ...
                <hudson.plugins.copyartifact.BuildSelectorParameter plugin="copyartifact@1.38.1"
                    <name>BUILD_SELECTOR</name>
                    <description></description>
                    <defaultSelector class="hudson.plugins.copyartifact.SpecificBuildSelector">
                        <buildNumber></buildNumber>
                    </defaultSelector>
                </hudson.plugins.copyartifact.BuildSelectorParameter>
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    </properties>
    ...
</project>

To replicate that using the configure clause you need to understand the following things:

  • The first argument to the configure clause is the job node.
  • Using the / operator will return a child of a node with the given node, if it doesn't exist gets created.
  • Using the << operator will append to the left-hand-side operand the node given as the right-hand-side operand.
  • When creating a node, you can give it the attributes in the constructor like: myNodeName(attrributeName: 'attributeValue')
  • You can pass a lambda to the new node and use it to populate its internal structure.
Santiago Alessandri
  • 6,630
  • 30
  • 46
  • Since asking this question I've slowly worked out how the configure block works, but this answer gives me a much better idea of how it comes together - so thanks a lot for this. – shaneoh Nov 29 '16 at 09:04
0

I have Jenkins version 1.6 (with copy artifact plugin) and you can do it in DSL like this:

job('my-job'){
  steps{
        copyArtifacts('job-id') {
            includePatterns('artifact-name')
            buildSelector { latestSuccessful(true) }
        }
    }
}

full example:

job('03-create-hive-table'){
    steps{
        copyArtifacts('seed-job-stash') {
            includePatterns('jenkins-jobs/scripts/landing/hive/landing-table.sql')
            buildSelector { latestSuccessful(true) }
        }
        copyArtifacts('02-prepare-landing-dir') {
            includePatterns('jenkins-jobs/scripts/landing/shell/02-prepare-landing-dir.properties')
            buildSelector { latestSuccessful(true) }
        }
        shell(readFileFromWorkspace('jenkins-jobs/scripts/landing/03-ps-create-hive-table.sh'))
    }
    wrappers {
        environmentVariables {
            env('QUEUE', 'default')
            env('DB_NAME', 'table_name')
            env('VERSION', '20161215')
        }
        credentialsBinding { file('KEYTAB', 'mycred') }
    }
    publishers{ archiveArtifacts('03-create-landing-hive-table.properties') }
}
Babu
  • 4,324
  • 6
  • 41
  • 60
  • Is this to add this as a parameter? Or to copy artifact as a step in the build? – shaneoh Dec 22 '16 at 07:19
  • I have it as a build step, for example my whole job def looks like this (edited in answer) – Babu Dec 22 '16 at 10:09
  • OK - looking at your example though, I can't see how this is creating a parameter for the build. It seems to be creating a Copy Artifact build step instead. – shaneoh Dec 22 '16 at 10:50