shaneoh shaneoh - 1 month ago 27
Groovy Question

Add parameter "Build Selector for Copy Artifact" using Jenkins DSL

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.


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.


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") {
            description('The build number to deploy')
            defaultSelector(class: 'hudson.plugins.copyartifact.SpecificBuildSelector') {

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:

                <hudson.plugins.copyartifact.BuildSelectorParameter plugin="copyartifact@1.38.1"
                    <defaultSelector class="hudson.plugins.copyartifact.SpecificBuildSelector">

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.