Dynamic Parameter in Jenkinsfile? - jenkins

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.

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}"

post build action with parameters

I have 2 parameterized pipeline A and B.
project A execute project B as a post build action.
Im using "Predefine parameters" to pass parameters to project B, but seems project B using default values and not the provided one.
the pass parameter is project A parameter.
Jenkins can get weird around parameters. If you are using a declarative pipeline, then define the parameters within the code instead of using the option on the Jenkins page:
build([
job : 'B',
wait : false,
parameters: [
string(name: 'process_id', value: "${id}")
]
])
And in pipeline B:
parameters {
string(defaultValue: null, description: 'parameter', name: 'process_id')
}
If using freestyle jobs, the way you have defined the parameter is correct. If Jenkins is not using the correct parameter and instead is using some cached value, then try these steps:
Clone your downstream job
Rename you downstream job to something else
Rename the cloned downstream job to the correct name that should be used
Run the downstream job once
If the problem is that Jenkins caches the parameters used, this should fix it.

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.

Using a Jenkins pipeline multiline /multi-line string parameter

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: '') }

Jenkins plugin for collecting user input

I am creating a Jenkins Pipeline job.
I want to achieve this: in the job home page, I want an HTML input tag, before each time manually triggering the build, I first fill in something in the tag, then the value can be retrieved and used in the pipeline script during the build.
It there a plugin for this purpose?
Thanks.
This is a so-called Parameterized Build.
In your pipeline definition, you can add these build parameters using the properties step, which comes with the workflow-multibranch plugin.
A simple example would be as follows:
properties([
parameters([
string(name: 'DEPLOY_ENV', defaultValue: 'TESTING', description: 'The target environment', )
])
])
P.S: As this feature is quite hidden, I wrote a blog post about this a few weeks ago.

Resources