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

12,155

Solution 1

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>

Solution 2

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);
});

Solution 3

For me is also working by getting a json:

http://<host>/job/<jobname>/lastBuild/api/json?tree=executor[progress]

Solution 4

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.

Share:
12,155
Russell Smith
Author by

Russell Smith

I started as a FORTRAN programmer, paid my dues writing C and X11/Motif, switched to Perl, discovered Tk, and from that, Tcl, and spent the next decade plus writing cross-platform GUIs in Tcl/Tk. I then spent three years using python and a smattering of ruby to create a cross-platform automated testing framework. After that I had two more gigs in windows shows, building automated testing frameworks and leading automation teams. I am now back to being a python developer at a small company in the independent publishing business. I was profiled as python developer of the week at http://www.blog.pythonlibrary.org/2015/03/02/pydev-of-the-week-bryan-oakley/ The open source projects I currently am active on are: The Robot Framework Hub - A web app and RESTful API for accessing robot framework test assets Robot Framework Lint - a static analysis tool for robot framework test files. Page Object Library - an implementation of the page object pattern for robot framework Robot framework extension for the brackets open source editor. I also maintain a sporadically-updated blog at boakley.github.io, focused mainly on my work with the robot framework.

Updated on June 20, 2022

Comments

  • Russell Smith
    Russell Smith almost 2 years

    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.

  • BLang
    BLang over 6 years
    I noticed that this only shows for when the build is complete, is there a feed to get live updates on the percent complete of the build? For instance when a person initiates a build i can emulate the jenkins progress bar on another website?
  • BLang
    BLang over 6 years
    I keep getting 404 using the last_result() call, if i have a job named, 'jobA', how do you pass it to last_result? Because if i use the function, last_build_info, i just pass it as last_build_info('jobA'), but if i do last_result('jobA') or last_result('/jobA') i get 404.
  • Metalskin
    Metalskin over 6 years
    Sorry BLang, it's been a long time since I last looked at this. I had a quick look through my old lib and the above is how I was doing it. You can see my old code at github.com/jamesjenner/Vor/blob/master/lib/server/jenkins.js‌​. It was working back in those days. Just no idea now a days sorry.
  • BLang
    BLang about 6 years
    no worries, i actually figured out a solution and when i finish/ get it stable, ill be posting to the thread i created here: stackoverflow.com/questions/48455608/…