I use the DynamicReferenceParameter for some advanced parameter inputs in Jenkins.
Here is a tiny example, which I entered using the UI:
This works fine, my checkboxes and input fields do show up!
But when I try to define my parameters in my Jenkinsfile, I get an error. My Jenkinsfile looks like this:
properties([parameters([
[
$class: 'DynamicReferenceParameter',
name: 'SFM',
script: [
$class: 'GroovyScript',
fallbackScript: '',
script: """
def services = ['service1',
'service2',
'service3']
def html =
'''
<!DOCTYPE html>
<html>
<body>
<table id="serviceTable">
'''
for (service in services){
html += "<tr>"
html += "<td><input type=\"checkbox\" id=\"checkbox_$service\">$service</td>"
html += "<td><div id=\"version_$service\" >version: <input type=\"text\"></div></td>"
html += "</tr>"
}
html += '''
</table>
</body>
</html>
'''
return html
"""
]
]
])])
You see, I just copy&pasted the script from the UI input and surrounded it with """.
This results in error no. 1:
Groovy.lang.MissingPropertyException: No such property: service for class: WorkflowScript
So there is an error parsing my variables.
So I just tried to delete all variables and set static values (replace $service with service1). This results in error no. 2:
java.lang.ClassCastException: org.biouno.unochoice.model.GroovyScript.script expects class org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript but received class java.lang.String
at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:416)
at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:340)
at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:281)
Caused: java.lang.IllegalArgumentException: Could not instantiate {fallbackScript=, script=[...]
What do I do wrong?
script and fallbackScript are not really String's. They are using the Security Script Plugin classes (it's been like that for a while already).
Here's some code snippet that may help.
properties([parameters([
[
$class: 'DynamicReferenceParameter',
name: 'TEST',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [], sandbox: true, script: ''
],
script: [
classpath: [], sandbox: true, script:
"""
def html =
'''
<!DOCTYPE html>
<html>
<body>
<table id="serviceTable">
<tr>
<td><input type="checkbox" id="checkbox">service1</td>
<td><div id="version" >version: <input type="text"></div></td>
</tr>
</table>
</body>
</html>
'''
return html
"""
]
]
]
])])
ps: take it with a grain of salt, as I normally use only FreeStyle jobs. I think some people tried using the plug-in with pipelines, but as far as I know, it's still a work-in-progress (see https://issues.jenkins-ci.org/browse/JENKINS-39742 for example)
Hope that helps,
Bruno
properties([parameters([
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
omitValueField: true,
description: 'Editable field when PARENT_PARAM is Others',
name: 'ACTIVE_PARAM',
randomName: 'choice-parameter-5631314456178624',
referencedParameters: 'PARENT_PARAM',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: true,
script: "return['undefined']"
],
script: [
classpath: [],
sandbox: true,
script:
"""
inputBox="<input class='setting-input' name='value' type='text' value=''>"
"""
]
]
]
])])
Related
I added the below to the pipeline so while the pipeline is running - at some stage I want the user to choose from the parameters but the output returns with parentheses at beginning and end.
def envs = input(id: 'Upgarde', message: 'On which customer do you want to apply the upgrade?', submitter: 'admin', ok: 'Submit', parameters: [extendedChoice(defaultValue: env.ENV.split().toString(), description: '', descriptionPropertyValue: env.ENV.split().toString(), multiSelectDelimiter: '', name: 'Customers to upgrade', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_SELECT', value: env.ENV.split().toString())]).split(',')
Screenshot from the Jenkins UI:
enter image description here
Fixed by added .replace("[", "").replace("]", "")
I have a Jenkins pipeline That has parameters defined via active choice parameter,
defining a default value is done by:
defaultValue: '',
you can put a string there or leave it empty which will give you the default result of the groovyScript.
I am trying to change the default parameter using a script so it will take the value using a groovy script.
This is the snippet of the relevant part of the pipeline:
parameters([
extendedChoice(
bindings: '',
defaultValue: '',
groovyClasspath: '',
groovyScript:"""
def proc = ["bash","-c","/usr/local/bin/aws s3 ls s3://Spark-Jenkins-Clusters/"].execute() | ["bash","-c","cut -c32-"].execute()
proc.waitForOrKill(10000)
return proc.text.tokenize()
""",
multiSelectDelimiter: ',',
name: 'Choose_Cluster',
description: 'This parameter is nice',
quoteValue: false,
saveJSONParameterToFile: false,
type: 'PT_SINGLE_SELECT',
visibleItemCount: 5
),
So The way to do that is to use "defaultGroovyScript",
I didn't find it in the documentation I just saw an option in the UI and tried it and luckily it worked:
This is what I finally did:
parameters([
extendedChoice(
bindings: '',
defaultGroovyScript: """
def proc = ["bash","-c","/usr/local/bin/aws s3 ls s3://Spark-Jenkins-Clusters/"].execute() | \
["bash","-c","sort"].execute() | \
["bash","-c","sed 's/PRE//g'"].execute() | \
["bash","-c","grep main"].execute() | \
["bash","-c","tail -n 1"].execute() | \
["bash","-c","tr -d '/'"].execute()
proc.waitForOrKill(10000)
return proc.text.tokenize().reverse()
""",
groovyClasspath: '',
groovyScript:"""
def proc = ["bash","-c","/usr/local/bin/aws s3 ls s3://Spark-Jenkins-Clusters/"].execute() | ["bash","-c","cut -c32-"].execute()
proc.waitForOrKill(10000)
return proc.text.tokenize()
""",
multiSelectDelimiter: ',',
name: 'Choose_Cluster',
description: 'This parameter is nice',
quoteValue: false,
saveJSONParameterToFile: false,
type: 'PT_SINGLE_SELECT',
visibleItemCount: 5
),
I'm trying to make my Jenkins UI more clean.
My Jenkins file calls a function which in turn runs the following:
properties ([
[$class: 'GitLabConnectionProperty', gitLabConnection: 'GitlabConnection'],
[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
[$class: 'BooleanParameterDefinition', defaultValue: false, description: '', name: 'activateInTest'],
[$class: 'ChoiceParameterDefinition', choices: 'false\ntrue\n', description: 'If running newBuild, skip unit tests', name: 'skipUnitTests']
]]
])
Currently, I can access these parameters like this:
if(activateInTest == 'true') {
//Do something
}
After going through other docs and examples. It looked as if I could also access parameters by doing something like params.activateInTest, which did not work. I also tried doing something like params["activateInTest"], but that didn't work either.
The reason I want to access it this way params["..."], is because I would like to have the name of my parameter be "Activate in Test" rather than "activateInTest".
In this example I see the person does use "BooleanParameterDefinition" with spaces in the name. But I can't seem to figure out how to use spaces in the name. Having spaces in the name is my only goal here.
yes, its possible, just use following notation:
${params['Name with space']}
tested on old Jenkins: 2.149
Indeed it is possible, user "string reference" to access it, i.e. params."Activate in Test"
For example:
properties([parameters([
string(name: 'Activate in Test', defaultValue: 'default value')
])])
echo params."Activate in Test"
In Java and Groovy space in a variable does not support! and it's not recommended but Jenkins supports it with 'String referencing'
But If you want to decorate the parameter Display Name it would be something like this
Jenkins Declarative Pipeline
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')
booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
echo "Biography: ${params.BIOGRAPHY}"
echo "Toggle: ${params.TOGGLE}"
echo "Choice: ${params.CHOICE}"
echo "Password: ${params.PASSWORD}"
}
}
}
}
Scripted Pipeline
node {
properties(
[
parameters(
[string(defaultValue: '/data', name: 'Directory', description: "Directort Path"),
string(defaultValue: 'Dev', name: 'DEPLOY_ENV', description: "Deploy Environment")
]
)
]
)
stage('debug') {
echo "${params}"
}
}
I am trying to set properties by jobs actual name.
My pipeline header looks as follows:
def jobs_actual_name = [
choice(name: 'var', choices: ['choice1', 'choice2', 'choice3']),
]
properties([
parameters(jobs_actual_name)
])
The upper pipeline works fine but I want to use a parameter instead of directly giving the name.
Like:
def jobs_actual_name = [
choice(name: 'var', choices: ['choice1', 'choice2', 'choice3']),
]
def another_jobs_actual_name = [
choice(name: 'var', choices: ['choice1', 'choice2', 'choice3']),
]
properties([
parameters("${env.JOB_BASE_NAME}")
])
How would I do this? Thanks in advance.
You can have some logic execute before setting the properties:
def actualParameters = (env.JOB_BASE_NAME == 'jobs_actual_name' ? jobs_actual_name : another_jobs_actual_name)
properties([
parameters(actualParameters)
])
I am using GitLab. Given multiple directories, e.g. folder1, folder2, folder3. I would like the job run only if there is any changes under folder1 only. Does anyone able to get includedRegions to work in Jenkins pipeline job.
checkout(
[
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PathRestriction', excludedRegions: '', includedRegions: 'folder1/.*']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'user', url: 'ssh://something/experiment.git']]
]
)
If you use a pipeline the checkout step performs after the build was launched.
The only way to manage this is to check the changeset with the groovy and skip the build if there were no changes at the included directory.
I managed to get it working. Since the checkout will be used across projects in our Jenkins, I wrote shared libraries to make it handy to use.
Example: strToSparseCheckout.groovy
#!/usr/bin/env groovy
import hudson.plugins.git.extensions.impl.SparseCheckoutPath
def call(paths) {
def list = paths.split('\n')
def sparsePaths = []
def isDefaultAdded = false
list.each {
def path = it - '/.*'
def sparsePath = new SparseCheckoutPath(path)
sparsePaths.push(sparsePath)
}
return sparsePaths
}
Example: sparseCheckout.groovy
#!/usr/bin/env groovy
def call(Map namedargs) {
checkout(
[
$class: 'GitSCM',
branches: [[name: "${namedargs.branch}"]],
extensions: [
[$class: 'LocalBranch', localBranch: '**'],
[$class: 'RelativeTargetDirectory', relativeTargetDir: "${namedargs.target_dir}"],
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: strToSparseCheckout("${namedargs.included_regions}")],
[$class: 'PathRestriction', excludedRegions: """${namedargs.excluded_regions}""", includedRegions: """${namedargs.included_regions}"""]
],
userRemoteConfigs: [[credentialsId: 'user', url: "${namedargs.url}"]]
]
)
}
In the pipeline, we can call the sparseCheckout shared library.
pipeline {
stage('Checkout') {
sparseCheckout(
[
url: 'ssh://path.to.git/something.git',
branch: 'refs/heads/master',
target_dir: 'something',
excluded_regions: "${params.excluded_regions}",
included_regions: "${params.included_regions}"
]
)
}
}