Skip to content

Instantly share code, notes, and snippets.

@okgolove
Created June 3, 2019 12:48
Show Gist options
  • Select an option

  • Save okgolove/ccfe9b560ce9478b7016709071543065 to your computer and use it in GitHub Desktop.

Select an option

Save okgolove/ccfe9b560ce9478b7016709071543065 to your computer and use it in GitHub Desktop.
Jenkins useful groovy commands
//This script allows you to get build information for selected jobs
/*
You can also add closures to filter the jobs or the builds...
.find{job->job.name.contains('dsl')}
.find{ [whatever]}
*/
Jenkins.instance.getAllItems(Job).each{
def jobBuilds=it.getBuilds()
//for each of such jobs we can get all the builds (or you can limit the number at your convenience)
jobBuilds.each { build ->
def runningSince = groovy.time.TimeCategory.minus( new Date(), build.getTime() )
def currentStatus = build.buildStatusSummary.message
def cause = build.getCauses()[0] //we keep the first cause
//This is a simple case where we want to get information on the cause if the build was
//triggered by an user
def user = cause instanceof Cause.UserIdCause? cause.getUserId():""
//This is an easy way to show the information on screen but can be changed at convenience
println "Cause: ${cause} | User: ${user}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment