Dynamic parameter on Jenkins Pipeline depending on branch - jenkins

I have something like this on my jenkins pipeline
properties([
parameters([
booleanParam(description: 'Merge master to this branch', name: 'merge_master', defaultValue: false),
someOtherParameters
])
])
Obviously the first parameter that doesn't make sense if the pipeline is running on master branch. So, how can I have this parameter only if the pipeline is not running on master branch?

If you haven't found a way yet, you could just add the elements to the parameters list conditionally like this
def list = []
if (env.BRANCH_NAME != 'master') {
list.add(booleanParam(description: 'Merge master to this branch', name: 'merge_master', defaultValue: false))
}
//example list.add(otherParams)
//finally
properties([parameters(list)])
More on adding to lists in groovy can be found here.

I was able to use hakamari's example as long as I only had items that had classes that could be found like string and boolean. Since I'm also using (CascadeChoiceParameter), and others, I got the same array error, and I had to convert all to the $class: 'org.biouno.unochoice.CascadeChoiceParameter' syntax to get it to work properly. I'm not sure why, but it sure was frustrating to figure that out.
newParameters.add([
$class: 'hudson.model.ChoiceParameterDefinition',
name: 'AWSenvironment',
choices: ['Development', 'Provision'],
description: 'where to deploy, most of the time will be Development'
])
newParameters.add([
$class: 'hudson.plugins.validating_string_parameter.ValidatingStringParameterDefinition',
name: 'HostName',
defaultValue: 'AutoBuild',
description: 'What hostname would you like?<br/><i>Your last name will be prefixed to this name</i>',
regex: /^[a-zA-Z0-9.:-]+$/,
failedValidationMessage: "Regular alphanumerics, periods, colons, and hyphens only!",
])

Related

How to conditionally hide parameter?

I am trying to create a jenkins pipeline job with parameters. I want the parameters to show up conditionally. The condition depends on a selection of a previous parameter.
I have tried the Active choices plug-in. It allows me to choose a value of a parameter conditionally. I want the whole parameter to appear in the UI conditionally.
Is it possible with jenkins pipeline files?
I do not believe this is possible. In the case of declarative/scripted pipelines, parameters are 'post-processed' meaning essentially the ones you see are what was evaluated in the previous 'run/build'. Which is why it takes a build before 'Build with Parameters' becomes available.
As an alternative, (if you're using scripted/declarative pipelines) you could use an Input step and make it trigger conditionally.
if ( x == true ) {
def userInput = input(
id: 'userInput', message: 'Let\'s promote?', parameters: [
[$class: 'TextParameterDefinition', defaultValue: 'uat', description: 'Environment', name: 'env'],
[$class: 'TextParameterDefinition', defaultValue: 'uat1', description: 'Target', name: 'target']
])
}
Example pulled from:
https://support.cloudbees.com/hc/en-us/articles/204986450-Pipeline-How-to-manage-user-inputs

Parameterized Jenkins Pipeline: Choices not showing up

I am trying to setup my pipeline as a Parameterized Pipeline using Single_select choice parameters.
My pipeline header looks as follows:
properties(
[
parameters([
[
$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select your testcase',
filterable: false,
name: 'testCases',
choices: ['HappyFlow', 'NewYork_HappyFlow']
]
]
),
pipelineTriggers([])
]
)
What happens when I am running my pipeline is the following:
Jenkins leaves the dropdown empty instead of giving me the options I specified in my pipeline properties
How would I get the dropdown to be filled with the parameters from my pipeline properties?
This worked for me:
parameters([choice(choices:['HappyFlow', 'NewYork_HappyFlow'], description: 'Select your testcase', name: 'testCases')
])
It sounds like you might be affected by JENKINS-26143: Workflow Snippet Generator - Incorrect format for Input with Choice Parameter. I think a fix is out for it in Jenkins 2.112 based on the comments on the issue, but for now, you can change choices from:
choices: ['HappyFlow', 'NewYork_HappyFlow']
to
choices: 'HappyFlow\nNewYork_HappyFlow'

How to access all nodes from NodeParameterDefinition in jenkins pipeline?

I'm writing a Jenkinsfile that use the NodeLabel Parameter Plugin for jenkins. Here I use the NodeParameterDefinition to give the user the ability to select nodes where the build should happen. I have enabled allowMultiSelectionForConcurrentBuilds, but I only still get a string with only one node name when accessing the parameter value in the Jenkinsfile. The parameter value type is also a string, how can I get all the nodes the user selected for the parameter?
paramter definition:
[
$class: 'NodeParameterDefinition',
allowedSlaves: ['ALL (no restriction)'],
defaultSlaves: ['master'],
description: 'What nodes to run the build on.',
name: 'BUILD_NODE',
nodeEligibility: [$class: 'AllNodeEligibility'],
triggerIfResult: 'allowMultiSelectionForConcurrentBuilds'
]
So if I select multiple nodes when executing, I still only get one node name when accessing this parameter value.
echo "Will build on $BUILD_NODE";
Is multi node selection was enabled not possible with pipeline scripts?
How I access the parameter value:
echo "Will build on $BUILD_NODE";
node("$BUILD_NODE")
{
...
}
NodeLabel Parameter Plugin doesn't work smoothly with Pipeline and Blue Ocean, just as it is not updated frequently (see the revision history). Jenkins Plugins must follow requirements in order to be compatible with Pipeline.
Unfortunately the issue is still unresolved (unknown when it will be resolved):
https://issues.jenkins-ci.org/browse/JENKINS-43720
The problem is that I can not use env.NODE_PARAM or NODE_PARAM to get
multiple selection of nodes, as those are only a string representation
of a single node.
You can vote for this jira-task JENKINS-43720 (click "Vote for this issue"), or participate in the plugin development.
So far I found my clumsy way to imitate the plugin behavior by using another parameter option choice (but this works in Blue Ocean!):
properties([
parameters([
choice(choices: ["none", "node_1", "node_2"], description: "", name: "NODE_1"),
choice(choices: ["none", "node_1", "node_2"], description: "", name: "NODE_2")
])
])
// here you can write your behaviour
// e.g. validation of params, e.g. if 'none' is selected, then use the default node_X
node(env.NODE_1) { }
node(env.NODE_2) { }
or you can use the option string:
properties([
parameters([
string(defaultValue: "node_1, node_2", description: "", name: "NODE", trim: false)
])
])
// parse here the param env.NODE

How can I use the Extended Choice Parameter plugin in a Jenkins pipeline script?

The Extended Choice Parameter plugin is great and I use it in jobs configured via the UI https://wiki.jenkins-ci.org/display/JENKINS/Extended+Choice+Parameter+plugin
However, I'm struggling to get it working in a Jenkinsfile style pipeline script.
It would appear that the Extended Choice Parameter plugin isn't yet fully compatible with Pipeline scripts since Jenkins pipeline-syntax generator creates the following snippet:
parameters([<object of type com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition>])
If I create the parameters manually I get the same behavior as mentioned in
https://issues.jenkins-ci.org/browse/JENKINS-32188
org.kohsuke.stapler.NoStaplerConstructorException: There's no #DataBoundConstructor on any constructor of class
Does anyone know of any workarounds that can get around the issue of ExtendedChoiceParameterDefinition not using #DataBoundConstructor?
Jenkins 2.19.2
Extended Choice Parameter plugin 0.75
Since April's 2nd, 2019 it's now possible because of this commit: https://github.com/jenkinsci/extended-choice-parameter-plugin/pull/25
You can use it like this for instance:
properties([
parameters([
extendedChoice(
name: 'PROJECT',
defaultValue: '',
description: 'Sélectionnez le projet à construire.',
type: 'PT_SINGLE_SELECT',
groovyScript: valueKeysScript,
descriptionGroovyScript: valueNamesScript
)
])
])
If you want to know every possible parameter you have to refer to the source code.
If you want to know every possible value for the "type" key, have a look at the PT_* constants.
Here is my workaround for this pb:
https://gist.github.com/jgraglia/44a7443847cff6f0d87387a46c7bb82f
ie : manually instanciate the parameter by declaring all the args
I was able to add a multi checklist parameter to my pipeline with that.
Navigate to your http://jenkins-url.com/pipeline-syntax.
On the Sample step dropdown select 'Properties: Set job properties'
There is a checkbox for 'This project is parameterized', then you can select Add parameter > Extended Choice Parameter. Add the menu items there then click 'Generate Pipeline Script' to convert.
Trim it so you remove the 'properties([parameters([' before and the '])])' after:
extendedChoice(defaultValue: 'whatif', description: 'Run as what if?', multiSelectDelimiter: ',', name: 'whatif', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', value: 'whatif, LIVE', visibleItemCount: 2)
Like mkobit said it is currently not possible to use the extended choice plugin as a build parameter.
What I like to use as a workaround is a construct like the following
timeout(time: 5, unit: TimeUnit.MINUTES) {
def result = input(message: 'Set some values', parameters: [
booleanParam(defaultValue: true, description: '', name: 'SomeBoolean'),
choice(choices: "Choice One\nChoice Two", description: '', name: 'SomeChoice'),
stringParam(defaultValue: "Text", description: '', name: 'SomeText')
]) as Map<String, String>
}
echo "${result.SomeBoolean}, ${result.SomeChoice}, ${result.SomeText}"
And call it in the beginning of my pipeline. You then get asked for these inputs shortly after your build starts.
Works for me :
I needed to retrieve all the versions number of artifacts from a Nexus Repo:
properties ([
parameters([
choice(choices: ['PROD', 'DEV', 'QA'], description: '', name: 'ParamEnv' ),
string(name: 'ParamVersion', defaultValue: '', description: 'Version to deploy'),
extendedChoice(
name: 'someName',
description: '',
visibleItemCount: 50,
multiSelectDelimiter: ',',
type: 'PT_SINGLE_SELECT',
groovyScript: '''
import groovy.json.JsonSlurper
List<String> nexusPkgV = new ArrayList<String>()
def pkgObject = ["curl", "https://xxxx:xxxx#xxxxxxxxxx"].execute().text
def jsonSlurper = new JsonSlurper()
def artifactsJsonObject = jsonSlurper.parseText(pkgObject)
def dataA = artifactsJsonObject.items
for (i in dataA) {
nexusPkgV.add(i.version)
}
return nexusPkgV
'''
)
])
])
Use the below code to build multichoice checkbox parameter:
parameters {
extendedChoice description: '', multiSelectDelimiter: ',', name: 'a', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'a,b,c', visibleItemCount: 3
}
It looks like in Jenkins UI:
Use Declarative Directive Generator to generate various pipeline source code which uses Extended Choice Parameter plugin

How to pass boolean parameter value in pipeline to downstream jobs?

I'm using Jenkins v2.1 with the integrated delivery pipeline feature (https://jenkins.io/solutions/pipeline/) to orchestrate two existing builds (build and deploy).
In my parameterized build I have 3 user parameters setup, which also needs to be selectable in the pipeline.
The pipeline script is as follows:
node: {
stage 'build'
build job: 'build', parameters: [[$class: 'StringParameterValue', name: 'target', value: target], [$class: 'ListSubversionTagsParameterValue', name: 'release', tag: release], [$class: 'BooleanParameterValue', name: 'update_composer', value: update_composer]]
stage 'deploy'
build job: 'deploy', parameters: [[$class: 'StringParameterValue', name: 'target', value: target]]
}
This works correctly except for the BooleanParameterValue. When I build the pipeline the following error is thrown:
java.lang.ClassCastException: hudson.model.BooleanParameterValue.value expects boolean but received class java.lang.String
How can I resolve this typecasting error?
Or even better, is there a less cumbersome way in which I can just pass ALL the pipeline parameters to the downstream job.
In addition to Jesse Glick answer, if you want to pass string parameter then use:
build job: 'your-job-name',
parameters: [
string(name: 'passed_build_number_param', value: String.valueOf(BUILD_NUMBER)),
string(name: 'complex_param', value: 'prefix-' + String.valueOf(BUILD_NUMBER))
]
Assuming
value: update_composer
was the issue, try
value: Boolean.valueOf(update_composer)
is there a less cumbersome way in which I can just pass ALL the pipeline parameters to the downstream job
Not that I know of, at least not without using Jenkins API calls and disabling the Groovy sandbox.
like Jesse Jesse Glick and abguy said you can enumerate string into Boolean type:
Boolean.valueOf(string_variable)
or the opposite Boolean into string:
String.valueOf(boolean_variable)
in my case I had to to downstream Boolean parameter to another job.
So for this you will need the use the class BooleanParameterValue :
build job: 'downstream_job_name', parameters:
[
[$class: 'BooleanParameterValue', name: 'parameter_name', value: false],
], wait: true
build job: 'downstream_job_name', parameters: [
booleanParam(name: 'parameter_name', value: false)
]
(cf. https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/#-build-%20build%20a%20job)
Jenkins "boolean" parameters are really just a shortcut for the "choice parameter" type with the choices hardcoded to the strings "true" and "false", and with a checkbox to set the string variable. But in the end, it is just that: a string variable, with nothing to do with a true boolean. That's why you need to convert the string to a boolean if you don't want to do a string comparison like:
if (myBoolean == "true")
Not sure if this answers this question. But I was looking for something else. Highly recommend see this 2 minute video. If you wanted to get into more details then see docs - Handling Parameters and this link
And then if you have something like blue ocean, the choices would look something like this:
As discussed in the video, Jenkins is blue because it's using Blue Ocean Plugin
You define and access your variables like this:
pipeline {
agent any
parameters {
string(defaultValue: "TEST", description: 'What environment?', name: 'userFlag')
choice(choices: ['TESTING', 'STAGING', 'PRODUCTION'], description: 'Select field for target environment', name: 'DEPLOY_ENV')
}
stages {
stage("foo") {
steps {
echo "flag: ${params.userFlag}"
echo "flag: ${params.DEPLOY_ENV}"
}
}
}
}
Automated builds will pick up the default params. But if you do it manually then you get the option to choose.
And then assign values like this:
Things are much easier nowadays: the builtin Snippet Generator supports the 'build' step (I don't know since when though).

Resources