Last active
September 17, 2019 13:22
-
-
Save daniilyar/eeec6516ddd8f2f9eb37 to your computer and use it in GitHub Desktop.
Script for Jenkins 'Total build status' job which displays all failed jobs with last changes, committers and links to job's console. It will fail if at least one other Jenkins job is failed. Usage: create Jenkins Freestyle job and add this script as "Execute system Groovy script" build step
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jenkins.* | |
import jenkins.model.* | |
import hudson.* | |
import hudson.model.* | |
import hudson.console.* | |
import groovy.time.* | |
import hudson.plugins.git.* | |
import java.text.SimpleDateFormat | |
import java.util.Date | |
static void main(String []args) { | |
Hudson hudson = Jenkins.getInstance() | |
def allBuilds = hudson.jobNames.collect { hudson.getJob(it) } | |
def failedBuilds = allBuilds.findAll { | |
it != null && it.lastBuild != null && it.lastBuild.result != null && !it.disabled && !it.lastBuild.building && it.lastBuild.result == Result.FAILURE | |
} | |
println "\n\nFAILED JOBS (${failedBuilds.size()}):\n" | |
failedBuilds.each { job -> | |
AbstractBuild lastBuild = job.lastBuild | |
Result lastBuildResult = lastBuild.result | |
String jobName = job.name | |
String buildNumber = lastBuild.number | |
println "============================================================================================" | |
link(out, "/job/$jobName/$buildNumber", "$jobName #$buildNumber"); print " - $lastBuildResult " | |
print "("; link(out, "/job/$jobName/$buildNumber/consoleFull", "CONSOLE"); print ")"; | |
// DY: to trigger builds we need a link sending POST requests to JENKINS_URL/job/JOB_NAME/build somehow | |
// link(out, "/job/$jobName/build?delay=0sec", job.getBuildNowText()); print "\n\n" | |
println "============================================================================================" | |
println "Committers since last non-broken build till now: ${lastBuild.culprits}" | |
// println "Build variables: ${lastBuild.buildVariables}" | |
getGitChangesets(lastBuild).each { chgset -> | |
TimeDuration duration = TimeCategory.minus(new Date(), new Date(chgset.timestamp)) | |
link(out, "/job/$jobName/$buildNumber/consoleFull", "${chgset.commitId.substring(0,7)}"); | |
print " <${chgset.author}> ${chgset.msg} (${duration.days}d ${duration.hours}h ${duration.minutes}min ago)\n" | |
// println "Affected files: " | |
// chgset.affectedFiles.each { println it.path; } | |
} | |
println "============================================================================================\n" | |
} | |
if(!failedBuilds.isEmpty()) { | |
throw new AbortException("Build failed") | |
} | |
return null | |
} | |
static void link(PrintStream out, String relativePath, String linkText) { | |
out.print(HyperlinkNote.encodeTo(relativePath, linkText)) | |
} | |
static List<GitChangeSet> getGitChangesets(AbstractBuild build) { | |
List<GitChangeSet> result = new ArrayList<GitChangeSet>(); | |
List<GitChangeSetList> changesets = build.changeSets | |
if (changesets.size() > 0) { | |
List<GitChangeSet> gitCommits = changesets.logs | |
gitCommits.each { commit -> | |
commit.each { chgset -> | |
result.add((GitChangeSet) chgset) | |
} | |
} | |
} | |
result.sort{a,b -> b.timestamp <=> a.timestamp} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script1.groovy: 66: unable to resolve class GitChangeSet