how do i get the latest build in jenkns with a specified parameter

徘徊边缘 提交于 2019-12-11 04:53:51

问题


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:


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>


来源:https://stackoverflow.com/questions/38986194/how-do-i-get-the-latest-build-in-jenkns-with-a-specified-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!