I want to concatenate the variable %dispatcher% = %region%COGAPP%env%:10000based on the Jenkins parameter selection. region is passed correctly but env is not. env has an additional loop so it returns a specific output (Example, if selected Dev in UI, return 901).
for (b in environmentGroup) {
String envX = ""
if ("$b" == "Dev"){
envX = "901"
}
else if ("$b" == "Test"){
envX = "801"
}
else if ("$b" == "Prod"){
envX = "101"
}
Jenkins Cosole Output return dispatcher = USOHCOGAPP:10000 but it should return dispatcher = USOHCOGAPP901:10000
[Cube@2] Running batch script
C:\Jenkins\workspace\Pipelines\Cube@2>rem @echo off
C:\Jenkins\workspace\Pipelines\Cube@2>set /p region= USOH
USOH
C:\Jenkins\workspace\Pipelines\Cube@2>set /p env= 901
901
C:\Jenkins\workspace\Pipelines\Cube@2>set /p cube= abc
abc
C:\Jenkins\workspace\Pipelines\Cube@2>set dispatcher = USOHCOGAPP:10000
Full Code
/**************** Variable Definition ****************/
// Create string array of environments based on user input
def String[] environmentGroup = "${params.environment}".split(',')
// Create string array of regions based on user input
def String[] regionGroup = "${params.region}".split(',')
// Create string array of regions based on user input
def String[] cubeGroup = "${params.cube}".split(',')
/**************** Pipeline Definition ****************/
pipeline {
// Allows pipeline to run on any available node if not explicitly defined
agent any
// Create separate stages in build pipeline
stages {
// Define first stage in pipeline: review user inputs and output to Jenkins console
stage ('Parameter Validation') {
// Define steps in the 'Parameter Validation' stage
steps {
script {
// Error handling - if any parameter is left blank, the build will automatically fail
if ("${params.environment}" == '' || "${params.region}" == '' || "${params.cube}" == '') {
error("Build failed because a required parameter was not selected.\n All available parameters in this pipeline are required.")
}
}
// Output parameters selected at time of build to Jenkins console
echo '------------------------------------------------------------------------------------------------------------------'
echo "Checking environment input... user selected: ${params.environment}"
echo "Checking region input... user selected: ${params.region}"
echo "Checking cube input(s)... user selected: ${params.cube}"
echo '------------------------------------------------------------------------------------------------------------------'
}
}
// Define second stage in pipeline: access NetApp and download file to local workspace
stage ('Execute Script') {
// Define steps in the 'Execute Script Stage'
steps {
// Script block to encapsulate three nested loops
script {
// Loop through regions in the regionGroup String Array
for (a in regionGroup) {
// Loop through regions in the environmentGroup String Array
for (b in environmentGroup) {
String envX = ""
if ("$b" == "Dev"){
envX = "901"
}
else if ("$b" == "Test"){
envX = "801"
}
else if ("$b" == "Prod"){
envX = "101"
}
// Loop through regions in the serverGroup String Array
for (x in cubeGroup) {
// Two echos: one for spacing in the log and the other to detail what will occur in each block
echo "$x --------------------------------------------------------------------------------------------------\n"
echo "The script execution in this block will refresh the $x cube in the $b $a environment."
// Node label determines where the script will execute
node(label: "DEFRCOGAPP901") {
// Execute Windows Batch File
bat """
rem @echo off
set /p region= ${params.region}
set /p env= $envX
set /p cube= $x
set dispatcher = %region%COGAPP%env%:10000
echo %dispatcher%
"""
}
}
}
}
}
}
}
}
}