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.
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>
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);
});
For me is also working by getting a json:
http://<host>/job/<jobname>/lastBuild/api/json?tree=executor[progress]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With