Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get build information from Jenkins API

Tags:

rest

jenkins

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 ?

like image 880
user479151 Avatar asked Feb 07 '13 09:02

user479151


People also ask

How do I get Jenkins build information?

Or if you want to access information about a particular build, e.g. https://ci.jenkins.io/job/Websites/job/jenkins.io/job/master/lastSuccessfulBuild/ , then go to https://ci.jenkins.io/job/Websites/job/jenkins.io/job/master/lastSuccessfulBuild/api/ and you'll see the list of functionalities for that build.

How can I see my Jenkins details?

From the CI/CD drop-down list, select Jenkins > Jobs. A dashboard show Jenkins job data. For details, see Jenkins > Jobs.

How do I access REST API Jenkins?

The simplest way to access Jenkins REST API is to gather the User Token which is available by selecting your User and clicking on Configure. Now you will use the token as parameter for your authentication.


1 Answers

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

like image 156
Shiva Kumar Avatar answered Oct 11 '22 22:10

Shiva Kumar