Trigger a Jenkins Build Programmatically using Java


Trigger a Jenkins Build Programmatically using Java



As we all know Jenkins is a popular CI Tools and can be integrated with almost all the other popular testing and DevOps tool.

In this article, we will learn how we can run our Jenkins job from JAVA Code.


We need to follow the below steps to run Jenkins Job from JAVA Code.

1)   Login into Jenkins with your admin user id/password and click on id present on the top right of the screen.


2)   Click on Configure then click on Add New Token option inside API Token.



3)   Click on Generate, One unique token id will be generated for your id. Copy this token id for your reference.

4)   Now either create or open existing Jenkins job which we want to execute through our code.


5)   Click on Trigger build remotely inside Build Triggers and provide the token id generated in step 2.



6)   Click on Apply and save the Jenkins Job.

7)   Now we need to use the below code.
Please Note in URL we first need to provide the exact path of Jenkins job followed by build?token=your token id.

 Now once we execute the above java code, we would be able to see that Jenkin's job is executed successfully.





SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post
October 1, 2019 at 4:30 PM

Hi ..im running my program as 'Run as Java application' given all the proper details but getting ....
Job status is 403
execution ended
please help

Reply
avatar
September 15, 2021 at 6:11 PM

Hi Ankur our Jenkins is hosted on cloud so only passing authentication value will work in URL?

Reply
avatar