Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list successful jenkins builds using a groovy script in a deploy job?

Tags:

jenkins

groovy

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();
like image 365
Alex Rodrigues Avatar asked Sep 05 '25 02:09

Alex Rodrigues


2 Answers

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();
like image 81
Alex Rodrigues Avatar answered Sep 07 '25 09:09

Alex Rodrigues


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.

like image 40
Alexander Sorkin Avatar answered Sep 07 '25 10:09

Alexander Sorkin