user479151
user479151

Reputation: 105

Get build information from Jenkins API

I'm writing a Jenkins plugin and I want to retrieve last build information (number, timestamp) for a given job from Jenkins api. I can do following REST call and obtain it.

<url_to_jenkins>job/<job name>/api/json?tree=builds[number,status,timestamp,id,result]

Since my plugin is also deployed inside Jenkins is there a way to get this info by calling direct JAVA api instead of this REST call ?

Upvotes: 6

Views: 21616

Answers (2)

Shiva Kumar
Shiva Kumar

Reputation: 3161

Jenkins java docs are available here. These apis can also be used along with groovy script directly. If you want to use Postbuild groovy script plugin, you can access the build with manager. Below is a sample code snippet which disables a build if it is unsuccessful

if (manager.build.result.isWorseThan(hudson.model.Result.SUCCESS)) {
manager.build.project.disabled = true
}

You can have look at Groovy Postbuild Plugin for more details

Upvotes: 2

Kanstantsin Shautsou
Kanstantsin Shautsou

Reputation: 771

From java code it should looks like:
1) get item: Jenkins.getInstance().getItem("jobName")
2) check that item is instanceof some job type (or just Abtract) and cast
3) then just call .getLastBuild() on this object
4) this will be a build object (AbstractBuild) where you can get id, date, result and etc.

Upvotes: 0

Related Questions