Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write conditional step with boolean parameter in jenkins scripted pipeline job?

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 ?

like image 889
Davis8988 Avatar asked Oct 18 '18 10:10

Davis8988


People also ask

How do you use a Boolean parameter in Jenkins 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.

Can we run a step conditionally in Jenkins?

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.

How do you pass a parameter in Jenkins pipeline script?

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.

How do I add a choice parameter in Jenkins?

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.


2 Answers

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"
    }
}
like image 70
Krzysztof Błażełek Avatar answered Sep 16 '22 12:09

Krzysztof Błażełek


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"
    }
}
like image 26
Welt Fahr Avatar answered Sep 18 '22 12:09

Welt Fahr