Basti
Basti

Reputation: 686

Jenkins Groovy URL get parameter

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:[email protected]/job/myTest/job/dev/build

Now I want to add a GET parameter like this:

http://user:[email protected]/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

Upvotes: 1

Views: 5634

Answers (2)

Basti
Basti

Reputation: 686

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.

Upvotes: 3

eyalstoler
eyalstoler

Reputation: 184

This is achievable in 3 simple steps:

  1. Set a boolean parameter for your pipeline build: enter image description here
  2. 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}?"]]
    
  3. Use the "buildWithParameters" api command rather than "build":
    http://user:[email protected]/job/myTest/job/dev/buildWithParameters?skipInput=true

Upvotes: 0

Related Questions