3
votes

I have a Jenkins deploy job that copies artifacts from a build job. In my deploy job, I am using a groovy script (see below) in an Extensible Choice parameter to present a list of successful builds from that build job in a drop-down. I would like to enhance the groovy script to list only successful builds from that build job. How can I do this?

def builds = []
def job = jenkins.model.Jenkins.instance.getItem(JOB-NAME)
job.builds.each {
    def build = it
    it.badgeActions.each {
        builds.add(build.displayName[1..-1])
    }
}

builds.unique();

2

2 Answers

10
votes

I managed to get it figured out ... see code snippet below

def builds = []

def job = jenkins.model.Jenkins.instance.getItem(JOB-NAME)
job.builds.each {
def build = it
if (it.getResult().toString().equals("SUCCESS")) {
    it.badgeActions.each {
             builds.add(build.displayName[1..-1])
     }
}
}

builds.unique();
0
votes

In Jenkins 2.289.2 the working code is as following:

def builds = []

def job = jenkins.model.Jenkins.instance.getItem(JOB-NAME)
job.builds.each {
    if (it.result == hudson.model.Result.SUCCESS) {
        builds.add(it.displayName[1..-1])
    }
}

return builds

The difference is in absence of badgeActions, which shows only builds made before the Jenkins upgrade.