Using a Jenkins pipeline multiline /multi-line string parameter - jenkins

I'm converting a bunch of jobs to use the Jenkins pipeline language.
In plain/normal Jenkins we can use parameter types including:
string
boolean
choice, and also
multi-line string.
A parameter using one of these types will pop up and prompt the user for input when they run the Jenkins job.
The available parameter types for Jenkins pipeline are: (According to here).
booleanParam
choice
file
text
password
run
string
There is no multiline string input parameter listed for pipeline. The Jenkins documentation say the documentation is "young" and still incomplete.
Has anyone managed to get a multi-line string input parameter working with the Jenkins pipeline?
Multi-line string parameters are in the out-of-the-box Jenkins package, but doesn't seem to be there in the pipeline.

I discovered the solution by looking at the Jenkins source code:
parameters{ text(name: 'mytextparam',
defaultValue: 'Default lines for the parameter',
description: 'A description of this param')
}
This pops up a multi-line text input prompt which becomes the parameter value which you can refer to later as params.mytextparam
This is not documented in the Jenkins Pipeline documentation, so there might be issues like it being unsupported or withdrawn in a future release. Or it could go the other way and they might document it in the next release.

There is a plugin that called:
"pipeline-syntax"
You should use it if you need some stuff for Jenkins pipeline.
1.Install the plugin: I installed it from "plugins" in my Jenkins server under "Manage Jenkins" then, Plugin Manager. source:https://wiki.jenkins.io/display/JENKINS/Pipeline+Plugin
2.You will see the plugin in the main screen of Jenkins on the left side. select it.
3.On the plugin select whatever you want for the plugin. here is an example of what you need now, check this out:
Go To Pipeline syntax dialog (/pipeline-syntax)
Select properties: Set Job Properties
6.After that press on "GENERATE PIPELINE SCRIPT".
7.copy the code (or part of it that you need) and add it to your Jenkins pipeline..
Let me know if this was helpful

Multi-line string parameters are text parameters in pipeline syntax. They are described on the Jenkins Pipeline Syntax page.
Example:
parameters { text(name: 'DEPLOY_TEXT', defaultValue: 'One\nTwo\nThree\n', description: '') }

Related

Parameterized pipeline with choice has trailing comma

I have a jenkins pipeline which uses the defualt choice parameter.
parameters {
choice(choices: ['optA', 'optB'], description: 'Some options', name: 'CHOSEN')
}
Whenever it run the chosen options will always have a comma appended to it. Resulting in the step it's used in taking it as part of the argument.
someprogram.sh optA,
This breaks everything. How do I disable the comma? Also why is this default behavior?
I tried removing it with a combination of shell substition and jenkins groovy string manipulation.
I searched for the option 'omit field value' which 'fixes' this for a choices plugin (I am not using the reactive choices plugin but this is the only response I could find) When I try to find the 'omit field value' box in pipeline settings in parameters it doesn't exists.
Try replacing it like below.
sh "someprogram.sh ${params.CHOSEN}"

Dynamically evaluate default in Jenkins pipeline build parameter

In Jenkins declarative pipeline we can define build parameters like
pipeline {
…
parameters {
string(name: 'PARAMETER', defaultValue: 'INITIAL_DEFAULT')
choice(name: 'CHOICE', choices: ['THIS', 'THAT'])
}
…
}
However the parameter definitions of the job are only updated when the job runs after the build parameters dialog was already shown. That is, when I change the INITIAL_DEFAULT to something else, the next build will still default to INITIAL_DEFAULT and only the one after that will use the new value.
The same problem is with the choices, and there it is even more serious, because string default can be overwritten easily when starting the build, but if the new option isn't there, it cannot be selected at all.
So is there a way to define functions or expressions that will be executed before the parameter dialog to calculate current values (from files, variable in global settings or any other suitable external configuration)?
I remember using some plugins for this in the past with free-style jobs, but searching the plugin repository I can't find any that would mention how to use it with pipelines.
I don't care too much that the same problem applies to adding and removing parameters, because that occurs rarely. But we have some parameters where the default changes often and we need the next nightly to pick up the updated value.
It turns out the extended-choice-parameter does work with pipeline, and the configurations can be generated by the directive generator. It looks something like
extendedChoice(
name: 'PARAMETER',
type: 'PT_TEXTBOX',
defaultPropertyFile: '/var/lib/jenkins/something.properties',
defaultPropertyKey: 'parameter'
)
(there are many more options available in the generator)
Groovy script to get global environment variables can be had from this other answer.

Adding custom job property to Jenkins job

I want to add a new mandatory job property to capture the some custom fields in the jenkins job. I searched in the plugins list but couldn't find any relevant plugin that solves the issue. Is there any plugin to solve this ? (Note: Extra columns plugin doesn't solve my usecase)
A freestyle job can be configured to build with parameters. See: https://wiki.jenkins.io/display/JENKINS/Parameterized+Build
You can configure the parameter type (string, boolean, drop down etc), give a description of the parameter and a default value. The string parameters can include validation rules:
https://wiki.jenkins.io/display/JENKINS/Validating+String+Parameter+Plugin
Though this only warns when the current parameter value does not meet the regex validation rule, it doesn't prevent the build from being submitted. If submitted in this state, however, the build will fail.
From a quick google, it appears this doesn't work for pipeline jobs, See the last comment on the plugin page url above from Miguelángel Fernández:
If you look at the implementation of class ValidatingStringParameterValue you'll see that it overrides the implementation of public BuildWrapper createBuildWrapper(AbstractBuild build) in a way that aborts if the string is invalid. This will only work on Freestyle jobs and other job types extending AbstractBuild. I'm afraid this does not apply to pipeline jobs. Maybe in your prior project you used freestyle jobs.
An alternative for freestyle jobs is to do in job validation before initiating any build steps using the 'Prepare an environment for the run' from:
https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin
You would need to write groovy to check the parameters submitted and abort the build at this point if the values aren't suitable. Something like:
def validateString = binding.variables.get('testParam')
if (!binding.variables.get('testParam').matches('\\d+')) {
println "failure of parameter validation - does not match regex"
throw new InterruptedException()
} else {
println "Validation passed carry on with build"
}
This doesn't work on pipeline builds - as the plugin is quote:
'This plugin has some known limitations. For Example, Pipeline Plugin is not fully supported.'.
But if you are using scripted pipelines you can implement something similar:
stage 'start up'
if(!env.testParam.matches('\\d+')) {
error 'failure of parameter validation - does not match regex'
}

Jenkins Addon in Jenkins Pipeline

I have a parameterized project. With the variable VAR1.
I'm using the the Xray for JIRA Jenkins Plugin for Jenkins. There you can fill four parameters:
JIRA Instance
Issues
Filter
File Path
I'm new to Jenkins but what I have learned so far, that you can't fill this fields with environment variables. Something like
Issues: ${VAR1} - doesn't work.
So I thought I can do this with a pipeline. When I click on Pipeline Syntax and chose step: General Build Step I can choose Xray: Cucumber Features Export Task. Then I fill the fields with my environment variable and click Generate Pipeline Script The output is as follows:
step <object of type com.xpandit.plugins.xrayjenkins.task.XrayExportBuilder>
That doesn't work. What I'm doing wrong?
All you're doing is OK, but what you want is not supported by Jenkins whether it is pipeline or not, since the parameters' load is happening prior to the pipeline-flow or the definition of the ${VAR1}.
You can try to overcome this by defining the 'Issues' value as a pipeline internal value instead of a parameter and base it on the ${VAR1} value.
If it must be a parameter, use 2 jobs where one defines the value of 'Issues' based on a the ${VAR1} and pass it to the other job that gets the 'Issues' as a fixed value.

Dynamic Parameter in Jenkinsfile?

How can I use the Jenkins Dynamic Plugin in a Jenkinsfile?
What I am looking for is a Jenkinsfile snippet that:
Enables the Build with Parameters option in the Jenkins job
When selected, a script that populates a list that can be used Dynamic Choice Parameters is populated and the user will see a drop down list.
When trying:
Pipeline syntax in the Jenkins editor
Selecting properties: Set job properties as Sample step
Selecting This project is parameterized
Using Dynamic Choice Parameter
Enter values for Name, Choice Script, Remote Script etc
Generate Pipeline Script
I get the following template:
properties([
parameters([
<object of type com.seitenbau.jenkins.plugins.dynamicparameter.ChoiceParameterDefinition>
]),
pipelineTriggers([])
])
i.e. the generated pipeline script does not contain the data that I have entered in step 5. above. How can I modify parameters so that parameter name, choices, etc will be visible to the user?
Jenkins version: 2.19.3
Dynamic Parameter Plugin version: 0.2.0
there is no need anymore for the Jenkins Dynamic Plugin anymore. Just use the normal choice or string parameter and have the value(s) updated by groovy code.
#!/bin/groovy
def envs = loadEnvs();
properties([
parameters([
choice(choices: envs, description: 'Please select an environment', name: 'Env')
])
])
node {
try {
stage('Preparation'){
...
If you use the choice parameter be aware the you must provide a string where the values are separated by a new line.
For example:
"a\nb\nc"
If you really need to plugin, then vote on this issue JENKINS-42149.

Resources