You can use the REST API to get a list of running assemblies. Using the following URL:
http://myjenkins/jenkins/computer/api/xml?depth=1
You will receive the following response containing the <executor>
elements. Only working assembly has an <url>
element inside an <executor>
. Also note that running builds has an <idle>false</idle>
value:
<computerSet> <busyExecutors>1</busyExecutors> <computer> ... <executor> <idle>true</idle> <likelyStuck>false</likelyStuck> <number>0</number> <progress>-1</progress> </executor> <executor> <currentExecutable> <number>328</number> <url>http://myJenkins/jenkins/job/someJob/328/</url> </currentExecutable> <currentWorkUnit/> <idle>false</idle> <likelyStuck>false</likelyStuck> <number>1</number> <progress>24</progress> </executor> ... </computer> <computerSet>
Therefore, use the REST API with XPath for url
to get only running assemblies (note that the &wrapper
parameter is the name of the root xml element to avoid errors when XPath does not match or returns more than one node):
http://myJenkins/jenkins/computer/api/xml?depth=1&xpath=//url&wrapper=builds
You will get something like:
<builds> <url> http://myJenkins/jenkins/job/someJob/300/ </url> <url> http://myJenkins/jenkins/job/another/332/ </url> </builds>
So, in Groovy, you can GET the REST API, parse the returned Xml, and then apply the regex for each <url>
to get data from running assemblies:
// get the xml from the rest api def builds = 'http://myJenkins/jenkins/computer/api/xml?depth=1&xpath=//url&wrapper=builds'.toURL().text // parse the xml result def xml = new XmlSlurper().parseText(builds) // for each url get the job name def jobNames = xml.url.collect{ url -> // regex to get the jobName and a build number def group = (url =~ /.*\/job\/([^\/]+)\/(\d+)/) println group[0] // [http://myJenkins/jenkins/job/someJob/300, someJob, 300] def jobName = group[0][1] return jobName // to get the build number // def buildNr = group[0][2] } println jobNames
source share