Required $class definition for all the Jenkins parameters - jenkins

How do I get to know all the $class definitions for all the parameters available in the Jenkins.
For an example, I have $class definitions for NodeParameter & String Parameter. Likewise, I want for all the available Jenkins parameters. Pls do share.
[
$class: 'NodeParameterValue', name: 'HOST', labels: ["${env.HOSTNAME}"], nodeEligibility: [$class: 'AllNodeEligibility']
],
[
$class: 'StringParameterValue', name: 'RELEASE_VERSION', value: "main" ],

Related

Groovy Script - Active Choices Jenkins Plugin - variable reference from inside "script:" section

I'm using the "Active Choices Jenkins Plugin" in order to realize a pipeline.
I've defined my parameters.
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: '_description',
name: '_name',
referencedParameters: '**customer**',
script:
[$class: 'GroovyScript',
script: [
sandbox: true,
script: **MUST INSERT SCRIPT**
]
]
Now i go a pretty big script to be processed, that need to evaluate:
A static variabile defined before execution
def customer_dictionary = [
"customer1": ["a", "b", "c"],
"customer2": ["a", "b", "c", "d"],
]
A runtime variabile passed through the referencedParameters, "customer" ) .
customer.each { service->
html_to_be_rendered += "<tr><td>"
configuration = configuration_dictionary[service]
I've to pass to "script:" a string.
But even if i use string interpolation ( """ script """ ) i can't manage to make him resolve this reference ( configuration_dictionary[service] ).
Someone can help me?
Thx

How to pass Jenkins 'file' parameter value to downstream pipeline job

I am looking for solution to pass a 'file' (.csv) parameter value to downstream job. I have tried with below code but its not working.
build job: "DownstreamJobName",
parameters: [
string(name: 'Releases', value: "1.2.9"),
[$class: "FileParameterValue", name: "test.csv", file: new FileParameterValue.FileItemImpl(new File(env.WORKSPACE/env.filepath))],
string(name: 'UserEmail', value: "testemail")
]
When I got researched got below link that there is an existing defect with file for Jenkins pipeline, dont know whether it got fixed or not. https://issues.jenkins.io/browse/JENKINS-27413
I am able to solve this like below
propertiesFilePath = "${env.WORKSPACE}/test.csv"
parameters: [
[$class: "FileParameterValue", name: "test.csv", file: new FileParameterValue.FileItemImpl(new File(propertiesFilePath))]
]

Export build parameters into shared library

This is an example of my Jenkinsfile:
properties([
parameters([
booleanParam(defaultValue: false, name: 'BuildAll', description: ''),
// lots of params here, some of them are Active Choice Plugin params...
])
])
pipeline {
agent any
stages {
stage ('Initialize') {
// code...
}
}
}
Now, is it posslible to export those parameters (that are enclosed inside "properties" section) into shared library?
Jenkins shared library: https://www.jenkins.io/doc/book/pipeline/shared-libraries/
I have lots of params and many similar projects and I would like simply to define parameters on one place and include them everywhere (DRY).
create deriveJobParams.groovy in vars folder with following code in shared library project.
def call() {
properties([
parameters([
booleanParam(defaultValue: false, name: 'BuildAll', description: ''),
[$class: 'ChoiceParameter', choiceType: 'PT_CHECKBOX',
description: 'Choose environment category.',
name: 'ENVIRONMENT',
script: [
$class: 'GroovyScript',
script: [sandbox: true, script: 'return ["QA", "DEV", "PROD"]']
]
]
])
])
}
In job Jenkinsfile, import the share library and call deriveJobParams()
#Library('my-library#branch or tag') _
// call deriveJobParams at beginning
deriveJobParams()
pipeline {
stages {
}
}

How to send referencedParameters value in Jenkins pipeline for CascadeChoiceParameter to ScriptlerScript

I'm declaring 2 parameters in properties section of a Jenkins pipeline:
a data center (can have multiple environment types)
an environment type
The data center is of type ChoiceParameter; the list is retrieved from a database; when the data center changes in the drop-down list, the environment types should populate accordingly, also from the database, through a ScriptlerScript.
The problem is that when changing selection on data center, nothing happens for environment types list, which is a CascadeChoiceParameter with referencedParameters: 'DataCenter'.
How should I link the referenced parameter to the scriptlet script I'm using - what do I have to send ?
The issue is with [name:'DataCenter', value: '$DataCenter'] for second parameter - the value is not sent to the ScriptletScript when first drop-down value changes.
If I define the 2 parameters from the Jenkins interface - so not through the DSL pipeline - under Configure section, everything works as expected.
It's not working for me to use something other than properties section - I've tried using activeChoiceParameter inside pipeline section, but I get an error 'Build parameters definitions cannot have blocks # line (...)', which is a known issue (see first example link below).
Examples I've used:
Jenkinsfile Active Choice Parameter
Active Choices Reactive Reference Parameter in jenkins pipeline
properties([
parameters([
[
$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
name: 'DataCenter',
script: [
$class: 'ScriptlerScript',
scriptlerScriptId:'getdatacenters.groovy',
parameters: [
[name:'StatusId', value:'']
]
]
],
[
$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
name: 'EnvironmentType',
script: [
$class: 'ScriptlerScript',
scriptlerScriptId:'getenvtypesbydatacenter.groovy',
referencedParameters: 'DataCenter',
parameters: [
[name:'DataCenter', value: '$DataCenter']
]
]
]
])
])
pipeline {
...
Expected result: Second drop-down list populates when data center changes
Actual result: Nothing happens when data center changes
Pipeline with params configured in UI - behaves ok (environment types loading on data center change):
One thing to keep in mind: Scriptlers are not secure, you should not use them: https://wiki.jenkins.io/display/JENKINS/Scriptler+Plugin !.
That being said, if you still want to go on and use Scriptler plugin and CascadeChoiceParameter, the code might look like this:
properties([
parameters([
[
$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
name: 'DataCenter',
randomName: 'datacenter-choice-parameter-102102304304506506',
script: [
$class: 'ScriptlerScript',
scriptlerScriptId:'getdatacenters.groovy',
fallbackScript: [ classpath: [], script: 'return ["N/A"]']
]
],
[
$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
name: 'EnvironmentType',
randomName: 'envtype-choice-parameter-101012020230303404',
referencedParameters: 'DataCenter',
script: [
$class: 'ScriptlerScript',
scriptlerScriptId:'getenvtypesbydatacenter.groovy',
fallbackScript: [ classpath: [], script: 'return ["N/A"]'],
]
]
])
])
The groovy code for getdatacenters.groovy for demo purposes is (but might be retrieved from a DB, as an alternative):
return["Dev","Prod"]
The groovy code for getenvtypesbydatacenter.groovy might look like this:
import groovy.sql.Sql
import jenkins.model.*
nodes = Jenkins.instance.globalNodeProperties
nodes.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
sql = Sql.newInstance("jdbc:sqlserver://SQLServerHere;connectionDataHere", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
envTypes = sql.rows("exec [DbHere].[schema].[GetEnvTypes] #DataCenter = $DataCenter").collect({ query -> query.EnvTypeName})
envTypes.add(0,'')
return envTypes
The most important thing to note here is that referencedParameters: 'DataCenter' was not inside the script block, but at the "root" level. If you need more parameters, you can separate them with comma.
Because DataCenter is a referenced parameter and automatically transmitted to the scriptler, $DataCenter variable from inside the SQL query will be mapped with its value. As a note, DataCenter should be added as parameter of the scriptler, in the UI Parameters section.
Credits for the solution go to CloudBees.

Jenkins: MatrixCombinationsParameterValue from a pipeline

I'm want to start a matrix-build from a pipeline job but I want to build only one Axis.
I tried with this:
build job: "Build_Android_Matrix", propagate: false, wait: true,
parameters: [[$class: 'StringParameterValue', name: 'branch', value: "$branch"],
[$class: 'BooleanParameterValue', name: 'production', value: true],
[$class: 'BooleanParameterValue', name: 'beta', value: false],
[$class: 'MatrixCombinationsParameterValue', name: 'paramFilter', description: null, combinations: ['buildType=Release']]]
I have 2 Axes, flavor and buildType, and paramFilter is the matrix combinations parameter.
The matrix-build starts with all the job parameters but it builds nothing because the matrix combinations selection is empty.
I've also tried with ['buildType==Release'] and ['buildType=="Release"'] but I always get the same result.
I've also tried with:
build job: "Build_Android_Matrix", propagate: false, wait: true, parameters: [
new hudson.plugins.matrix_configuration_parameter.MatrixCombinationsParameterValue
("paramFilter",
null,
['buildType=Release'])
]
but it fails because RejectedAccessException: Scripts not permitted to use new.
I'm almost sure that I'm not providing the combinations in the right way but I don't know what else I can try.
Update
After Christopher Orr answer I tried to set the parameters like this:
[$class: 'MatrixCombinationsParameterValue', name: 'paramFilter', description: null, combinations: ['buildType=Release,flavor=Italy']]]
with this as my Axes:
flavor: Germany Italy Mexico UnitedStates
buildType: Debug Release
And was not working because I forgot that I have also a Slaves Axis and that must be specified as well.
So this is what worked for me:
[$class: 'MatrixCombinationsParameterValue', combinations: ["buildType=Release,flavor=Italy,label=android"], description: '', name: 'paramFilter']
When you use the Matrix Combinations plugin from the web UI, you need to explicitly specify all of the combinations that you want to run. So in Pipeline you need to do the same, for example:
combinations: ['buildType=Release,flavor=beta',
'buildType=Release,flavor=production']
Order of parameters matters.

Resources