Let users specify any kind of parameters when creating a job from a pipeline template and handle them in the job created

Issue

You want to let users specify any kind of parameters when creating a job from a pipeline template and handle them in the pipeline.

Environment

  • CloudBees Jenkins Enterprise
  • CloudBees Template plugin
  • Pipeline plugin

Resolution

Flow Definition

You need to create a pipeline template with the following characteristics:

<flow-definition plugin="workflow-job@1.14">
<actions/>
<description/>
<keepDependencies>false</keepDependencies>
<properties>
    <% if (Variables) { %>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>                           
                ${serializeAll(Variables)}
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    <% } %>
</properties>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@1.14">
<sandbox>false</sandbox>
</definition>
<triggers/>
</flow-definition><flow-definition plugin="workflow-job@1.14">
<actions/>
<description/>
<keepDependencies>false</keepDependencies>
<properties>
    <% if (Variables) { %>
        <hudson.model.ParametersDefinitionProperty>
            <parameterDefinitions>                           
                ${serializeAll(Variables)}
            </parameterDefinitions>
        </hudson.model.ParametersDefinitionProperty>
    <% } %>
</properties>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@1.14">
<sandbox>false</sandbox>
</definition>
<triggers/>
</flow-definition>

## Script

import hudson.model.ParameterValue
import hudson.model.ParametersAction;

node('master') {
        def myparams = currentBuild.rawBuild.getAction(ParametersAction)
        for(ParameterValue p in myparams.parameters) {
            println p.name
            println p.value
        }
}

This produces the output:

Started by user anonymous
[Pipeline] Allocate node : Start
Running on master in /Users/fbelzunc/cloudbees/support/support-shinobi-tools/cases/34880/5-cloudbees-support_2016-04-01_19.17.49/jenkins-home/jobs/felix/workspace
[Pipeline] node {
[Pipeline] echo
parameter
[Pipeline] echo
DANIEL
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: SUCCESS
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.