How to get Jenkins build job details?

Garima Damani
Sep 8, 2018 · 4 min read
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.Jenkins

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()
}

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.*

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! ⬇

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Garima Damani

Written by

Garima is a production engineer for an online transportation network company. Open to learning

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade