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

前端 未结 3 784
南旧
南旧 2020-12-20 04:34

I am using active choice plugin, have groovy script which retrieves names of all jobs in Jenkins. Can I write a groovy script which get me list of all the build numbers of t

相关标签:
3条回答
  • 2020-12-20 05:21

    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:

    0 讨论(0)
  • 2020-12-20 05:22

    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.

    Also I tend to work through these things in groovysh, it makes it really easy to explorer how objects work. In this case you might want to also be looking at the raw XML as you try objects in groovysh.

    0 讨论(0)
  • 2020-12-20 05:26

    I would go over the REST-API (because there I know that it works):

    http:///api/xml?tree=jobs[name,builds[result,description,id,number,url,timestamp]]&pretty=true&xpath=hudson/job[name='']&wrapper=jobs

    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.

    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 -> ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题