Jenkins - Get last completed build status
Asked Answered
T

3

54

I know I can call localhost/job/RSpec/lastBuild/api/json to get the status of the lastest Jenkins build. However, since our build runs very long (a couple hours), I'm really more interested in the last complete build status than the run that is running at this exact moment.

Is there an API end point for the last fully run build? Or should I instead pull the full list of builds and select the next-to-last one from there?

Tor answered 14/8, 2013 at 17:40 Comment(0)
C
122

Try http://$host/job/$jobname/lastSuccessfulBuild/api/json

Jenkins (and Hudson) expose multiple different builds, such as lastBuild, lastStableBuild, lastSuccessfulBuild, lastFailedBuild, lastUnstableBuild, lastUnsuccessfulBuild, lastCompletedBuild.

Cordeelia answered 14/8, 2013 at 20:4 Comment(3)
Wondering if there is a way to do this for all the jobs in a view.Antipus
Yep @SteventheEasilyAmused, just do .../job/$jobname/api/json?depth=2Orr
NOTE: at least as of Jenkins 2.263.3, if there has never been a successful build for $jobname, this API endpoint will return an HTML 404 Error pageKabyle
V
11

To get the last successful build number:

curl --user <userName:password> https://<url>/job/<job-Name>/api/xml?xpath=/*/lastSuccessfulBuild/number
Volkslied answered 23/7, 2019 at 10:12 Comment(0)
A
1

JenkinsAPI is also a nice option. I'm mainly using python and so here is my version of things:

  1. pip install jenkinsapi
  2. python code: (in my case no special authentication to Jenkins is needed, otherwise i assume a token is needed)
from jenkinsapi.jenkins import Jenkins
server = Jenkins(<Jenkins server URL>)
job = server.get_job(<job name>)
last_successful_build_number = job.get_last_good_buildnumber()
Arithmetician answered 4/1, 2022 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.