简体   繁体   English

如何使用groovy脚本获取jenkins中特定作业的所有构建号?

[英]How can I get all build numbers of particular job in jenkins using groovy script?

I am using active choice plugin, have groovy script which retrieves names of all jobs in Jenkins. 我正在使用主动选择插件,有一个groovy脚本,可以检索Jenkins中所有作业的名称。 Can I write a groovy script which get me list of all the build numbers of that particular job. 我可以编写一个groovy脚本来获取该特定作业的所有构建号的列表。

In order to get a list of builds for a particular job using the active choice plugin, You can use the following example. 为了使用活动选择插件获取特定作业的构建列表,您可以使用以下示例。

First, create an active choice parameter for retriving the job names: 首先,为恢复作业名称创建一个活动的选择参数: 在此输入图像描述

Then create an active choice parameter for retriving the build numbers from the selected job: 然后创建一个活动的选择参数,用于从所选作业中检索构建号: 在此输入图像描述

Try: 尝试:

def xml=new XmlSlurper().parse("http:{serverName}/Jenkins/rssLatest")
def projects = xml.entry.collect{(it.title as String).split("#")[0].trim()}
println projects

In general I suggest browsing your jenkins feed for the info you want then look for an "rss feed" -- then put that rss URL into the XmlSlurper and see what you get. 一般情况下,我建议浏览你的jenkins feed以获取你想要的信息,然后查找“rss feed” - 然后将该rss URL放入XmlSlurper中,看看你得到了什么。

Also I tend to work through these things in groovysh, it makes it really easy to explorer how objects work. 此外,我倾向于在groovysh中完成这些工作,这使得探索对象如何工作变得非常容易。 In this case you might want to also be looking at the raw XML as you try objects in groovysh. 在这种情况下,您可能还希望在groovysh中尝试对象时查看原始XML。

I would go over the REST-API (because there I know that it works): 我会讨论REST-API(因为我知道它可以工作):

http:///api/xml?tree=jobs[name,builds[result,description,id,number,url,timestamp]]&pretty=true&xpath=hudson/job[name='']&wrapper=jobs HTTP:/// API / XML树=就业[名称,建立[结果说明,身份证,号码,URL,时间戳]]漂亮=真XPath的=哈德森/工作[名称= ''] =包装工作

This results in something like this: 这导致如下所示:

<jobs>
    <job _class="...">
        <name>JOBNAME</name>
        <build _class="...">
            <id>66</id>
            <number>66</number>
            <result>FAILURE</result>
            <timestamp>1489717287702</timestamp>
            <url>JOB_URL</url>
        </build>
        ...
    </job>
<jobs>

So the following call in conjunction with an foreach-loop should do the trick. 因此,以下调用与foreach循环一起应该可以解决问题。

def text = "rest_api_url".toURL().text
def jobs = new XmlSlurper().parseText(text.toString())

Otherwise something like this should also work - even I haven't tested it. 否则这样的事情也应该有用 - 即使我还没有测试过。

import jenkins.model.*

for(item in Jenkins.instance.items) {
    if ("JOBNAME".equals(item.name)) {
        // do something with $item.builds
        // foreach item.builds -> ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM