Jenkins JavaUtil

Rohan Ravi Yadav
1 min readMay 25, 2021

Jenkins util to get list of all jobs from a Jenkins and trigger all jobs. it is easy with ‘Jenkins API Client for Java’.

This library is just a piece of java code which uses the REST API of Jenkins. This means you can trigger builds, extract informations about jobs or builds etc.

If you are using maven build , please add below dependency in your pom.xml.

<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.8</version>
</dependency>

If Gradle

compile 'com.offbytwo.jenkins:jenkins-client:0.3.8'

Usage:

JenkinsServer jenkins = new JenkinsServer(new URI("http://localhost:8080/view/<ViewName>/"), "username", "password");Map<String, Job> jobs = jenkins.getJobs()This will give a map of all jobs present on given jenkins url location. Now you can do multiple operations for example:- trigger builds, extract informations about jobs or builds etc

Example 1:

you want details of a particular job and build it:

JobWithDetails job = jobs.get("Job name here").details();//Build jobs
job.build();
//Build Jobs with parameter
Map <String, String> map=new <String, String>HashMap();
map.put("param1","value1");
map.put("param2","value2");
job.build(param);

Example 2:

Now I am want to build all jobs so how to do it

JenkinsServer jenkins = new JenkinsServer(new URI("http://localhost:8080/view/<ViewName>/"), "username", "password");//Jobs map to get all jobsMap<String, Job> jobs = jenkins.getJobs();//Build Jobs with parameter for example environment ,browser, os
Map <String, String> map=new <String, String>HashMap();
map.put("param1","value1");
map.put("param2","value2");//Iterate Jobs Map and build each jobs with parameter set in Map "map" for ( Map.Entry<String, Job> entry : jobs.entrySet()) {
if (entry.getValue().details().isBuildable()){
entry.getValue().details().build(map));
}

Reference : https://github.com/jenkinsci/java-client-api

Thanks for visiting this. hope it is useful.

--

--

Rohan Ravi Yadav

I am Test automation engineer from India , Currently working with LogMeIn as Senior SDET in Frontend & backend automation.