1
votes

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.

3
That should be possible. I recommend you to take a look at javadoc.jenkins-ci.org/jenkins/model/Jenkins.html. There are many APIs that you could use. - Chong
what did you try already? - Yuri G.
I tried using active Choice reactive parameter, and groovy script to get all the build numbers of a selected job def getAllBuildNumbers(Job job) { (job.getBuilds()).each { build -> buildNumbers print buildNumbers } } but not able to retrieve the build numbers - ZzzZ

3 Answers

3
votes

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

0
votes

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
votes

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