dooplenty
dooplenty

Reputation: 124

How to query specific branch build number from Jenkins JSON Remote Access API

Within the browser for my jenkins job I'm running the following query.

lastStableBuild/api/json?pretty=true&tree=actions[buildsByBranchName[*[*]]]

Results from the above query

{
  "_class" : "hudson.model.FreeStyleBuild",
  "actions" : [
    {
      "_class" : "hudson.model.CauseAction"
    },
    {

    },
    {
      "_class" : "jenkins.metrics.impl.TimeInQueueAction"
    },
    {

    },
    {
      "_class" : "hudson.plugins.git.util.BuildData",
      "buildsByBranchName" : {
        "my-branch-name" : {
          "_class" : "hudson.plugins.git.util.Build",
          "buildNumber" : 587,
          "buildResult" : null,
          "marked" : {
            "SHA1" : "***",
            "branch" : [
              {

              }
            ]
          },
          "revision" : {
            "SHA1" : "***",
            "branch" : [
              {

              }
            ]
          }
        },
        "my-other-branch-name" : {
          "_class" : "hudson.plugins.git.util.Build",
          "buildNumber" : 1373,
          "buildResult" : null,
          "marked" : {
            "SHA1" : "***",
            "branch" : [
              {

              }
            ]
          },
          "revision" : {
            "SHA1" : "***",
            "branch" : [
              {

              }
            ]
          }
        },

I would like to be able to narrow it down to just the build number like you would get with

/lastSuccessBuild/buildNumber

using the api but I would settle for just everything inside of the branch name key so that I wouldn't have to loop through all branches and compare the name. I'm assuming I can narrow it down more where I have my "*" specified but can't figure out the right syntax to use.

Upvotes: 2

Views: 1891

Answers (2)

Wimateeka
Wimateeka

Reputation: 2686

If you don't mind getting your answer back in xml, xpath works very well.

For the url:

/lastStableBuild/api/xml?xpath=//buildsByBranchName&wrapper=meep

Creates an xml that looks like:

<meep>
    <buildsByBranchName>
        ... 
    </buildsByBranchName>
</meep>

And will be populated with the buildsByBranchName (NOTE: there may be more than one if there are multiple git remotes, hence the need for a wrapper) for the specified last successful build of the job specified in the url. You can substitute anything for the word "meep", that will become the wrapper object for the newly created xml object.

Upvotes: 0

Glen Bizeau
Glen Bizeau

Reputation: 73

I got that info from here instead.

tree=actions[lastBuiltRevision[*,branch[*]]]

Either way, if you want the branch info, from inside the buildsByBranchName section of the tree, you will have to query it as I did above.

Upvotes: 2

Related Questions