Reputation: 4421
The Jenkins job was launched by hand from the GUI.
The user should be able to add as many text parameters as he wants.
The job will do something (for example saving them to file or concatenate them but doesn't matter what) with these parameters.
I would like to see something like this:
properties([ parameters([
textListParam(name: 'MESSAGE')])])
echo ${params.MESSAGE[0]}
echo ${params.MESSAGE[1]}
Result:
This is my first very long text
This is my second very long text
Is that possible?
Upvotes: 1
Views: 392
Reputation: 6824
There are several ways that you can achieve that, dependent on how you want the user to supply the different parameters, and if you want named parameters or just a list of values.
The first options is to use a simple string
parameter and ask the user to add the parameter values separated by a comma (or any other character) and then in your pipeline code split the string according to your separator and you will get a list of all values. Something like:
pipeline {
agent any
parameters{
string(name: 'Parameters', description: 'Comma separated list of parameters', trim: true)
}
stages {
stage('Parse parameters') {
steps {
script {
def paramList = params.Parameters.tokenize(',')
paramList.each { println it }
// Access parameters with paramList[0], paramList[1]
}
}
}
}
}
A more elegant solution will be to use a text
(multi-line string) parameter, where users can add separated lines in the format of a properties file (propertyName=propertyValue
or propertyName:propertyValue
), you then parse the value in to a dictionary that contains keys that represent the parameter names and values representing the parameters value. Something like:
pipeline {
agent any
parameters{
text(name: 'Parameters', description: 'Each line should contain a parameter in format: propertyName=propertyValue')
}
stages {
stage('Parse parameters') {
steps {
script {
def paramList = readProperties text: params.Parameters
paramList.each { println "Name: ${it.key}, Value: ${it.value}" }
// Access parameters with their name paramList.<param_name>
}
}
}
}
}
** Here i am using readProperties
from the Pipeline Utility Steps Plugin which also adds ability for default values and so, but this logic can also be implemented with plain groovy code.
For the user it will look like:
One last thing, if you need the map (or list) of parameters to be available for several steps you can define it as a global parameter and use it in all steps:
paramList = [:] // Global parameter
pipeline {
agent any
parameters{
text(name: 'Parameters', description: 'Each line should contain a parameter in format: propertyName=propertyValue')
}
stages {
stage('Parse parameters') {
steps {
script {
paramList = readProperties text: params.Parameters
paramList.each { println "Name: ${it.key}, Value: ${it.value}" }
}
}
}
stage('Use parameters') {
steps {
println paramList.Param1
}
}
}
}
Upvotes: 3