4

My goal is to get the latest Jenkins job (jobname hanna) with a specific parameter.

The only reason why I'm doing this is Jenkins do not return a build number when I trigger from my script, so I have to pass in a parameter, but I have to be able to query against that parameter later.

Rigth now I have this, which doesn't return the build ID:

curl -X POST 'http://server:8080/api/xml?tree=jobs[name,builds[actions[parameters[name,value]]]]&xpath=/hudson/job[build/action/parameter[name="snapshot"][value="bb"]]&pretty=true'

and I get a list of jobs like this:

<job><name>hanna</name><build><action><parameter><name>snapshot</name><value>bb</value></parameter></action><action/><action/><action/><action/></build><build><action><parameter><name>snapshot</name><value>bb</value></parameter></action><action/><action/><action/><action/></build><build><action><parameter><name>snapshot</name><value>aa</value></parameter></action><action/><action/><action/><action/><action/></build><build><action/><action/><action/><action/></build></job>⏎  

which is not exactly what I want, because I want the latest instance of the job hanna with parameter snapshot=bb, this return snap=aa as well, and also I cannot figure out where the build ID is stored on Jenkins. Can someone tell me?

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
cakester
  • 421
  • 4
  • 15

1 Answers1

1

If you absolutely need to use the Jenkins Rest api call to query for a specific build number containing a specific build parameter, use the following query:

http://localhost:8080/job/MyJenkinsJob/api/xml?tree=builds[actions[parameters[value]],number]&xpath=//build[action[parameter[value="MyParameterValue"]]]/number

This should return something like:

<number>49</number>

If you would like to know more about XPath filtering, please see some examples here.

ALTERNATE APPROACH

To answer your original problem of "the only reason why im doing this is jenkins do not return a build number when i trigger from my script,"

What you could do is run curl with the "-i" parameter. When you do that, you should see a queue item id # returned.

For example:
curl -X POST -i http//localhost:8080/job/test123/buildWithParameters?aaa=ABC

Returns:
HTTP/1.1 201 Created
Date: Wed, 17 Aug 2016 03:15:28 GMT
X-Content-Type-Options: nosniff
Location: http://localhost:8080/queue/item/44/
Content-Length: 0
Server: Jetty(9.2.z-SNAPSHOT)

With this queue ID in hand, you can get the build number by going to:
http://localhost:8080/queue/item/44/api/xml

Which should return an XML with the number that you are looking for.

For example:

<leftItem>
   ...
   <executable _class="hudson.model.FreeStyleBuild">
      <number>11</number>
      <url>http://localhost:8080/job/test123/11/</url>
    </executable>
</leftItem>
Daniel Omoto
  • 2,431
  • 17
  • 15
  • this queue number is very short lived, and i have to keep polling to see if its assigned, if i miss it i have to find another way – cakester Aug 17 '16 at 15:53
  • is there anyway of fetching only the latest if there is multiple with the same parameter? – cakester Aug 22 '16 at 20:27
  • Yes, I think there probably is a way. XPath is pretty powerful and contains many functions. However, this answer has already gotten long (and transgressed even to an alternate approach). Let's call it a day on this question and start a new one. :) – Daniel Omoto Aug 23 '16 at 03:23