pbx
pbx

Reputation: 718

How can I get the final status of a completed Jenkins job via API?

I make a call like:

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

and get a result like:

{
    "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"
    }]
}

I am passing JT_BUILD_ID so I can match to this run of MYJOB.

When I find the matching JT_BUILD_ID, I look at result and when I see SUCCESS I assume I am done. But the job is still running do to post build actions.

How can I both match the instance of the build (using the parameter JT_BUILD_ID) and detect the final status of the job?

Upvotes: 4

Views: 2967

Answers (1)

Larry Cai
Larry Cai

Reputation: 59963

There is another key building under builds, if the post-build is still working, the status will be kept as true. It turns to false when the job is totally completed.

Therefore you can add extra check for building == false, the final url looks like below

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]

The result could be like below in your case if latest build is ongoing (in post-build phase)

{
  "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"
  }]
}

Upvotes: 4

Related Questions