In my Jenkins multi pipeline project i am having a input step like this:
input message: 'Merge', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: "Merge ${branchConfig.merge} to ${env.BRANCH_NAME}?"]]
I am starting this job by calling this url:
http://user:api-token#awesome.jenkins.de/job/myTest/job/dev/build
Now I want to add a GET parameter like this:
http://user:api-token#awesome.jenkins.de/job/myTest/job/dev/build?skipInput=true
My question now is, how can I get this parameter in groovy?
UPDATE: Following the first comment, I did the following:
// Add parameter to skip MergeInput.
properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'BooleanParameterDefinition', name: 'skipMergeInput', defaultValue: false]]]])
And adjusted the input like that:
input message: 'Merge', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: params.skipMergeInput, description: '', name: "Merge ${branchConfig.merge} to ${env.BRANCH_NAME}?"]]
When I am now starting my job, it shows me a popup that ask for the value that should be set. But no matter what i decide, the input is always false. I am trying to figure out what is going wrong and will update my post then.
UPDATE 2:
So I kept on debugging. I added the following to my groovy script:
// Add parameter to skip MergeInput.
def doMerge = properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'BooleanParameterDefinition', name: 'doMerge', defaultValue: true]]]])
println doMerge;
The output returns me NULL, and when I am doing something like
println params.doMerge
It tells me that params is not defined. Any idea what is going wrong?
UPDATE 3:
Call URL: /job/dg_test/job/master/buildWithParameters?test=true
Groovy Script:
properties([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [[$class: 'BooleanParameterDefinition', name: 'test', defaultValue: false]]]])
println params.test
Result:
No such property: params for class: groovy.lang.Binding
I finally solved it, this post really helped me: https://stackoverflow.com/a/41276956/1565249
And this is my implementation:
// Add fancy build parameter.
properties([
parameters([
booleanParam(
defaultValue: false,
description: 'Some description',
name: 'developmentMerge'
),
])
])
if (developmentMerge == "true" || developmentMerge == true) {
// Your code here
}
else {
// Your code here
}
When I now start my job manually from the GUI, it asks me which value should be set for "developmentMerge".
And I also can start my job by calling this URL:
"/job/dg_test/job/master/buildWithParameters?developmentMerge=true"
Where "dg_test" is the name of my Jenkins project and "master" is the job i wanted to start.
The if statement must be done like this:
if (developmentMerge == "true" || developmentMerge == true)
because when you start the job from GUI, it will send a boolean "true", but when you start the job by the URL call, you receive a string.
This is achievable in 3 simple steps:
Set a boolean parameter for your pipeline build:
Use the "params." prefix to access your parameter in your input message step:
input message: 'Merge', parameters: [[$class: 'BooleanParameterDefinition', defaultValue: params.skipInput, description: '', name: "Merge ${branchConfig.merge} to ${env.BRANCH_NAME}?"]]
Use the "buildWithParameters" api command rather than "build":
http://user:api-token#awesome.jenkins.de/job/myTest/job/dev/buildWithParameters?skipInput=true
Related
I have a jenkins job with multi select extended choice parameter. There are list of elements in a parameter. So, my requirement is I want to allow users to select multiple parameters excluding first element in a parameter. Means user should not able to select first element with other elements in a parameters. I am using jenkinsfile to create parameter.
Like shown above, users should not able to select 'None' with any other element in a parameter. Does anyone know how to do this?
I don't think you can do this with just Extended choice parameter. As a workaround, you can add two parameters. The first parameter to check whether it's None or not, if not you can bring up the second parameter. You can use the Active Choice Parameter for this.
properties([
parameters([
[$class: 'CascadeChoiceParameter',
choiceType: 'PT_MULTI_SELECT',
description: 'Select a choice',
filterLength: 1,
name: 'choice1',
referencedParameters: 'CHOICE',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: 'return ["ERROR"]'
],
script: [
classpath: [],
sandbox: true,
script: """
if (CHOICE == 'Yes') {
return['Item1','Item2','Item3']
}
else {
return[]
}
""".stripIndent()
]
]
]
])
])
pipeline {
agent any
parameters {
choice(name: 'CHOICE', description: "Do you have any choices?", choices: ["Yes", "None"])
}
stages {
stage("Run Tests") {
steps {
sh "echo SUCCESS on ${params.CHOICE}"
}
}
}
}
I would like to know how to make a parameter mandatory in Jenkins, so the "Build" button is unavailable until someone fills it.
[$class: 'hudson.model.StringParameterDefinition', defaultValue: 'master', description: 'Description', name: 'repositoryTag'],
I have this, and I was wondering if there is something I could do such as adding "required: True" for example.
In jenkins I'd like to do this:
parameters {
choice(
name: 'blah',
choices: 'one\ntwo\ncustom',
description: 'if you choose custom enter a custom number'
)
}
So they have three options on the drop down, but it would be nice if when they select the "custom" choice jenkins pops an input box to type in raw user input.
Is this possible? I don't want to use user input during the pipeline run because that means they need to choose custom then wait for jenkins to get to the stage where it asks them for input.
Yep, Once the build starts, check the value of params.blah and throw up an input step with a String param, so
if (params.blah == 'custom' ) {
timeout(time: 1, unit: 'minute') { // change to a convenient timeout for you
userInput = input(
id: 'Proceed1', message: 'Custom value?', parameters: [
[$class: 'StringParameterDefinition', defaultValue: '',
description: 'Enter Custom value', name: 'Value']
])
}
}
I'm trying to access a variable from an input step using the declarative pipelines syntax but it seems not to be available via env or params.
This is my stage definition:
stage('User Input') {
steps {
input message: 'User input required', ok: 'Release!',
parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor', description: 'What is the release scope?')]
echo "env: ${env.RELEASE_SCOPE}"
echo "params: ${params.RELEASE_SCOPE}"
}
}
Both echo steps print null. I also tried to access the variable directly but I got the following error:
groovy.lang.MissingPropertyException: No such property: RELEASE_SCOPE for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)
What is the correct way to access this choice parameter?
Since you are using declarative pipelines we will need to do some tricks. Normally you save the return value from the input stage, like this
def returnValue = input message: 'Need some input', parameters: [string(defaultValue: '', description: '', name: 'Give me a value')]
However this is not allowed directly in declarative pipeline steps. Instead, what you need to do is wrap the input step in a script step and then propagate the value into approprierte place (env seems to work good, beware that the variable is exposed to the rest of the pipeline though).
pipeline {
agent any
stages {
stage("foo") {
steps {
script {
env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor', description: 'What is the release scope?')]
}
echo "${env.RELEASE_SCOPE}"
}
}
}
}
Note that if you have multiple parameters in the input step, then input will return a map and you need to use map references to get the entry that you want. From the snippet generator in Jenkins:
If just one parameter is listed, its value will become the value of the input step. If multiple parameters are listed, the return value will be a map keyed by the parameter names. If parameters are not requested, the step returns nothing if approved.
Using the Input Step in the following form works for multiparameters:
script {
def params = input message: 'Message',
parameters: [choice(name: 'param1', choices: ['1', '2', '3', '4', '5'],description: 'description'),
booleanParam(name: 'param2', defaultValue: true, description: 'description')]
echo params['param1']
echo params['param2']
}
#DavidParker, I have figured out what you are asking. Just try to use it in your code. I have input params and using them 1 by 1. I am printing only "variable" key in job which is working. You can also try this way.
steps{
script {
deploymentParams.findAll{ key, value -> key.startsWith('backendIP') }.each { entry ->
stage("Node_$entry.key "){
echo $entry.value
}
}
}
}
I have 3 Jobs (let's name it as job1,job2,job3) which all accepts same parameters. I have a build flow calling these jobs
Eg Build Flow::
build(job1, param1 : "value1", param2 : "value2" )
build(job2, param1 : "value1", param2 : "value2" )
build(job3, param1 : "value1", param2 : "value2" )
As you see in the example all the jobs accept same parameters.
And I want to run the same sequence in the build flow (job1, job2, job3) with different parameter values.
So Now I am having different build flows to pass different values to the parameter:
eg:
Build flow1---
build(job1, param1 : "value1", param2 : "value2" )
build(job2, param1 : "value1", param2 : "value2" )
build(job3, param1 : "value1", param2 : "value2" )
Build flow2---
build(job1, param1 : "value3", param2 : "value4" )
build(job2, param1 : "value3", param2 : "value4" )
build(job3, param1 : "value3", param2 : "value4" )
I am thinking to have a build flow as shared and able to pass parameters to it
like :
Example:::
Build flow
build(job1, param1 : $paramvalue1, param2 : $paramvalue2 )
build(job2, param1 : $paramvalue1, param2 : $paramvalue2 )
build(job3, param1 : $paramvalue1, param2 : $paramvalue2 )
And I want to call this build flow with different parameter values from another build flow.
Can I do that ??? If yes ,,
1. How to define and pass parameter to the flow ?
You can use Workflow Plugin to achieve what you want.
job1, job2 and job3 must be parameterized jobs with param1 and param2 as parameters (they can be freestyle jobs or whatever you want).
Create a parameterized Workflow job with param1 and param2 as parameters. The workflow script would be something like this:
build job: 'job1', parameters:
[[$class: 'StringParameterValue', name: 'param1', value: param1],
[$class: 'StringParameterValue', name: 'param2', value: param2]]
build job: 'job2', parameters:
[[$class: 'StringParameterValue', name: 'param1', value: param1],
[$class: 'StringParameterValue', name: 'param2', value: param2]]
build job: 'job3', parameters:
[[$class: 'StringParameterValue', name: 'param1', value: param1],
[$class: 'StringParameterValue', name: 'param2', value: param2]]
Then just run the top level job, it will ask you for parameters values and downstream jobs will be triggered (and top level parameters passed).