0

How to get Jenkins last successful build number? I tried a few ways and none of them are working..

I'm using Jenkins 2.309

/api/xml doesn't have Last successful build either.

def buildNumber = Jenkins.instance.getItem('jobName').lastSuccessfulBuild.number doesn't return anything and the step files.

And then I tried this but it doesn't return the number.

                    catchError {
                        script {

                            def jenkins = Jenkins.getInstance()
                            def jobName = "demo-spring-boot"
                            def job = jenkins.getItem(jobName)

                            println "Last successfull build: ${job.getLastSuccessfulBuild()}"
                        }
                    }

It returns Last successfull build: null

I checked the job history and it does have few succssful builds

user630702
  • 2,529
  • 5
  • 35
  • 98
  • Is your job in a folder? Use "getItemByFullName()". – Ian W Sep 29 '21 at 10:40
  • Does this answer your question? [Jenkins - groovy script - get last successful build date in dd-mm-yyyy format](https://stackoverflow.com/questions/32180785/jenkins-groovy-script-get-last-successful-build-date-in-dd-mm-yyyy-format) – Ian W Sep 29 '21 at 10:41
  • Does this answer your question ? [List Jenkins job build detials for last one year along with the user who triggered the build]()https://stackoverflow.com/a/64509896/598141 – Ian W Oct 03 '21 at 23:46

1 Answers1

2
def lastSuccessfulBuildId = currentBuild.previousSuccessfulBuild?.id
println lastSuccessfulBuildId

(I put ? because there could be no successful builds)

towel
  • 2,094
  • 1
  • 20
  • 30