How to Add a Parameter in Jenkins
If you are trying to add a parameter in Jenkins, you have come to the right place. Check out how to add a parameter in Jenkins herein.
At times you might be required to take inputs from the user before proceeding with your tests. For e.g. letting a user or client decide which URL to run the code at, or a particular id or name to continue the whole testing process with. During those times, a parameter at the very beginning of the test comes in handy.
So every time your client wants you to add an input parameter, please don’t think about suicide just yet. The solution is easy.
We are assuming you already have some code with you and the requirement is to take some input from the end user.
Here are the steps on How to Add a Parameter in Jenkins.
Steps on How to Add a Parameter in Jenkins
Step 1: First navigate to your project. It must look like something as shown in the diagram below, with the name of your project on the right-hand side.
Step 2: Click on Configure.
Step 3: In the next screen look for the checkbox which says “This project is parameterized”. Then check the checkbox.
Step 4: Then click on Add Parameter.
I am going to make use of String Parameter for my project. So I am gonna click on that:
Step 5: In the next block, you need to specify the name of the parameter. Then provide the description for the same.
NOTE: You can choose to check the box for Trim the String if you want your String to be wished. But that is something you could do in your code too. So it is up to you. Trim simply removes white spaces from both your String’s ends.
Step 6: Scroll down to the Build section and type the following:
-Did = “$id”
in the Goals and options textbox alongside mvn clean install command of maven. If you wish to know more about -D, check out our new maven job in jenkins article.
You can replace id with the name of your parameter.
Updating Code to Take String Parameter
Step 7: Now your code needs to be updated so as if asking for you to enter an input. This is what should go inside your code in the very beginning:
String id = System.getProperty("id");
if (id == null) {
id = "ABCD";
}
What if the parameter is left null? What would happen then? I have handled it in the second line of the above code by passing “ABCD” in that case. You can tweak it as you want.
The first line on the other hand will be the actual line responsible for taking inputs from the end user.
Step 8: You need to push your code so that the above code changes get reflected in Jenkins. Don’t know how to do that? Check this article out.
Here’s what it would entail:
- git add –all
- git commit -m “Your message here”
- git push
Step 9: Once you are done click on Apply.
Step 10: Then go for Save. It will navigate you to your project screen.
Step 11: It will take you to the following screen:
Step 12: Click on Build with Parameters. The next screen will be reflecting your input textbox that you had created:
There you go!
The input parameter has begun to reflect. That easy!
Later.
1 Response
[…] 0 […]