Sunday, September 9, 2018

Jenkins Utils

Hello Techienthu! This post is all about, how to use Jenkinsutils


Jenkinsutils works based on API called jenkins-client. Jenkins API client for java uses REST API of jenkins. By using jenkins-client we can do the below mentioned.
  • Can trigger the builds.
  • Extract information about jobs or builds.
  • This API doesn't support Jenkins version 2
Things to know before proceed
  • We are all know that Jenkins is a continuous integration tool. It can communicate with the source code management tools and can pull the code from those repositories automatically. It can execute that code in different environments with the concept of master and slave.
  • In order to do that task, Jenkins uses the concept called Jobs.
  • A single Jenkins server can connect with number of slaves and can contain number of jobs in number of views.
  • A single view can contain multiple jobs and every job contains number of builds.
The below program is divided into two sections.
  • Extracting job or builds details from Jenkins using jenkins-client API
  • Parsing the extracted data into a HTML file using Jsoup
Explanation
  • The com.offbytwo.jenkins.JenkinsServer class provides the main entry point into the API. You can create a reference to the Jenkins server given its location and (optionally) a username and password
  • JenkinsServer jenkins = new JenkinsServer(new URI("http://localhost:8080/jenkins"), "admin", "password");
  • You can get all of the currently defined jobs in jenkins or from a specific view. It returns a map of job names
  • Map jobs = jenkins.getJobs();
  • The JobWithDetails class provides you with access to the list of builds (and related information such as the first, last, successful, etc) and upstream and downstream projects.
  • JobWithDetails job = jobs.get("My Job").details();
  • Other methods used
  • Method Useage
    com.offbytwo.jenkins.model.JobWithDetails.getBuilds()
    Returns the number of build s available in job
    com.offbytwo.jenkins.model.BuildWithDetails.getResult()
    Returns the status of the specific build

package com.jenkins.util.util_jenkins;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;

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

public class AppTest2 {

 private static String baseURL = "jenkinsurl";
 private static String viewName = "Viewname";
 private static String userName = "jenkinsusername";
 private static String passWord = "jenkinspassword";

 public static void main(String h[]) throws URISyntaxException, IOException{

  JobWithDetails jobdetails = null;
  ArrayList<Object> JD = null;
  ArrayList<Object> DT = null;
  ArrayList<ArrayList<Object>> DETAILS = null;

  JenkinsServer jenkins = new JenkinsServer(new URI(baseURL+"/"+viewName), userName, passWord);

  int pass=0, fail=0, abort=0;
  JD = new ArrayList<Object>();
  DETAILS = new ArrayList<ArrayList<Object>>();
  Map<String, Job> jobs = jenkins.getJobs();

  for(String job:jobs.keySet()){

   DT = new ArrayList<Object>();
   jobdetails = jobs.get(job).details();
   DT.add(jobdetails.getBuilds().get(0).getNumber());
   DT.add(jobdetails.getBuilds().size());

   for(int i=0;i<jobdetails.getBuilds().size();i++){

    if(jobdetails.getBuilds().get(i).details().getResult().toString().equalsIgnoreCase("SUCCESS")){
     pass++;
    }
    if(jobdetails.getBuilds().get(i).details().getResult().toString().equalsIgnoreCase("FAILURE")){
     fail++;
    }
   }
   if((pass+fail)!=jobdetails.getBuilds().size()){
    abort = jobdetails.getBuilds().size()-(pass+fail);
   }
   DT.add(pass);
   DT.add(fail);
   DT.add(abort);
   JD.add(jobdetails.getDisplayName());
   DETAILS.add(DT);
   pass=0;
   fail=0;
   abort=0;
  }
  createHTMLFile(JD,DETAILS);
  System.out.println("Execution done..HTML file created");
 }

 public static void createHTMLFile(ArrayList<Object> jobs,ArrayList<ArrayList<Object>> data) throws IOException{
  File f = new File("d://template.html");
  FileWriter fw = new FileWriter(f);
  int j = 0;
  Document doc = Jsoup.parse("<html>"
    + "<head></head>"
    + "<body class='body-styles-cls'>"
    + "<table border='3'><tbody id='tid'>"
    + "<th bgcolor='4771F3'>JOB NAME</th>"
    + "<th bgcolor='4771F3'>#BUILDS</th>"
    + "<th bgcolor='4771F3'>#AVAILABLE BUILDS</th>"
    + "<th bgcolor='4771F3'>#PASS</th>"
    + "<th bgcolor='4771F3'>#FAIL</th>"
    + "<th bgcolor='4771F3'>#ABORT</th>");

  Element div = doc.getElementById("tid");
  for(Object jobName:jobs){
   ArrayList<Object> dataReturned = data.get(j);
   j++;
   div.append("<tr><td bgcolor='F496F4'>"+jobName+"<td bgcolor='F496F4'>"
     +dataReturned.get(0)+"<td bgcolor='F496F4'>"
     +dataReturned.get(1)+"<td bgcolor='green'>"
     +dataReturned.get(2)+"<td bgcolor='red'>"
     +dataReturned.get(3)+"<td bgcolor='yellow'>"
     +dataReturned.get(4));
  }
  fw.write(doc.toString());
  fw.flush();
  fw.close();
 }

}