How to get Jenkins build job details?

Garima Damani
FAUN — Developer Community 🐾
4 min readSep 8, 2018

--

Source: Jenkins + Groovy

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.Jenkinsname = "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.

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

Disclaimer: Views displayed in the blog are mine and not my employers. Please test code before running.

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--