How to get a list of jenkins starts using groovy script?

Is there a way to get the list of RUNNING assemblies in Jenkins through Groovy Script? I tried a loop through busy executors, but from the executing object I cannot get the assembly object:

def busyExecutors = Jenkins.instance.computers .collect { c -> c.executors.findAll { it.isBusy() } } .flatten() // reminder: transforms list(list(executor)) into list(executor) busyExecutors.each { e -> println('=====print out methods of executor object======='); println e.metaClass.methods*.name.sort().unique(); } 

I can also target a job that interests me like this:

 def item = hudson.model.Hudson.instance.getItem("my_job"); println item.metaClass.methods*.name.sort().unique(); 

But then I will have to iterate over 100 (if not more) assemblies and ask each assembly if they are running.

There should be an easier / better way to get a list of running assemblies.

There is a lot of information on how to do various things through System Groovy Scripts (some of which I wrote), but I cannot figure out how to get a list of running assemblies:

How to get node name of current job in jenkins using groovy

https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

https://gist.github.com/dnozay/e7afcf7a7dd8f73a4e05

How to get Jenkins / Hudson's work to track some other tasks and decide whether to build or not?

+6
source share
3 answers

I found a way to do this without using the REST API or XML parsing:

 Jenkins.instance.getItems().each { job-> job.builds.each { build-> if (build.getResult().equals(null)) { // do stuff here... } } } 

Please note that this will not happen in folders or Multibranch Pipelines or something like that. You will need to manually go down to the folders or come up with a way to do this automatically. For example, here is the version that works for Multibranch Pipeline:

 Jenkins.instance.getItemByFullName(multibranchPipelineProjectName).getItems().each { repository-> repository.getItems().each { branch-> branch.builds.each { build-> if (build.getResult().equals(null)) { // do stuff here ... } } } } 

I think there may be a more accurate use method than build.getResult().equals(null) to determine if the assembly works or not, but it's hard for me to find good API documents, so I'm not sure. This was only the first method I found with an object introspection that worked.

Again, due to the lack of API documents, I'm not sure if there is a significant difference between the Jenkins.instance.getItems() that I used here and the Jenkins.instance.getAllItems() that was used in this answer .

Finally, note that this is a relatively inefficient method. It iterates over each assembly of each work, so if you save a long build history (by default, only 10 collections are saved per task) or thousands of tasks, this can take some time. See How to efficiently list ** All ** Jenkins jobs currently in progress using Groovy for a question that asks how to make this task more efficient.

+2
source

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> <!-- This is the url from the current running build --> <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 
+1
source

This is not particularly efficient (but MUCH is more efficient than using the API). It prints all current assemblies using an HTML link. It can be run in the console script or through the scriptler.

 def now = new Date() // Get the current time // Get a list of all running jobs def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll { it.isBuilding() } buildingJobs.each { job-> // Enumerate all runs allRuns = job._getRuns() allRuns.each { item -> // If NOT currently building... check the next build for this job if (!item.isBuilding()) return // Access and calculate time information for this build. def startedAt = new Date(item.getStartTimeInMillis()) def duration_mins = ((now.getTime() - item.getStartTimeInMillis()) / 60000).intValue() estDurationMins = (item.getEstimatedDuration() / 60000).intValue() String jobname = item.getUrl() jobname = jobname.replaceAll('job/', '') // Strip redundant folder info. println "${duration_mins.toString().padLeft(5)}/" + "${estDurationMins.toString().padLeft(4)} - " + "<a href=${baseURL}${item.getUrl()}>${jobname}</a>" } } 
+1
source

Source: https://habr.com/ru/post/1011818/


All Articles