detail description of throttleJobProperty in jenkins declarative pipeline - jenkins

I am building a jenkins pipeline and trying to configure thorttle within the jeknins script.
I found the following code:
options {
throttleJobProperty(
categories: ['TEST'],
limitOneJobWithMatchingParams: true,
maxConcurrentPerNode: 1,
maxConcurrentTotal: 2,
paramsToUseForLimit: 'PORT',
throttleEnabled: true,
throttleOption: 'category',
)
}
However, I can't find an explanation for each parameter.
https://javadoc.jenkins.io/plugin/throttle-concurrents/hudson/plugins/throttleconcurrents/ThrottleJobProperty.html
I saw above document, but I don't know.
Please explain each parameters.

Related

In jenkins how to restrict users to select first default element with other options in extended choice parameter

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

Need plugin to control who can approve a stage in Jenkins Pipeline

I need a plugin where a team lead can approve if a pipeline can move to the next stage in Jenkins. I am planning to use multistage pipeline(declarative) so after dev stage I need a person to approve(only he can approve) and the developer folks can just run the job. Is there any such plugin available so that only one person can approve such request?
tried role based access plugin but here there is no ways to control segregate people who can do stage approvals
#sidharth vijayakumar, I guess you can make use of the 'Pipeline: Input Step' plugin.
As per my understanding, this plugin pauses Pipeline execution and allows the user to interact and control the flow of the build.
The parameter entry screen can be accessed via a link at the bottom of the build console log or via link in the sidebar for a build.
message
This parameter gives a prompt which will be shown to a human:
Ready to go?
Proceed or Abort
If you click "Proceed" the build will proceed to the next step, if you click "Abort" the build will be aborted.
Your pipeline script should look like some what similar to below snippet
// Start Agent
node(your node) {
stage('Checkout') {
// scm checkout
}
stage('Build') {
//build steps
}
stage('Tests') {
//test steps
}
// Input Step
{
input message: 'Do you want to approve if flow can proceeded to next stage?', ok: 'Yes'
}
stage('Deploy') {
...
}
}
Reference link : https://www.jenkins.io/doc/pipeline/steps/pipeline-input-step/
To solve the problem of role based approval i used input block with submitter. This means that person who listed as the submitter will only be able to give stage approvals.
stage('Approval') {
agent none
steps {
script {
def deploymentDelay = input id: 'Deploy', message: 'Deploy to production?', submitter: 'rkivisto,admin', parameters: [choice(choices: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'], description: 'Hours to delay deployment?', name: 'deploymentDelay')]
sleep time: deploymentDelay.toInteger(), unit: 'HOURS'
}
}
}
Please note there submitter must be a valid user. Use role based access plugin and create a user with relevant access.
Reference link for the script :
https://support.cloudbees.com/hc/en-us/articles/360029265412-How-can-I-approve-a-deployment-and-add-an-optional-delay-for-the-deployment

Is there a way to have a placeholder text at Validating String Parameter in a Free-Style Job

as the title says I need a placeholder text for "Validating String Parameter" in a Free-Style job in Jenkins. What I'm mentioning looks as follows.
But what you see in the image is an Active Choices Reactive Reference Parameter" and it lets you only put the text into the Filter section. I need a text in the parameter itself and if there isn't any entry Jenkins should consider it as an empty variable. Is that possible if yes how? Thank you in advance.
Hello there is a way through the Active Choices Reactive Reference Parameter. The choice type will be Formatted HTML.
The jenkinsfile I use has a parameters block outside of the pipeline block and it is declarative. I'm not sure if this will work with scripted.
properties([
parameters([
[
$class: 'DynamicReferenceParameter',
name: 'INPUT_TEST',
choiceType: 'ET_FORMATTED_HTML',
description: '',
referencedParameters: '',
omitValueField: true,
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: 'return [""]'
],
script: [
classpath: [],
sandbox: false,
script: '''
return '<input name="value" class="jenkins-input" placeholder="Placeholder text here">'
'''
]
]
]
)]
)]
Note: the name should be "value", the class is optional the jenkins-input will make it look like a String parameter. Placeholder is where you want to have the text.

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

How do I run a Jenkins job where user can enter a date value?

I wanted to run a jenkins job by accepting a date field (in format YYYY-MM-DD) from user. I found a link where user can enter a string parameter:
job('example') {
parameters {
stringParam('myParameterName', 'my default stringParam value', 'my description')
}
}
But in string param user can enter any thing. So how do I force user to enter a date field like a calender field and select date from the calender ?
There seems to be no plugin which provides a date chooser.
But you can use the Validating String Parameter Plugin, which can use a regular expression to validate a string parameter. See Regex to validate date format dd/mm/yyyy for regular expressions matching date values.
The Job DSL plugin has no built-in support for the Validating String Parameter Plugin, but you can use a Configure Block to add the relevant config XML.
job('example') {
configure { project ->
project / 'properties' / 'hudson.model.ParametersDefinitionProperty' / parameterDefinitions << 'hudson.plugins.validating__string__parameter.ValidatingStringParameterDefinition' {
name('DATE')
description('date in YYYY-MM-DD format')
defaultValue('2016-03-01')
regex(/\d\d\d\d-\d\d-\d\d/)
failedValidationMessage('Enter a YYYY-MM-DD date value!')
}
}
}
I came across this same issue today and this is how I solved it.
Using: Active Choice Plugin
In a Declarative Pipeline I added the following parameter
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HIDDEN_HTML',
description: '',
name: 'YOUR_PARAMETER_NAME',
omitValueField: true,
referencedParameters: '',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: [], sandbox: false, script: ''],
script: [
classpath: [],
sandbox: false,
script: 'return "<input type=\\"date\\" name=\\"value\\" value=\\"\\" />"'
]
]
]
Basically it adds an HTML Input Element of type Date, which you can then catch the value during the run.
pipeline {
agent { label "master" }
stages {
stage('Output Date') {
steps {
script {
println params.YOUR_PARAMETER_NAME
}
}
}
}
}
Here's a picture of how it looks on Chrome:
HTML Date parameter
Note: You can also use it to add parameters of type TextArea and so on.

Resources