Issue
On running the below code, I am getting to the else
block of String comparison section.
But unable to get into the else
block of Boolean comparison section.
pipeline {
agent any
parameters{
string(
name: "s_Project_Branch_Name", defaultValue: "master",
description: "Enter Brnach Name")
}
stages {
stage('Example') {
input {
message "Proceed to Prod Deployment with ${params.s_Project_Branch_Name} branch?"
ok "Yes"
parameters {
string(name: 'PERSON', defaultValue: 'master', description: 'Who should I say hello to?')
booleanParam(name: "TOGGLE", defaultValue: false, description: "Check this box to Proceed.")
}
}
steps {
// echo "Hello, ${PERSON}, nice to meet you."
script{
echo 'String'
if ("${PERSON}" == "${params.s_Project_Branch_Name}") {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
currentBuild.result = "FAILURE"
}
}
script{
echo 'Boolean'
echo "${TOGGLE}"
if ("${TOGGLE}") {
echo 'I only execute if boolean true'
} else {
error('I only execute if boolean false')
currentBuild.result = "FAILURE"
}
}
}
}
}
}
Solution
When you do String interpolation "${TOGGLE}"
you will be getting a String, not a Boolean. If you want to get the Boolean value you can directly access the variable by doing params.TOGGLE
. So change the condition like below.
if (params.TOGGLE) {
echo 'I only execute if boolean true'
} else {
error('I only execute if boolean false')
currentBuild.result = "FAILURE"
}
OR
if ("${TOGGLE}" == "true") {
echo 'I only execute if boolean true'
} else {
error('I only execute if boolean false')
currentBuild.result = "FAILURE"
}
Answered By - ycr
Answer Checked By - Timothy Miller (JavaFixing Admin)