Skip to content

Instantly share code, notes, and snippets.

@bryantrobbins
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryantrobbins/0a637331f486d8a61f62 to your computer and use it in GitHub Desktop.
Save bryantrobbins/0a637331f486d8a61f62 to your computer and use it in GitHub Desktop.
A quick script to get the # of builds in the "Build Queue" - i.e., the number of builds which are awaiting an open Executor slot - in Jenkins.
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.8'
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7'
}
task getBuildCount (dependsOn: 'classes', type: JavaExec) {
main = 'getBuildCount'
classpath = sourceSets.main.runtimeClasspath
}
import groovyx.net.http.RESTClient
def client = new RESTClient('http://jenkins:someport' )
println getAwaitingBuildCount(client)
int getAwaitingBuildCount(RESTClient client){
def resp = client.get( path : '/queue/api/json' )
return resp.data['items'].size()
}
@bryantrobbins
Copy link
Author

NOTE: getBuildCount.groovy needs to be in src/main/groovy to work with this gradle file (Gist won't let me indicate path on the filename.)

@bryantrobbins
Copy link
Author

TODO: Merge this function into http://github.com/bryantrobbins/jenkinsutils

@bryantrobbins
Copy link
Author

TODO: Show how to add auth to this.

@bryantrobbins
Copy link
Author

Jenkins Remote API docs are here. If you haven't looked or tried something in a while, it's constantly improving (because opensource):

https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment