ZzzZ
ZzzZ

Reputation: 59

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. Can I write a groovy script which get me list of all the build numbers of that particular job.

Upvotes: 1

Views: 8017

Answers (3)

L. Tischler
L. Tischler

Reputation: 384

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 -> ...
    }
}

Upvotes: 0

Tidhar Klein Orbach
Tidhar Klein Orbach

Reputation: 2976

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: enter image description here

Then create an active choice parameter for retriving the build numbers from the selected job: enter image description here

Upvotes: 3

Bill K
Bill K

Reputation: 62789

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.

Upvotes: 0

Related Questions