Tuesday, August 8, 2017

Jenkins Script Console and some Groovy Scripts

I am one of those jenkins users who use Jenkins Script Console  [1] extensively. I have been maintaining some of those scripts in a private repository, lately I came across this gist [2] by Damien Nozay which uses a much simpler way to share these scripts i.e. using gists :D
After forking it I have started adding my scripts in gists as well   https://gist.github.com/mubbashir/484903fda934aeea9f30 [3].

I found it quite useful e.g. lately I need to know the results of all the downstream jobs triggered by a certain job:
https://gist.github.com/mubbashir/#file-jobstatustriggeredbyupstreamcause

// author : Ahmed Mubbashir Khan
// Licensed under MIT
// ---------------------------------------------------------
// This script prints out information of last downstream job of Upstream job
// e.g. printInformationOfDownstreamJobs("ChangeListener", 11, "All Tests")
// will print all the downstream jobs invoked by ChangeListener build 11 in the view "All Tests"
// ---------------------------------------------------------
import hudson.model.*
//printInformationOfDownstreamJobs("ChangeListener", 11, "All Tests") 
 def printInformationOfDownstreamJobs(jobName, buildnumber, viewName){
  def build = Jenkins.getInstance().getItemByFullName(jobName).getBuildByNumber(buildnumber)
  println "${build.fullDisplayName} ${build.getCause(hudson.model.Cause.UpstreamCause).upstreamRun}"
  def cause_pattern = /.*${jobName}.*${buildnumber}.*/
  println "Cause pattern: ${cause_pattern}"
  def view = Hudson.instance.getView(viewName)
  def jobsByCause = view.getItems().findAll{job -> job.lastBuild != null &&
    job.lastBuild.getCause(hudson.model.Cause.UpstreamCause)!= null &&
    job.lastBuild.getCause(hudson.model.Cause.UpstreamCause).upstreamRun==~cause_pattern}
  jobsByCause.each{ d_build->
   // def d_build = job.lastBuild
    println("Build: ${d_build.lastBuild.fullDisplayName}->"+
     "result:${d_build.lastBuild.result}->${d_build.lastBuild.buildStatusSummary.message}, " +
     "(was triggered by:${d_build.lastBuild.getCause(hudson.model.Cause.UpstreamCause).upstreamRun})" )
  }
}
Or information about the builds in a new e.g. node on  which they were executed along with the time they took:
https://gist.github.com/mubbashir/#file-jobs-in-view-with-duration-label-groovy 

// author : Ahmed Mubbashir Khan
// ---------------------------------------------------------
// This script goes through all the jobs in a view, filters succesful and failed jobs seprately
// Then prints outs them along with the time they took
// ---------------------------------------------------------
import hudson.model.*
def str_view = "Pipeline Tests"
def view = Hudson.instance.getView(str_view)
def successfulJobs = view.getItems().findAll{job -> job.lastBuild != null && job.lastBuild.result == hudson.model.Result.SUCCESS}
def faildJobs = view.getItems().findAll{job -> job.lastBuild != null && job.lastBuild.result == hudson.model.Result.FAILURE}
def disabledJob = view.getItems().findAll{job -> job.disabled == true}
def enabledJob = view.getItems().findAll{job -> job.disabled != true}
println "Total jobs: " + view.getItems().size +" Successful: " +successfulJobs.size+
  " Failed: " + faildJobs.size + " Enabled jobs: " +enabledJob.size + " Disabled jobs: " +disabledJob.size 
println "Current Successful job:"
successfulJobs.each{job -> printInfo(job)}
println "Current Fail job:"
faildJobs.each{job -> printInfo(job)}
println "Current disabled job:"
disabledJob.each{job -> printInfo(job)}
println "Current enabled job:"
enabledJob.each{job -> printInfo(job)}

def printInfo(job){
  println "Job: ${job.name} build on ${job.getAssignedLabelString()}, "+
    "took ${job.lastBuild.getDurationString()} to build, is disabled : ${job.disabled}"
}


[1] https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console
[2] https://gist.github.com/dnozay/e7afcf7a7dd8f73a4e05
[3] https://gist.github.com/mubbashir/484903fda934aeea9f30