简体   繁体   中英

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 ?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM