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

Article ID:218874337
1 minute readKnowledge base

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:

pipeline template variables
<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('built-in') {
        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 built-in 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
This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.