Reputation: 696
This condition in my script always gets evaluated as true and prints "Yes equal - running the stage"
stage('test cond'){
if(env.BUILD_TESTING2 == true){
echo "Yes equal - running the stage"
} else {
echo "Not equal - skipping the stage"
}
}
Even if I start the build by setting env.BUILD_TESTING2 = false it still enters the condition and prints "Yes equal - running the stage".
I also tried this syntax:
stage('test cond'){
if(env.BUILD_TESTING2){
echo "Yes equal - running the stage"
} else {
echo "Not equal - skipping the stage"
}
}
But it also still always gets evaluated as true.
How can I write a conditional step with boolean parameter in Jenkins scripted pipeline ?
Upvotes: 27
Views: 68444
Reputation: 810
If you're using booleanParam, the cleanest way that I know is to use when directive with expression.
stage('test cond') {
when { expression { return params.BUILD_TESTING2 } }
steps {
sh 'echo "running the stage"'
}
}
Upvotes: 13
Reputation: 632
Better reference parameters by params instead of env, this way they have the correct object type. So use:
stage('test cond') {
if(params.BUILD_TESTING2) {
echo "Yes equal - running the stage"
} else {
echo "Not equal - skipping the stage"
}
}
Upvotes: 19
Reputation: 893
You need to convert this environment variable (of type string) to boolean using toBoolean() function:
stage('test cond'){
if(env.BUILD_TESTING2.toBoolean()){
echo "Yes equal - running the stage"
} else {
echo "Not equal - skipping the stage"
}
}
Upvotes: 34