how do i get the latest build in jenkns with a spe

2019-07-25 18:10发布

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?

1条回答
一夜七次
2楼-- · 2019-07-25 18:22

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>
查看更多
登录 后发表回答