Add "Build selector to copy artifact" option using Jenkins DSL

I am converting a Jenkins job from manual configuration to DSL, which means I'm trying to create a DSL script that creates the job, as it is today.

Currently, the task is specified by a parameter, and one of the parameters is of the Build Selector for Copy Artifact type. I can see in the XML workplace that this is a copyartifact plugin and, in particular, I need to use BuildSelectorParameter.

However, the Jenkins DSL API does not have instructions on using this plugin to set a parameter - it only has help in using it to create a build step that is not what I need.

I also cannot find anything to do with this parameter in the API.

I want to include something in the DSL semester of a script that will create a parameter in the generated job that matches what is in the image.

parameter

If I need to use the configure block, any advice on this subject would be welcome, because for beginners the documentation on this subject is useless.

+5
source share
2 answers

I did not find another way to configure the assembly selector parameter, but using the configure block. This is what I used to configure it:

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() } } } } 

To achieve this, I manually created a task with an assembly selector parameter. And then I looked for the XML configuration to work under jenkins to look at the corresponding 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 this 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 returns the child element of the node with the given node, if it does not exist, is created.
  • Using the << operator will add the node character specified as the right operand to the left side operand.
  • When creating a node, you can assign attributes to it in the constructor, for example: myNodeName(attrributeName: 'attributeValue')
  • You can pass lambda to a new node and use it to populate your internal structure.
+3
source

I have version Jenkins 1.6 (with a plugin for copying an artifact), and you can do it in DSL as follows:

 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') } } 
0
source

Source: https://habr.com/ru/post/1260285/


All Articles