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 ?
Step 1: Click on Configure. Step 2: Then look for “This project is parameterized” checkbox. Then check the checkbox. A small section will open up for you to enter the details in.
The Conditional BuildStep plugin lets users add conditional logic to Freestyle jobs from within the Jenkins web UI. It does this by: Adding two types of Conditional BuildStep ("Single" and "Multiple") - these build steps contain one or more other build steps to be run when the configured condition is met.
Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.
Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.
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"
}
}
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"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With