简体   繁体   English

如何通过API获取正在运行的jenkins作业的进度条数据

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

I want to use the jenkins API to get information about my current jobs. 我想使用jenkins API来获取有关我当前工作的信息。 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. 我可以找到有关上一次构建的信息( .../job/MyJob/lastBuild/api/xml ),但我没有看到可以让我创建进度条的字段。 I see an estimatedDuration field and a building field, but nothing that tells me how long it's already been running. 我看到了一个estimatedDuration字段和一个building字段,但没有任何东西可以告诉我它已经运行了多长时间。

Here's the URL that gives me the information I need: 这是给我提供所需信息的URL:

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). 响应包括两个字段, timestamp (作业开始的时间)和estimatedDuration持续时间(毫秒)。

If you take the current time, you can subtract the timestamp from the current time. 如果您使用当前时间,则可以从当前时间中减去timestamp 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. 使用此计算值,您可以将其与estimatedDuration字段进行比较,从而确定完成百分比。

So the formula would be (where data is a JSON object of the returned data): 所以公式是(其中data是返回数据的JSON对象):

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. 你使用XML而不是JSON,但我确信它是一个类似的结构。

I'm using the following lib in node.js: https://github.com/jansepar/node-jenkins-api 我在node.js中使用以下lib: 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);
});

对我来说,也是通过获得一个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 例如:27

similarly you can get other parameters like user, id(build number), timestamp etc. 类似地,您可以获得其他参数,如用户,ID(内部版本号),时间戳等。

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. 上面的url返回一个xml,其中列出了上一次构建的所有必需参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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