Jenkins pipeline, groovy map array as choises - jenkins

How can I pass the array variable into the global variable in groovy?
It works perfectly without if-else.
choiceArray = []
if (appName == 'ms'){
['78', '99', '10'].each {
"${choiceArray}" << it
}
}
else if (appName == 'ms2'){
['12', '34', '56'].each {
"${choiceArray}" << it
}
}
pipeline {
parameters {
string (name: 'Branch', defaultValue: 'master', description: 'Select Branch')
choice(name: 'Environment', choices: choiceArray, description: 'Choose Environment')

if(...){
choiceArray.addAll(['78', '99', '10'])
}

Related

Loop through multi selection value of Jekins Parameter BooleanParam

I have on-premise Jenkins node slave that are running. The value of each param is the nodename. For example the nodename I have is (value1,value2,value3)
Suppose a user select a value1 and value2 in the parameter options in the checkbox, I want to be able to loop the selected params and pass the value to the node($selected).
That means Jenkins will connect to value1 and value2 node. Any Idea how to do this?
The code is here:
#!/usr/bin/env groovy
properties([
parameters([
booleanParam(name: 'value1', defaultValue: false, description: '') ,
booleanParam(name: 'value2', defaultValue: false, description: '') ,
booleanParam(name: 'value3', defaultValue: false, description: '')
])
])
stage('stash'){
Loop here //
node('$selected'){
}
}
Do you want to do something like the below?
node {
properties([
parameters([
booleanParam(name: 'value1', defaultValue: false, description: '') ,
booleanParam(name: 'value2', defaultValue: false, description: '') ,
booleanParam(name: 'value3', defaultValue: false, description: '')
])
])
stage('Stash') {
def list = []
if("$value1" == "true") {
list.add("value1")
}
if("$value2" == "true") {
list.add("value2")
}
if("$value3" == "true") {
list.add("value3")
}
list.each { node ->
echo "$node"
node('$node'){}
}
}
}

Jenkinsfile with list of all parameter values or single value from parameter list

I want to run jenkins job using jenkinsfile with list of all parameters value or with individual value from parameter list.
def deploy(env) {
step([$class: 'UCDeployPublisher',
siteName: siteName,
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
deployApp: appName,
deployEnv: 'DEV',
deployVersions: "${compName}:${version}",
deployProc: simpleDeploy,
deployOnlyChanged: false,
deployReqProps: "ID=${params.ID}"
]])
CHOICES = [ 'id1', 'id2', 'id3', 'id4', 'id5' ]
PARAMETERS = CHOICES + "all"
parameters {
choice(
name: 'ID',
choices: PARAMETERS,
)
stage (DEV') {
steps {
script {
if (params.ID == "all"){
CHOICES.each {
echo "$it"
}
deploy('devl') ===> this will call my deploy function
}
else {
echo "$params.ID"
deploy('devl') ===> this will call my deploy function
}
}
}
}
I was able to run job using bye selecting each value from droplist. But I also want to the run the job with all values from ID list. I tried with all but it is not taking all the the values 'id1', 'id2', 'id3', 'id4', 'id5'
You would need to define the choices in a var outside of the parameter and then use that as choices e.g.
CHOICES = [ 'id1', 'id2', 'id3', 'id4', 'id5' ]
PARAMETERS = CHOICES + "all"
pipeline {
agent any
parameters {
choice(name: "ID", choices: PARAMETERS )
}
stages {
stage('Test') {
steps {
script {
if (params.ID == "all"){
CHOICES.each {
echo "$it"
}
}
else {
echo "$params.ID"
}
}
}
}
}
}

How to pass parameter value to stage build in Jenkins pipeline?

I have a pipeline created which takes the parameter value from the user. I want to trigger a jenkin job using this parameter.
How can I pass the parameter value to the build's parameter.
Here is my code:
pipeline {
agent any
parameters {
string(name: 'SYSTEM', defaultValue: '', description: 'Enter array. Example:SYS-123')
string(name: 'EMail', defaultValue: '', description: 'Enter email id')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.SYSTEM}"
echo "Hello ${params.EMail}"
}
}
stage('core-rest-api-sanity') {
steps {
build job: 'xyz', parameters: [string(name: 'E-Mail', value: ${params.EMail}), string(name: 'SYSTEM', value: ${params.SYSTEM})]
}
}
}
}
In the above code, I am taking email and system details from the user. Then I want to trigger my job "xyz" which would require these parameter.
pipeline {
agent any
parameters {
string(name: 'SYSTEM', defaultValue: '', description: 'Enter array. Example:SYS-123')
string(name: 'EMail', defaultValue: '', description: 'Enter email id')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.SYSTEM}"
echo "Hello ${params.EMail}"
}
}
stage('core-rest-api-sanity') {
steps {
build job: 'xyz', parameters: [string(name: 'E-Mail', value: params.EMail),
string(name: 'SYSTEM', value: params.SYSTEM)]
}
}
}
}

Jenkins Date Parameter Plugin - How to use it in a Declarative Pipeline

What is the syntax for using the Date Parameter Plugin in a declarative pipeline.
So far I have tried this:
pipeline {
agent {
node {
label 'grange-jenkins-slave'
}
}
options { disableConcurrentBuilds() }
parameters {
date(name: 'EffectiveDate',
dateFormat: 'MMddyyy',
defaultValue: 'LocalDate.now();',
description: 'Effective Date',
trim: true)
file(name:'algo.xlsx', description:'Your algorithm file')
choice(name: 'currency',
choices: ['USD'],
description: 'Select a currency')
}
stages {
stage('genRates') {
steps {
script {
echo "test"
}
}
}
}
}
The error I get is WorkflowScript: 11: Invalid parameter type "date". Valid parameter types: [booleanParam, choice, credentials, file, text, password, run, string] # line 11, column 3.
you can define parameter as class DateParameterDefinition.
example:
properties([parameters([
string(name: 'somestring', defaultValue: 'somevalue'),
[$class: 'DateParameterDefinition',
name: 'somedate',
dateFormat: 'yyyyMMdd',
defaultValue: 'LocalDate.now()']
])])
pipeline {
...
}
I did not use date parameter plugin as I didn't find any example how to use it. I resolved this in a different way.
import java.text.SimpleDateFormat
def sdf = new SimpleDateFormat("yyyyMMdd")
def dateDefaultValue = sdf.format(new Date())
pipeline {
parameters {
string(name: 'SOMEDATE', defaultValue: "${dateDefaultValue}", description: 'Default value is current date in the format YYYYmmdd', trim: true)
}
.....
.....
}

Version Number Plugin in Jenkins declarative pipeline

I'm trying to use version number plugin to format a version number for our
packages.
From some reason the placement for the version variable doesn't work
and when I echo the following I only get the build number, for instance: "...54"
def Version_Major = '1'
def Version_Minor = '0'
def Version_Patch = '0'
pipeline {
environment {
VERSION = VersionNumber([
versionNumberString: '${Version_Major}.${Version_Minor}.${Version_Patch}.${BUILD_NUMBER}',
worstResultForIncrement: 'SUCCESS'
]);
}
stage ('Restore packages'){
steps {
script{
echo "${VERSION}"
}
}
}
}
Edit: It does look like an issue with the plugin usage since this works:
properties([
parameters([
string(name: 'Version_Major', defaultValue: '1', description: 'Version Major'),
string(name: 'Version_Minor', defaultValue: '0', description: 'Version Minor'),
string(name: 'Version_Patch', defaultValue: '0', description: 'Version Patch')
])
])
pipeline {
agent any
environment {
VERSION = "${params.Version_Major}.${params.Version_Minor}.${params.Version_Patch}.${BUILD_NUMBER}"
}
stages{
stage ('Test'){
steps {
echo "${VERSION}"
}
}
}
}
You must define the variables inside the pipeline.
Try this:
pipeline {
environment {
Version_Major = '1'
Version_Minor = '0'
Version_Patch = '0'
VERSION = VersionNumber([
versionNumberString: '${Version_Major}.${Version_Minor}.${Version_Patch}.${BUILD_NUMBER}',
worstResultForIncrement: 'SUCCESS'
]);
}
stage ('Restore packages'){
steps {
script{
echo "${VERSION}"
}
}
}
}
If you need to use a parameter instead that's also possible via:
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
usage:
"Hello ${params.PERSON}"

Resources