I have a choice parameter called 'Data Center' which has two values DC01 and DC02. If user chooses DC01 I want few other build parameters to show up, if he chooses DC02 I want other parameters to show up. Is there any way to do it?
If user chooses DC01, I want two more fields such as 'hostname' and 'IP address' which are just text fields.
I have tried using Active Choice Plugin, but I don't think we can generate text field parameters using that.
Can anyone help?
We can generate text field parameters using Active Choices Plugin.
We will use two types of parameters here: Active Choices Parameter and Active Choices Reactive Reference Parameter
Active Choices Reactive Reference Parameter
In Active Choices Reactive Reference Parameter, there are wide variety of rendering options available, of which one is Formatted HTML i.e. in the return string you can pass the html tags.
So, making use of the above two parameters, here is the pipeline code that will help in your case:
Note: After saving the below pipeline code, first Click on Build Now. After the build got success, then refresh the page. You will be able to see the option Build with Parameters. You can also see in the Job Configuration page, all the fields of This build is parameterized gets populated automatically once you build the job after saving the pipeline.
properties([
parameters([
[$class: 'ChoiceParameter',
choiceType: 'PT_CHECKBOX',
description: 'Select the Application Service from the Dropdown List',
filterLength: 1,
filterable: false,
name: 'data_center',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script:
"return['Could not get the services list']"
],
script: [
classpath: [],
sandbox: false,
script:
"return['DC01', 'DC02', 'DC03']"
]
]
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: 'enter job params',
name: 'hostname',
referencedParameters: 'data_center',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (data_center.contains('DC01')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
} else
if (data_center.contains('DC02')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
}
'''
]
],
omitValueField: true
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: 'enter job params',
name: 'ipaddress',
referencedParameters: 'data_center',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (data_center.contains('DC01')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
} else
if (data_center.contains('DC02')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
}
'''
]
],
omitValueField: true
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: 'enter job params',
name: 'port_number',
referencedParameters: 'data_center',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (data_center.contains('DC02')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
}
'''
]
],
omitValueField: true
]
])
])
pipeline {
environment {
vari = ""
}
agent any
stages {
stage ("Example") {
steps {
script{
echo "${params.data_center}"
echo '\n'
echo "${params.hostname}"
echo "${params.ipaddress}"
echo "${params.port_number}"
}
}
}
}
}
Screenshots:
Output when data center DC01 is selected,
Output when data center DC02 is selected,
Related
i want to have checkbox/single select , when user checked the checkbox/use the filed in the single select , it will create dynamically few text box(5).2 of those text box should contain password/keys (so should be encrypted /not shown in jenkins parameters.
i tried with Active Choices Parameter and Active Choices Reactive Reference Parameter from jenkins file and from gui
example:
i have parameters :
Active Choices Parameter
name: use_custom
Groovy Script: return ["true", "false"]
Choice Type: single select
Active Choices Reactive Reference Parameter
name: custom_values
Groovy Script:
myPassword = ''
if ( use_custom == "true") {
return """
inputBox="<input class='setting-input' name='value' type='password' value=''>"
inputBox="<input class='setting-input' name='value' type='text' value=''>"
"""
echo(text1)
}
else {
return ""
}
Choice Type: formatted html ,
Referenced parameters: use_custom
i created 2 dynamic html box (work only for text box , not passwords/key that user need to enter manually (not via jenkins cred)
example output in parameters:
custom_values: 1234,11
*it show the password in jenkins params (free text)
what is the solution to allow user to write password and text box ( 2 pass, 3 box) when another field is true (password cannot be free text)
groovy pipeline example(in case of Env:dev , create 2 textbox):
properties([
parameters([
[$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select the Env Name from the Dropdown List',
filterLength: 1,
filterable: true,
name: 'Env',
randomName: 'choice-parameter-5631314439613978',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script:
'return[\'Could not get Env\']'
],
script: [
classpath: [],
sandbox: false,
script:
'return["Dev","QA","Stage","Prod"]'
]
]
],
// [$class: 'CascadeChoiceParameter',
// choiceType: 'ET_FORMATTED_HTML',
// description: 'Select the Server from the Dropdown List',
// filterLength: 1,
// filterable: true,
// name: 'Server',
// randomName: 'choice-parameter-5631314456178619',
// referencedParameters: 'Env',
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
omitValueField: true,
description: '123',
name: 'Server',
randomName: 'choice-parameter-5631314456178624',
referencedParameters: 'Env',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script:
'return[\'Could not get Environment from Env Param\']'
],
script: [
classpath: [],
sandbox: false,
script:
''' if (Env.equals("Dev")){
inputBox2 = "<input name='value' class='setting-input' type='text'>"
inputBox = "password(name: 'KEY', description: 'Encryption key')"
return[inputBox]
}
else {
return ""
}
'''
]
]
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
omitValueField: true,
description: '1234',
name: 'pass',
//randomName: 'choice-parameter-5631314456178624',
referencedParameters: 'Env',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script:
'return[\'Could not get Environment from Env Param\']'
],
script: [
classpath: [],
sandbox: false,
script:
''' if (Env.equals("Dev")){
inputBox = "<input name='value' class='setting-input' type='password'>"
return[inputBox]
}
else {
return ""
}
'''
]
]
]
])
])
pipeline {
environment {
vari = ""
}
agent any
stages {
stage ("Example") {
steps {
script{
echo 'Hello'
echo "${params.Env}"
echo "${params.Server}"
echo "${params.pass}"
if (params.Server.equals("Could not get Environment from Env Param")) {
echo "Must be the first build after Pipeline deployment. Aborting the build"
currentBuild.result = 'ABORTED'
return
}
echo "Crossed param validation"
} }
}
}
}
i tried also with mask_plugin , it mask from console output , not from parameters , any ideas ?
thanks
I am trying to set a global map variable in a jenkins declarative pipeline. I am trying to access it inside an Active Choice Reactive Reference parameter. I tried many ways to achieve this but nothing worked.
Below is my sample Pipeline.
def sampleMap= [
'students' : ['12312312'],
'teachers' : ['145436436']
]
pipeline {
agent any
stages {
stage('set params') {
steps{
script {
properties([
parameters([
[
$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: '',
filterLength: 1,
filterable: false,
name: 'Type',
randomName: 'choice-parameter-1325654724334254',
referencedParameters: '',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: ''
], script: [
classpath: [],
sandbox: true,
script: '''
def choices = []
choices.add('students')
choices.add('teachers')
return choices'''
]
]
],
[
$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: '',
name: 'value',
randomName: 'choice-parameter-14347325234254',
referencedParameters: 'Type',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: ""
],
script: [
classpath: [],
sandbox: true,
script: '''
def result= ${sampleMap.get(Type)}
return """<input name=\"value\" value=\"${result}\" class=\"setting-input\" type=\"text\">"""
'''
]
],
omitValueField: true
]
])
])
}
}
}
}
As per above script, I have a global Map variable with list of students and list of teachers. I have two build parameters named as Type and value. Type is a dropdown with values 'students' and 'teachers' . Based on the dropdown selection, I want to refer to the global map variable and access it's respective value in another build parameter.
It seems like the active choice parameter is unable to access global variables. Or is that a syntax issue?
Can anyone help?
Thanks!
You should pass whole map into script and call it:
[$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
filterLength: 1,
filterable: false,
name: 'value',
referencedParameters: 'Type',
script: [$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: 'return ["ERROR"]'
],
script: [
classpath: [],
sandbox: true,
script: """
def sampleMap = ${sampleMap.inspect()}
return sampleMap.get(Type)
""".stripIndent()
]
]
]
I am trying to use following jenkins job to generate Active Choices user selectable params for a job.
my ask is to show/hide textbox when heartbeat_consumer checkbox is selected/unselected. I would also like to store the value of user input in heartbeat_consumer_parms, which can be accessed thru env.heartbeat_consumer_parms. I plan to use this value in downstream jobs.
pipeline {
agent any
stages {
stage('Parameters'){
steps {
script {
properties([
parameters([
text(
defaultValue: '''''',
name: 'application_servers',
description: 'Please provide semicolon delimited (;) application server list ',
),
[$class: 'CascadeChoiceParameter',
choiceType: 'PT_CHECKBOX',
description: 'Select Services',
name: 'application_services_list',
referencedParameters: 'application_servers',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (application_servers.length() > 0){
return["heartbeat_consumer", "surgeon_cloud_login", "system_configuration"]
}
'''
]
]
]
])
])
if (application_services_list.contains('heartbeat_consumer')){
text(
defaultValue: '''''',
name: 'heartbeat_consumer_parms',
description: 'Please provide heartbeatconsumer job parms ',
)
}
}
}
}
}
}
with current implmentation I am not seeing that textbox heartbeat_consumer_parms is being displayed at all.
Pipeline script for your scenario:
properties([
parameters([
[$class: 'ChoiceParameter',
choiceType: 'PT_CHECKBOX',
description: 'Select the Application Service from the Dropdown List',
filterLength: 1,
filterable: false,
name: 'application_services_list',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script:
"return['Could not get the services list']"
],
script: [
classpath: [],
sandbox: false,
script:
"return['heartbeat_consumer', 'surgeon_cloud_login', 'system_configuration']"
]
]
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: 'SAdd params',
name: 'heartbeat_consumer_parms',
omitValueField: 'true',
referencedParameters: 'application_services_list',
script:
[$class: 'GroovyScript',
script: 'return["Could not get Information"]',
script: [
script: '''
if (application_services_list.equals("heartbeat_consumer")){
inputBox="<input type ='text' id = 'myText' name='value' >"
return inputBox
}
'''
]
]
]
])
])
pipeline {
environment {
vari = ""
}
agent any
stages {
stage ("Example") {
steps {
script{
echo "${params.application_services_list}"
echo '\n'
echo "${params.heartbeat_consumer_parms}"
}
}
}
}
}
Please find the screenshot of the output:
Output of pipeline after build:
this worked for me,
pipeline {
agent any
stages {
stage('Parameters'){
steps {
script {
properties([
parameters([
text(
defaultValue: '''''',
name: 'application_servers',
description: 'Please provide semicolon delimited (;) application server list ',
),
[$class: 'CascadeChoiceParameter',
choiceType: 'PT_CHECKBOX',
description: 'Select Services',
name: 'application_services_list',
referencedParameters: 'application_servers',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (application_servers.length() > 0){
return["heartbeat_consumer", "surgeon_cloud_login", "system_configuration"]
}
'''
]
]
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: 'enter job params',
name: 'hb_job_params',
referencedParameters: 'application_services_list',
script:
[$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: "return['']"
],
script: [
classpath: [],
sandbox: false,
script: '''
if (application_services_list.contains('heartbeat_consumer')){
return """<textarea name=\"value\" rows=\"5\" class=\"setting-input \"></textarea>"""
}
'''
]
],
omitValueField: true
],
])
])
}
}
}
}
}
I used, DynamicReferenceParameter for using HTML generated textarea however accessing entered textarea values was tricky. after a bit of researching I found that,
The trick for formatting a text input box is to format the HTML in a way that reflects the HTML Jenkins uses to display parameters in a job submission form. The easiest way to do this is to review the HTML Jenkins uses to display an existing String parameter.
src: https://github.com/biouno/uno-choice-plugin/wiki/Using-Uno-Choice-for-Dynamic-Input-Text-Box-Defaults
I have the following Managed Jenkinsfile for Pipeline Job stripped of the Stages for brevity.
#!groovy
import groovy.json.JsonSlurperClassic
def json = new File("TEST_JSON.json").text
def data = new JsonSlurperClassic().parseText(json)
def string_1 = data.test
properties([
parameters([
string(defaultValue: string_1, description: 'STRING 1', name: 'STRING_1', trim: false),
string(defaultValue: string_1, description: 'STRING 2', name: 'STRING_2', trim: false),
[$class: 'ChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'MPSS Flavor',
filterLength: 1, filterable: true,
name: 'MPSS_FLAVOR', randomName: 'choice-parameter-10980926894589',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: [], sandbox: false, script: ''],
script: [
classpath: [], sandbox: false,
script: '''
import groovy.json.JsonSlurperClassic
def json = new File("TEST_JSON.json").text
def data = new JsonSlurperClassic().parseText(json)
mpss_flavors = []
for (option in data.options) {
println option
mpss_flavors.add(option.mpss_flavor)
}
return mpss_flavors
'''
]
]
],
[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'TARGET',
filterLength: 1, filterable: true,
name: 'TARGET', randomName: 'choice-parameter-10980967122105',
referencedParameters: 'MPSS_FLAVOR',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: [], sandbox: false, script: ''],
script: [
classpath: [], sandbox: false,
script: '''
import groovy.json.JsonSlurperClassic
def json = new File("TEST_JSON.json").text
def data = new JsonSlurperClassic().parseText(json)
targets = []
for (option in data.options) {
if (option.mpss_flavor == MPSS_FLAVOR) {
targets.add(option.target);
}
}
return targets;
'''
]
]
],
[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Build Command',
filterLength: 1, filterable: true,
name: 'BUILD_CMD', randomName: 'choice-parameter-11980967122105',
referencedParameters: 'TARGET,MPSS_FLAVOR',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: [], sandbox: false, script: 'return ["ERROR"]'],
script: [
classpath: [], sandbox: false,
script: '''
import groovy.json.JsonSlurperClassic;
def json = new File("TEST_JSON.json").text;
def data = new JsonSlurperClassic().parseText(json);
build_cmds = [];
for (option in data.options) {
if ((option.mpss_flavor == MPSS_FLAVOR) && (option.target == TARGET)) {
build_cmds.addAll(option.build_commands);
}
}
return build_cmds;
'''
]
]
]
])
])
And the following JSON File containg the configurations
{
"test": "TEST STRING FROM JSON",
"options": [
{
"mpss_flavor": "MPSS1",
"target": "TARGET1",
"build_commands": [
"BUILD_COMMAND_1"
]
},
{
"mpss_flavor": "MPSS2",
"target": "TARGET2",
"build_commands": [
"BUILD_COMMAND_2",
"BUILD_COMMAND_3"
]
}
]
}
I would like to configure the Parameters for the Job automatically when the JOSN file is updated (I am fully aware that the first Job run after the JSON Update will not contain the intended changes, but that is Ok for us). MPSS_FLAVOR and TARGET are showing the Values as intended. However the BUILD_CMD Choice parameter is returning an error. When I run the groovy script code with Statically defined MPSS_FLAVOR and TARGET in command line the Script works fine and the returned build_cmds are as expected. However Through Jenkins UI It is showing as ERROR (Fallback Script)
I have tried several iterations without any success. I am sure there is a minor mistake and I Could Figure out.
My Question is there any way to see the print Logs of the script that is used for choice parameter to isolate the issue.
Update
Same code works fine for Jenkins file defined within the Job. issue seems to be specific to Managed Jenkins file
just wrap script with try-catch
try{
...your parameter script here
}catch(Throwable t){
return [t.toString()]
}
in this case you'll see error as a parameter value
I have a consul keys AAA/BBB/test-key like '1,2,3', AAA/CCC/test-key like '4,5,6' and others.
I have a shared Jenkinsfile between several jobs.
I do not want to make Jenkinfile per each job. I want to access keys by job name but I can't make it working.
It works if I hardcode key in the URL, e.g.
node('master') {
properties([parameters([
[ $class: 'ChoiceParameter',
name: 'GROUPS',
description: 't2',
randomName: 't3',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [], sandbox: false, script: ''
],
script: [
classpath: [], sandbox: false, script:
'''
def text = new URL('http://consul.local:8500/v1/kv/AAA/BBB/test-key?raw').getText()
return text.split(",").collect{ (it=~/\\d+|\\D+/).findAll() }.sort().collect{ it.join() } as List
'''
]
],
choiceType: "PT_RADIO", //PT_SINGLE_SELECT,PT_MULTI_SELECT,PT_RADIO,PT_CHECKBOX
filterable: true,
filterLength: 1
]
])])
}
However, when I try to use env.JOB_NAME inside the URL, it does not work:
node('master') {
properties([parameters([
[ $class: 'ChoiceParameter',
name: 'GROUPS',
description: 't2',
randomName: 't3',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [], sandbox: false, script: ''
],
script: [
classpath: [], sandbox: false, script:
'''
def text = new URL('http://consul.local:8500/v1/kv/AAA/'+ env.JOB_NAME + '/test-key?raw').getText()
return text.split(",").collect{ (it=~/\\d+|\\D+/).findAll() }.sort().collect{ it.join() } as List
'''
]
],
choiceType: "PT_RADIO", //PT_SINGLE_SELECT,PT_MULTI_SELECT,PT_RADIO,PT_CHECKBOX
filterable: true,
filterLength: 1
]
])])
}
How can I access env variables inside the choice parameter defined with the Groovy script?
If you want to pass env.JOB_NAME to the script content you have to replace ''' with """ and refer to the variable with ${env.JOB_NAME}. Something like this:
script: [
classpath: [], sandbox: false, script:
"""
def text = new URL('http://consul.local:8500/v1/kv/AAA/${env.JOB_NAME}/test-key?raw').getText()
return text.split(",").collect{ (it=~/\\d+|\\D+/).findAll() }.sort().collect{ it.join() } as List
"""
]
Yes after changing ''' to """,my issue got resolved.
And also by using ${abc}
Thanks #szymon-stepniak but ${env.JOB_NAME} does not work in ChoiceParameter
Next code works well
node('master') {
properties([parameters([
[ $class: 'ChoiceParameter',
name: 'GROUPS',
description: 't2',
randomName: 't3',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [], sandbox: false, script: ''
],
script: [
classpath: [], sandbox: false, script:
"""
def build = Thread.currentThread().toString()
def regexp= ".+?/job/(.*?)/build "
def match = build =~ regexp
def jobName = match[0][1].replaceAll("/job/", "/") as String
def text = new URL('http://consul.local:8500/v1/kv/${jobName}/test-key?raw').getText()
return text.split(",").sort() as List
"""
]
],
choiceType: "PT_RADIO", //PT_SINGLE_SELECT,PT_MULTI_SELECT,PT_RADIO,PT_CHECKBOX
filterable: true,
filterLength: 1
]
])])
}