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
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
[1] https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console
[2] https://gist.github.com/dnozay/e7afcf7a7dd8f73a4e05
[3] https://gist.github.com/mubbashir/484903fda934aeea9f30
Update:
Updated https://gist.github.com/mubbashir/#file-jobstatustriggeredbyupstreamcause to list any downstream job which matches and upstream cause
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 dwonstream job of Upstream job
// e.g. printInformationOfDownstreamJobs("ChangeListner", 11, "All Tests")
// will print all the downstream jobs invodked by ChangeListner build 11 in the view "All Tests"
// ---------------------------------------------------------
import hudson.model.*
//printInformationOfDownstreamJobs("ChangeListner", 11, "All Tests")
def printInformationOfDownstreamJobs(jobName, buildnumber, viewName){
def upStreamBuild = Jenkins.getInstance().getItemByFullName(jobName).getBuildByNumber(buildnumber)
println "${upStreamBuild.fullDisplayName}" +
"${upStreamBuild.getCause(hudson.model.Cause.UpstreamCause).upstreamRun}"
def cause_pattern = /.*${jobName}.*${buildnumber}.*/
println "Cause pattern: ${cause_pattern}"
// Upated for any builds of the upstream job in a view from only the last build
def view = Hudson.instance.getView(viewName)
def buildsByCause = []
// For each item in the view
view.getItems().each{
def jobBuilds=it.getBuilds() // get all the builds
jobsBuildsByCause = jobBuilds.findAll { build ->
build != null &&
build.getCause(hudson.model.Cause.UpstreamCause)!= null &&
build.getCause(hudson.model.Cause.UpstreamCause).upstreamRun==~cause_pattern
}
buildsByCause.addAll(jobsBuildsByCause)
}
// printing information
buildsByCause.each{ d_build->
// def d_build = job.lastBuild
println("Build: ${d_build.fullDisplayName}->"+
"result:${d_build.result}->${d_build.buildStatusSummary.message}, " +
"(was triggered by:${d_build.getCause(hudson.model.Cause.UpstreamCause).upstreamRun})" )
}
}
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
Update:
Updated https://gist.github.com/mubbashir/#file-jobstatustriggeredbyupstreamcause to list any downstream job which matches and upstream cause
No comments:
Post a Comment