Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get progress bar data for a running jenkins job through the API

Tags:

jenkins

api

I want to use the jenkins API to get information about my current jobs. I can find information on the last build (.../job/MyJob/lastBuild/api/xml) but I'm not seeing the field(s) that would let me create a progress bar. I see an estimatedDuration field and a building field, but nothing that tells me how long it's already been running.

like image 781
Bryan Oakley Avatar asked Oct 17 '12 22:10

Bryan Oakley


4 Answers

Here's the URL that gives me the information I need:

http://<host>/job/<jobname>/lastBuild/api/xml?tree=timestamp,estimatedDuration

Which yields something like:

<freeStyleBuild>
  <estimatedDuration>86126</estimatedDuration>
  <timestamp>1350615637401</timestamp>
</freeStyleBuild>
like image 132
Bryan Oakley Avatar answered Oct 18 '22 03:10

Bryan Oakley


I came across this question while trying to retrieve the percentage. As I figured out a solution, I thought I would post it here.

The response includes two fields, timestamp (time the job started) and estimatedDuration (milliseconds).

If you take the current time, you can subtract the timestamp from the current time. This will give you the number of milliseconds since the job started. Using this calculated value, you can compare it with the estimatedDuration field and thus determine the percentage complete.

So the formula would be (where data is a JSON object of the returned data):

Console.log(" complete: " + Math.round((new Date().getTime() - data.timestamp) / data.estimatedDuration * 100) + "%");

Your using XML and not JSON, but I'm sure it's a similar structure.

I'm using the following lib in node.js: https://github.com/jansepar/node-jenkins-api

My logic is:

var jenkinsapi = require('./lib/jenkins');

var jenkins = jenkinsapi.init("http://jenkins.myServer.com:8080");

jenkins.last_result('/myProj', function(err, data) {

  if(err) {
    console.log("last result kitchen_ellipse: ERROR (last result): " + data.statusCode);
    return;
  }

  console.log("progress " + data.fullDisplayName + " number: #" + data.number + 
          + " complete: " + 
            Math.round((new Date().getTime() - data.timestamp) / 
                       data.estimatedDuration * 100) + "%"
          + " result: " + data.result);
});
like image 31
Metalskin Avatar answered Oct 18 '22 03:10

Metalskin


For me is also working by getting a json:

http://<host>/job/<jobname>/lastBuild/api/json?tree=executor[progress]
like image 7
sliwinski.lukas Avatar answered Oct 18 '22 01:10

sliwinski.lukas


use following :

 http://<host>/job/<jobname>/lastBuild/api/xml?depth=1&xpath=*/executor/progress/text()

this will return you the progress in percentage

ex : 27

similarly you can get other parameters like user, id(build number), timestamp etc.

you can find them using following url:

http://<host>/job/<jobname>/lastBuild/api/xml?depth=1

above url returns an xml where all the required parameter ,from last build, are listed.

like image 2
Rajeev Ranjan Avatar answered Oct 18 '22 01:10

Rajeev Ranjan