How to Create a Boolean Parameter in Jenkins

Sometimes you might be required to deal with boolean parameters in Jenkins. We will see how to create a boolean parameter in Jenkins in this post and then see how to use it in your code to take user input.

Pretty sure you have aced things when it comes to creating parameters in Jenkins. But sometimes things might get tricky when there is a boolean parameter involved. I know this because I struggled with it when I came along a certain requirement. The tricky part is not with the creation of the parameter on Jenkins, that’s fairly easy, but the real issue you might face could be whilst writing the code that should actually take the user input. Don’t worry, I will cover that part as well.

boolean meme

Let us see how to create a boolean parameter in Jenkins and take inputs from a user.

Steps on How to Create a Boolean Parameter in Jenkins

Following the same old Jenkins Parameter tutorial to navigate to the project first.

Step 1: Click on Configure.

click to configure jenkins project

Step 2: Then look for “This project is parameterized” checkbox. Then check the checkbox.

add parameter option in jenkins

Step 3: Click on Add Parameter and select boolean parameter from the dropdown as shown in the image below:

boolean parameter option in jenkins

A small section will open up for you to enter the details in.

Step 4: Enter the name of your checkbox in the name section. You can provide a description as well for your users to understand what the field is all about.

boolean parameter requirements in jenkins

You need to also use the same name in the goals and options section to take the user input from your code.

Step 5: Just navigate to the Build Section and type the following:

-Drun = "$run"

in the Goals and options textbox where run is nothing but the variable, you have chosen in your code. Ensure the usage of -D and $ thus allowing user input possible. Here is how it will look after you have made the changes.

-D and $ usage in Jenkins

Things prefixing .xml is nothing but the name of your suite file you are running using Maven.

Step 6: Just click on Apply and Save after you are done with the above changes.

click on apply then save in jenkins

Updating Code to Reflect Boolean Parameter

Now you need to make one final change which is the major one that would actually make things happen. It is supposed to be in your code which you would then git bash to reflect.

Step 7: In your code you need to write the following to take user input:

boolean status = Boolean.getBoolean(run);

The above code is very similar to the System.getProperty(String s) that we had seen in the add a parameter article.

You could then use the status variable as is required further. Here’s an example:

if(!status) { 
    do something 
} else { 
    do something else
}

Now once you are done with the code you need to git bash to make it reflect in Jenkins. Here’s a short summary of the process:

  • git add –all
  • git commit -m “Your message here”
  • git push

You can also check out this article on how to git bash your code into BitBucket.

Step 8: Once you are done doing that you and once you have saved your Jenkins job by using Apply and Save, just click on Build with Parameters to see the changes:

click on build with parameters

That’s it. The next screen might look like something like this:

Step 9: Go ahead and check or uncheck if you wish to pass true or false value to your code and then click on build.

Enjoy running your build. 🙂

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

2 Responses

Leave a Reply