Answers: <p>I used the Groovy script console to obtain it this way:</p>
  <pre><code>def executables = Jenkins.instance.computers.collect {c -&gt; c.executors}.
  def runs = Jenkins.instance.computers.collect {c -&gt; c.executors}.
     flatten().
    findAll { executor -&gt; executor.isBusy() }.
    collect { executor -&gt; executor.getCurrentExecutable() }.
      collect { executable -&gt; &quot;${executable.displayName}: ${executable.number}&quot; }
  </code></pre>
  <p>or if you want the <code>Build</code> class, this:</p>
  <pre><code>Jenkins.instance.computers.collect {c -&gt; c.executors}.
    flatten().
    findAll { executor -&gt; executor.isBusy() }.
    collect { executor -&gt;
      def u = executor.currentExecutable.url
      def n = executor.currentExecutable.number
      def job_qual_name = u.split('/')[0..-2].findAll { it != 'job' }.join('/')
      def job = Jenkins.instance.getItemByFullName(job_qual_name, Job.class)
      job.getBuildByNumber(build.number)
    }
  </code></pre>
  <p>It's pretty painful that Jenkins doesn't provide something as simple as this with a clear, high-level API.</p>
  <p>It's extra ugly that the paths used by the folders plugin jobs don't work with <code>Jenkins.getItemByFullName()</code> without munging. Ick. Also that <code>Jenkins.getItemByFullName()</code> doesn't seem to support getting a <code>Build</code>.</p>
  <p>You won't want to put this in Pipeline code. Instead put it in your script library so it isn't subject to method authorization issues, and only returns the final results.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-02-22T04:44:09.393Z
Answers: 2021-02-22T06:46:15.953Z
Answers: 2021-02-22T06:46:15.953Z
Answers: Craig Ringer
Answers: <p>Turns out you can hit the following URL to get an XML output of all currently running builds, including their build numbers. </p>
  
  <pre><code>http://jenkinsURL/computer/api/xml?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&amp;xpath=//url&amp;wrapper=builds
  </code></pre>
  
  <p>I <code>grep</code>ed it for a count and used that for my calculations.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-07-19T15:27:17.640Z
Answers: 2017-07-19T15:27:17.640Z
Answers: Alex
Body: <p>There's plenty of questions asking how to return a list of all jobs on your Jenkins instance, or even to get a list of which jobs are currently running, but I have a more specific use-case. </p>
  
  <p>I need to see if a single job is already running. As in, return one value if there is 1 builds of the job running, and a different value if 2 or more builds are running.</p>
  
  <p>The use case here is that we've automated our image creation to build after successful unit tests. It's set to build a maximum of one image every seven hours, by checking the build time of the last image and cancelling the build if it's been less than 7hrs. This works perfectly for most cases. However, these images take ~20min to build. If it's been over 7 hours and <strong>two</strong> unit tests finish within less than 20min of each other, the second build doesn't see the first creating, and continues building. We're getting two functionally identical images made once every 7hrs, only minutes apart. I'd like to check the status of the controller job to know if it's already running, so that the second build can know to cancel. </p>
  
  <p>I've tried curl-ing the following API, but it turns out it doesn't register individual builds, just that the job is running in general, which is useless in this case as it self-triggers since it checking means it's already running; there's no difference between a single build and multiple builds.</p>
  
  <pre><code>http://JENKINS_URL/api/xml?tree=jobs[name,url,color]&amp;xpath=/hudson/job[ends-with(color/text(),%22_anime%22)]&amp;wrapper=jobs
  </code></pre>
  
  <p>TL;DR: Is there a different call I can make that will return some list/file/content that indicates that multiple builds of the same job are currently running?</p>
  
Tags: <jenkins><jenkins-pipeline>
Title: Jenkins API call (or Groovy builtin) that returns list of all currently running builds of jobs
UserDisplayName: Alex
ThreadText: # Jenkins API call (or Groovy builtin) that returns list of all currently running builds of jobs
  
  (Asked by: username_0 on 2017-07-13)
  
  There's plenty of questions asking how to return a list of all jobs on your Jenkins instance, or even to get a list of which jobs are currently running, but I have a more specific use-case.
  
  I need to see if a single job is already running. As in, return one value if there is 1 builds of the job running, and a different value if 2 or more builds are running.
  
  The use case here is that we've automated our image creation to build after successful unit tests. It's set to build a maximum of one image every seven hours, by checking the build time of the last image and cancelling the build if it's been less than 7hrs. This works perfectly for most cases. However, these images take ~20min to build. If it's been over 7 hours and **two** unit tests finish within less than 20min of each other, the second build doesn't see the first creating, and continues building. We're getting two functionally identical images made once every 7hrs, only minutes apart. I'd like to check the status of the controller job to know if it's already running, so that the second build can know to cancel.
  
  I've tried curl-ing the following API, but it turns out it doesn't register individual builds, just that the job is running in general, which is useless in this case as it self-triggers since it checking means it's already running; there's no difference between a single build and multiple builds.
  
  ```
  http://JENKINS_URL/api/xml?tree=jobs[name,url,color]&xpath=/hudson/job[ends-with(color/text(),%22_anime%22)]&wrapper=jobs
  
  ```
  
  TL;DR: Is there a different call I can make that will return some list/file/content that indicates that multiple builds of the same job are currently running?
  
  **Tags:** jenkins, jenkins-pipeline
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-07-19. Score: 8 | Accepted answer
  
  Turns out you can hit the following URL to get an XML output of all currently running builds, including their build numbers.
  
  ```
  http://jenkinsURL/computer/api/xml?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds
  
  ```
  
  I `grep`ed it for a count and used that for my calculations.
  
  
  ---
  
  ### Answer by username_1 on 2021-02-22. Score: 0
  
  I used the Groovy script console to obtain it this way:
  
  ```
  def executables = Jenkins.instance.computers.collect {c -> c.executors}.
  def runs = Jenkins.instance.computers.collect {c -> c.executors}.
     flatten().
    findAll { executor -> executor.isBusy() }.
    collect { executor -> executor.getCurrentExecutable() }.
      collect { executable -> "${executable.displayName}: ${executable.number}" }
  
  ```
  
  or if you want the `Build` class, this:
  
  ```
  Jenkins.instance.computers.collect {c -> c.executors}.
    flatten().
    findAll { executor -> executor.isBusy() }.
    collect { executor ->
      def u = executor.currentExecutable.url
      def n = executor.currentExecutable.number
      def job_qual_name = u.split('/')[0..-2].findAll { it != 'job' }.join('/')
      def job = Jenkins.instance.getItemByFullName(job_qual_name, Job.class)
      job.getBuildByNumber(build.number)
    }
  
  ```
  
  It's pretty painful that Jenkins doesn't provide something as simple as this with a clear, high-level API.
  
  It's extra ugly that the paths used by the folders plugin jobs don't work with `Jenkins.getItemByFullName()` without munging. Ick. Also that `Jenkins.getItemByFullName()` doesn't seem to support getting a `Build`.
  
  You won't want to put this in Pipeline code. Instead put it in your script library so it isn't subject to method authorization issues, and only returns the final results.
  
  
  ---
  
  