Mandatory Jenkins parameter - jenkins

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.

Related

Assign multiple issues in Jira using Jenkins

I am trying to move and assign multiple issues from Jira using Jenkins
ticketID= jiraIssueSelector(issueSelector: [$class: 'JqlIssueSelector', jql: 'some JQL queries'])
step([$class: 'JiraIssueUpdateBuilder', jqlSearch: "someJQLqueries", workflowActionName: 'Done'])
jiraAssignIssue idOrKey: ticketID, userName: null, failOnError: false
The first step where I am changing the workflow is working fine. However, I can not change the assignee I am getting the bellow message. Is there a way to use the Jira plugin instead of the Jira plugin step to update a non-custom field?
java.lang.ClassCastException: org.thoughtslive.jenkins.plugins.jira.steps.AssignIssueStep.idOrKey expects class java.lang.String but received class java.util.HashSet
I also tried the following and it is not working:
step([$class: 'IssueFieldUpdateStep', fieldId: 'assignee', fieldValue: 'someUser', issueSelector: [$class: 'JqlIssueSelector', jql: 'some JQL query']])
I found the answer to loop through all the tickets in the jql:
ticketID= jiraIssueSelector(issueSelector: [$class: 'JqlIssueSelector', jql: 'some JQL queries'])
ticketID.each{ ticket ->
jiraAssignIssue idOrKey: ticket, userName: null, failOnError: false
}

Parameters control in jenkins using pipelines as code

Can we write like this in pipeline script?. Need to populate the string by choice parameter. I am new to Jenkins world.
parameters { choice(name: 'CHOICE', choices: ['FirstName', 'LastName'], description: 'Pick something') }
script {
if (${CHOICE} == "FirstName")
{
parameters { string(name: 'FirstName', defaultValue: '', description: 'This is FirstName')}
}
else
{
parameters { string(name: 'LastName', defaultValue: '', description: 'This is LastName') }
}
}
Is there any other way to do it inside the jenkins? Any plugin which can help!
It's a bit unclear what exactly you are trying to achieve.
If what you want is interactivity — when you make a choice in the dropdown box, the contents of another parameter changes based on your choice — then ActiveChoice plugin can help you with that.
If you want to define different params as depending on your choice of the first one, then it's not possible. Jenkins needs to fully parse your Jenkinsfile (parameters and all) in order to present you with the parameters screen, but you seem to want to define your parameters based on the results of the presentation. This is a kind of a chicken and egg problem here.

Jenkins pipeline dynamic choice parameter list for active checkboxes

I have a pipeline job where I want to execute Selenium tests on multiple browsers depending on the user parameters selection. Currently I have an extended choice parameter with checkboxes:
parameters{
extendedChoice(defaultValue: 'Chrome', description: 'Tests will run at the default resoluton of 1024x768', descriptionPropertyValue: '', multiSelectDelimiter: ',', name: 'Browsers', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'Chrome, Firefox, IE, Edge, Safari', visibleItemCount: 5) }
What I want to do is also add a single choice selection for each checked browser, consisting of browser supported versions, basically sending a map of browser-version key/values. I tried also with Active choice parameters but with no luck.
https://i.stack.imgur.com/GNwM8.jpg

how can I pop an input box for a specific choice?

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

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: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

Resources