Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 432
shaneoh Avatar asked Oct 18 '22 21:10

shaneoh


1 Answers

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: "[email protected]") {
            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="[email protected]"
                    <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.
like image 115
Santiago Alessandri Avatar answered Oct 21 '22 04:10

Santiago Alessandri