How to extract build_id from last successful build in Jenkins?

Typically, to get the artifact of the last successful build, I do wget at the URL below:

http://jenkins.com/job/job_name/lastSuccessfulBuild/artifact/artifact1/jenkins.txt

Is there any way I can do wget on lastSuccessfulBuild and get build_id as build_id below?

 build_id='wget http://jenkins.p2pcredit.local/job/job_name/lastSuccessfulBuild' 
+8
source share
3 answers

Yes, there is a way, and it is quite simple:

 $ build_id=`wget -qO- jenkins_url/job/job_name/lastSuccessfulBuild/buildNumber` $ echo $build_id 131 # that my build number 
+15
source

I think the best solution is to use groovy with zero dependencies.

 node { script{ def lastSuccessfulBuildID = 0 def build = currentBuild.previousBuild while (build != null) { if (build.result == "SUCCESS") { lastSuccessfulBuildID = build.id as Integer break } build = build.previousBuild } println lastSuccessfulBuildID } } 

You do not need to specify jenkins_url or job_name, etc. to get the last successful build ID. Then you can easily use it in all Jenkinsfile repositories without unnecessary settings.

Tested on Jenkins v2.164.2

0
source

To get the last successful build number using curl:

curl --user userName: password https: // url / job / jobName / api / xml? xpath = / * / lastStableBuild / number

0
source

All Articles