21

I am trying to get the number of builds in the Jenkins Build Queue.

May I know the Jenkins command to get the number of builds running in the queue ?

Vitalii Elenhaupt
  • 7,146
  • 3
  • 27
  • 43
Lokith
  • 211
  • 1
  • 2
  • 4

6 Answers6

20

See Jenkins' Remote access API.

Access the API description with:

  http://<Your Jenkins>/api/

and the actual data with:

  http://<Your Jenkins>/api/xml

The Build queue has its own separate API:

  http://<Your Jenkins>/queue/api/

with its data:

  http://<Your Jenkins>/queue/api/xml

Alternatively you can use json if you prefer that format, just replace xml to json in the URL:

  http://<Your Jenkins>/queue/api/json
redseven
  • 849
  • 6
  • 11
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
7

That's easy to do with Jenkins Script Console:

println Hudson.instance.queue.items.length
// => 2

Also that's possible to execute groovy script remotely. For example, from command line:

$ curl -u username:password -d "script=println Hudson.instance.queue.items.length" jenkins_url/scriptText
2

Note: user with specified username should have access to Jenkins Script Console.

Vitalii Elenhaupt
  • 7,146
  • 3
  • 27
  • 43
  • 4
    Note in more recent versions the call is now `Jenkins.instance.queue.items.size()`, or if you're calling it from a script, `jenkins.model.Jenkins.instance.queue.items.size()` – jpyams Feb 13 '18 at 18:03
6

Here is a shell script implementation of the mentioned Jenkins REST API

_queuesize=$(curl -s -k -m 60 http://${yourjenkinsserver}:8180/jenkins/queue/api/xml 2>/dev/null | grep -c '<item>')
if [[ -z "${_queuesize}" ]]; then
  _queuesize=0;
fi
Nick Constantine
  • 963
  • 9
  • 19
4

This single bash variable definition using the awesome "jq" utility was all I needed to get the queue length:

  QUEUE_SIZE=$(curl -s -k http://<your-jenkins-here>/queue/api/json | jq '.items | length')

Hope this helps.

matias elgart
  • 1,123
  • 12
  • 18
1

Just slightly related, but if you looking for the queue size for monitoring, there is a jenkins plugin for munin which does that (and some others too):

https://gallery.munin-monitoring.org/plugins/munin-contrib/jenkins_/

enter image description here

redseven
  • 849
  • 6
  • 11
0

Try Jenkins API in Python.

get_jobs()
  Get list of jobs running.
  Each job is a dictionary with ‘name’, ‘url’, and ‘color’ keys.
Returns:    list of jobs, [ { str: str} ]
mainframer
  • 20,411
  • 12
  • 49
  • 68