如何通过API获取已完成的Jenkins作业的最终状态?

时间:2014-03-04 21:20:55

标签: api jenkins

我拨打电话:

http://jenkins.mysite.com/job/MYJOB/api/json?depth=2&tree=builds%5Bactions%5Bparameters%5Bname%2Cvalue%5D%5D%2Cnumber%2Cresult%5D

得到如下结果:

{
    "builds": [{
        "actions": [{
            "parameters": [{
                "name": "JT_BUILD_ID",
                "value": "1274"
            }]
        }, {}, {}, {}, {}],
        "number": 3,
        "result": "SUCCESS"
    }, {
        "actions": [{
            "parameters": [{
                "name": "JT_BUILD_ID",
                "value": "1273"
            }]
        }, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
        "number": 2,
        "result": "SUCCESS"
    }, {
        "actions": [{
            "parameters": [{
                "name": "JT_BUILD_ID",
                "value": "0"
            }]
        }, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
        "number": 1,
        "result": "SUCCESS"
    }]
}

我正在传递JT_BUILD_ID,所以我可以匹配MYJOB的这次运行。

当我找到匹配的JT_BUILD_ID时,我会看结果,当我看到SUCCESS时,我认为我已经完成了。但是这项工作仍然在进行,以发布构建行动。

我如何才能匹配构建实例(使用参数JT_BUILD_ID)并检测作业的最终状态?

1 个答案:

答案 0 :(得分:4)

building下还有另一个键builds,如果构建后仍然有效,状态将保持为true。当工作完成后,它会变为false

因此,您可以为building == false添加额外检查,最终网址如下所示

https://ci.jenkins-ci.org/view/Libraries/job/lib-jira-api/api/json/?pretty=true&depth=2&tree=builds[actions[parameters[name,value]],number,result,building]

如果最新版本正在进行(在构建后阶段),结果可能如下所示

{
  "builds": [{
    "actions": [{
        "parameters": [{
            "name": "JT_BUILD_ID",
            "value": "1274"
        }]
    }, {}, {}, {}, {}],
    "building" : true,
    "number": 3,
    "result": "SUCCESS"
  }, {
    "actions": [{
        "parameters": [{
            "name": "JT_BUILD_ID",
            "value": "0"
        }]
    }, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
    "building" : false,
    "number": 1,
    "result": "SUCCESS"
  }]
}