So, It all started when I wanted to know if users are running their jobs which they no longer require. But I reliased I had infront of me approx 7000 jobs to process and find out who all are triggering their jobs by using SCM change or timer or they themselves trigger it and these jobs are failing all the time.
Getting details of all the jobs manually was out of question. Then I thought of trying out Jenkins plugin like Build Metrics, It gives good overview of jobs but I wanted something more like recipents, last successful build number, cause or culprits for this job. That’s how I ended up writing groovy script for Jenkins.
We can get the details of one more build jobs from jenkins by writing groovy scripts. Jenkins has script console option to execute groovy scripts on its server.
So let’s get started…
Here we will try to get the details of only job. We have to import jenkins.model.jenkins to use method like getItemByFullName() which will return item. We will use this item to get details of last build.
Method getLastBuild() will return RunT which is nothing but a object type. Every time a job runs it is recorded as runnable object.
import jenkins.model.Jenkins
name = "test_master_builder_project"
//If we want to add more then one job
def items = new LinkedHashSet();
def job = Hudson.instance.getJob(name)
items.add(job);
items.each { item ->
try {
def job_data = Jenkins.instance.getItemByFullName(item.fullName)
println 'Job: ' + item.fullName
if (job_data.getLastBuild()) {
last_job_num = job_data.getLastBuild().getNumber()
def upStreamBuild = Jenkins.getInstance().getItemByFullName(item.fullName).getBuildByNumber(last_job_num)
println 'LastBuildNumer: ' + last_job_num
println "LastBuildTime: ${upStreamBuild.getTime()}"
} else {
println 'LastBuildNumer: Null'
}
} catch (Exception e) {
println ' Ignoring exception ' + e
}
}
Here we have to make sure the job name that we pass may have few builds or may not have any builds triggered at all. We check if job has any last build, Only if it has atleast one build job then only we go ahead get the details.
If we execute the above script in Jenkins script console. We will get something like this:
Job: test_master_builder_project
LastBuildNumer: 49
LastBuildTime: Thu Aug 30 14:25:06 IST 2018
Now, we will try to get the last successful build details by using method getLastSuccessfulBuild() which will also return object RunT. We will have to check for the given build job if it has atleast one successful build.
if (job_data.getLastSuccessfulBuild()) {
println 'LastSuccessNumber: ' + job_data.getLastSuccessfulBuild().getNumber()
println 'LastSuccessResult: ' + job_data.getLastSuccessfulBuild().result
} else {
println 'LastSuccessNumber: Null'
println 'LastSuccessResult: Null'
}
In order get what was the cause for the last build. We will have to use some already provided classes to get the cause if it is of type trigger, scm, user or upstream (triggered by some other job). We will put all this together in a function called findCause().
def findCause(upStreamBuild) {
//Check if the build was triggered by SCM change
scmCause = upStreamBuild.getCause(hudson.triggers.SCMTrigger.SCMTriggerCause)
if (scmCause != null) {
return scmCause.getShortDescription()
}
//Check if the build was triggered by timer
timerCause = upStreamBuild.getCause(hudson.triggers.TimerTrigger.TimerTriggerCause)
if (timerCause != null) {
return timerCause.getShortDescription()
}
//Check if the build was triggered by some jenkins user
usercause = upStreamBuild.getCause(hudson.model.Cause.UserIdCause.class)
if (usercause != null) {
return usercause.getUserId()
}
//Check if the build was triggered by some jenkins project(job)
upstreamcause = upStreamBuild.getCause(hudson.model.Cause.UpstreamCause.class)
if (upstreamcause != null) {
job = Jenkins.getInstance().getItemByFullName(upstreamcause.getUpstreamProject(), hudson.model.Job.class)
if (job != null) {
upstream = job.getBuildByNumber(upstreamcause.getUpstreamBuild())
if (upstream != null) {
return upstream
}
}
}
return;
}
For more info read: https://javadoc.jenkins-ci.org/hudson/model/Cause.UserCause.html
We call this function in our script and output will say something like Started by an SCM change if it is triggered by SCM change.
def upStreamBuild = Jenkins.getInstance().getItemByFullName(item.fullName).getBuildByNumber(last_job_num)
findCause(upStreamBuild)
To get the recipient list we will import hudson.plugins.emailext.* because in Jenkins I have a plugin called ExtendedEmailPublisher in job config where I have enetered the recipent email id’s. So using it to fetch the details.
import hudson.plugins.emailext.*
for(publisher in item.publishersList) {
if(publisher instanceof ExtendedEmailPublisher) {
if(publisher.recipientList) {
println 'recipients: ' + publisher.recipientList
} else {
println 'recipients: Null'
}
}
}
We will put all this code together.
// Name: get_job_details_by_name.groovy
// Author: Garima Damani
// ---------------------------------------------------------------
// This script gets the below job details with the given job name:
// *Full name
// *Last build number
// *Last build time
// *Last build cause
// *Last success build number
// *Last result
// *Get recipient list
// ---------------------------------------------------------------
import hudson.model.*
import hudson.maven.*
import hudson.tasks.*
import jenkins.model.Jenkins
import hudson.maven.reporters.*
import hudson.plugins.emailext.*
name = "test_master_builder_project"
//If we want to add more then one job
def items = new LinkedHashSet();
def job = Hudson.instance.getJob(name)
items.add(job);
def findCause(upStreamBuild) {
//Check if the build was triggered by SCM change
scmCause = upStreamBuild.getCause(hudson.triggers.SCMTrigger.SCMTriggerCause)
if (scmCause != null) {
return scmCause.getShortDescription()
}
//Check if the build was triggered by timer
timerCause = upStreamBuild.getCause(hudson.triggers.TimerTrigger.TimerTriggerCause)
if (timerCause != null) {
return timerCause.getShortDescription()
}
//Check if the build was triggered by some jenkins user
usercause = upStreamBuild.getCause(hudson.model.Cause.UserIdCause.class)
if (usercause != null) {
return usercause.getUserId()
}
//Check if the build was triggered by some jenkins project(job)
upstreamcause = upStreamBuild.getCause(hudson.model.Cause.UpstreamCause.class)
if (upstreamcause != null) {
job = Jenkins.getInstance().getItemByFullName(upstreamcause.getUpstreamProject(), hudson.model.Job.class)
if (job != null) {
upstream = job.getBuildByNumber(upstreamcause.getUpstreamBuild())
if (upstream != null) {
return upstream
}
}
}
return;
}
// Iterate through each item.
items.each { item ->
try {
def job_data = Jenkins.instance.getItemByFullName(item.fullName)
println 'Job: ' + item.fullName
//Check if job had atleast one build done
if (job_data.getLastBuild()) {
last_job_num = job_data.getLastBuild().getNumber()
def upStreamBuild = Jenkins.getInstance().getItemByFullName(item.fullName).getBuildByNumber(last_job_num)
println 'LastBuildNumer: ' + last_job_num
println "LastBuildTime: ${upStreamBuild.getTime()}"
println 'LastBuildCause: ' + findCause(upStreamBuild)
//Check if job had atleast one successful build
if (job_data.getLastSuccessfulBuild()) {
println 'LastSuccessNumber: ' + job_data.getLastSuccessfulBuild().getNumber()
println 'LastSuccessResult: ' + job_data.getLastSuccessfulBuild().result
} else {
println 'LastSuccessNumber: Null'
println 'LastSuccessResult: Null'
}
} else {
println 'LastBuildNumer: Null'
}
//Get the list of recipient
if(!(item instanceof ExternalJob)) {
if(item instanceof MavenModuleSet) {
for(reporter in item.reporters) {
if(reporter instanceof MavenMailer) {
if(reporter.recipients) {
println 'recipients: ' + reporter.recipients
} else {
println 'recipients: Null'
}
}
}
}
for(publisher in item.publishersList) {
if(publisher instanceof Mailer) {
if(publisher.recipients) {
println 'recipients: ' + publisher.recipients
} else {
println 'recipients: Null'
}
}
else if(publisher instanceof ExtendedEmailPublisher) {
if(publisher.recipientList) {
println 'recipients: ' + publisher.recipientList
} else {
println 'recipients: Null'
}
}
}
}
} catch (Exception e) {
println ' Ignoring exception ' + eVF DSGB NH FV N
}
}
return;
Output should look something like:
Job: test_master_builder_project
LastBuildNumer: 49
LastBuildTime: Thu Aug 30 14:25:06 IST 2018
LastBuildCause: Started by an SCM change
LastSuccessNumber: 49
LastSuccessResult: SUCCESS
recipients: g_abc@gmail.com
评论前必须登录!
注册