Ram
Ram

Reputation: 29

How to get Jenkins pending build id in python

views.py

here i can get the all information of build of jenkins job which is currently running. but when user runs or triggers same job again it will make queue on jenkins but as a pending build. so i get the build id of currently running job and not of pending jenkins build. Actualy i want build id of pending build.

  try:
                             print "in try.."
                             jenkinsStream   = urllib2.urlopen( "http://10.211.213.138:8080/job/TE-mobius/lastBuild/api/json?pretty=true" )
                             print "after fetching url"
                        except urllib2.HTTPError, e:
                             print "URL Error: " + str(e.code)
                             print "      (job name [" + jobName + "] probably wrong)"


                        try:
                            print "Before loading json"
                            buildStatusJson = json.load( jenkinsStream )
                            print "After loading json"
                        except:
                            print "Failed to parse json"
                        print "before while.."
                        print buildStatusJson["building"]
                       #print "[" + jobName + "] duration: " + str(buildStatusJson["duration"])
                        print buildStatusJson.has_key( "result" )
                        if buildStatusJson.has_key( "result" )==True:
                            print "in if"
                            print "[" + jobname + "] duration: " + str(buildStatusJson["duration"])
                            print "[" + jobname + "] building: " + str(buildStatusJson["building"])
                            print "[" + jobname + "] timestamp: " + str(buildStatusJson["timestamp"])
                            print "[" + jobname + "] url: " + str(buildStatusJson["url"])
                            print "[" + jobname + "] result: " + str(buildStatusJson["result"])
                            print "[" + jobname + "] build number: " + str(buildStatusJson["number"])
                            print "[" + jobname + "] JobName: " + str(buildStatusJson["fullDisplayName"])

Upvotes: 0

Views: 2282

Answers (1)

freshtop
freshtop

Reputation: 796

There's an API endpoint at http://10.211.213.138:8080/job/TE-mobius/api/json?pretty=true that has a nextBuildNumber key.

It also has

"queueItem" : {
    "blocked" : false,
    "buildable" : true,
    "id" : 119,
    "inQueueSince" : 1401783373284,
    "params" : "",
    "stuck" : false,
    "task" : {
      "name" : "YourJobName",
      "url" : "http://jenkins/job/YourJobName/"
    },
    "url" : "queue/item/119/",
    "why" : "Waiting for next available executor",
    "buildableStartMilliseconds" : 1401783373290,
    "pending" : false
  }

with some basic information about that pending build. Some of the build information you're trying to print in your code won't exist in Jenkins until the build has started.

Unfortunately, this will only let you get the build number of the next pending build. If you have a parameterised build and multiple builds in the queue for one job, I'm not aware of any way to get the build numbers of the other pending builds. You might have to write a plugin if you need to get those.

Upvotes: 1

Related Questions