<p>This should be fairly straightforward to do in a script block.  I don't think there's a declarative-native way to do this.</p>
<p>I use this script block to cancel builds for earlier (i.e. outdated) revisions of PRs:</p>
<pre><code>script {
  // currentBuild is a Pipeline-builtin variable for accessing the Build object
  // https://www.jenkins.io/doc/book/pipeline/getting-started/#global-variable-reference
  def build = currentBuild

  while(true) {
    build = build.getPreviousBuild()

    // exit the loop if we have finished iterating over builds
    // (groovy doesn't have a do...while feature)
    if (build == null) break

    def rawBuild = build.rawBuild

    if (rawBuild.isBuilding()) {
      // abort the build
      rawBuild.doTerm()
    }
  }
}
</code></pre>
<p>As you can hopefully see, this iterates over <strong>all</strong> previous builds and uses the <code>isBuilding()</code> method to find ones that are currently running.</p>
<p>If you want to check later builds instead of previous ones, you can use <code>getNextBuild()</code> instead (I believe the API doc is <a href="https://javadoc.jenkins-ci.org/hudson/model/Run.html" rel="nofollow noreferrer">here</a>, it's been a while since I wrote this so this might be the wrong class).</p>
<p>And if you want to check only one directly adjacent build instead of all earlier/later builds, you can just omit the loop and only call <code>getPreviousBuild()</code>/<code>getNextBuild()</code> once.</p>

CC BY-SA 4.0
2020-11-27T22:08:34.497Z
2020-11-27T22:08:34.497Z
jayhendren
<p>I have a github multibranch pipeline, in declarative format. It is configured to trigger on pushes to pull request branches (ie, branches which are included in pull requests to a target branch, say <code>main</code>.</p>
<p>If a new commit comes in before the execution of a previous commit is finished, this will trigger a concurrent build of the pipeline. I want to maintain this behaviour, but <strong>need to know if there is a new pipeline running</strong> in one of the stages.</p>
<p>Ie I want a groovy script step like this pseudo code:</p>
<pre><code>if (build_number + 1) is_currently_running
  do one thing
else
  do something else
</code></pre>
<p>Is there any way to ask Jenkins if build n+1 is running?</p>

<jenkins><jenkins-pipeline><jenkinsfile>
How can I check if there is a new build the same jenkins pipeline
Bruce Becker
# How can I check if there is a new build the same jenkins pipeline

(Asked by: username_0 on 2020-11-25)

I have a github multibranch pipeline, in declarative format. It is configured to trigger on pushes to pull request branches (ie, branches which are included in pull requests to a target branch, say main.
If a new commit comes in before the execution of a previous commit is finished, this will trigger a concurrent build of the pipeline. I want to maintain this behaviour, but need to know if there is a new pipeline running in one of the stages.
Ie I want a groovy script step like this pseudo code:
if (build_number + 1) is_currently_running
  do one thing
else
  do something else

Is there any way to ask Jenkins if build n+1 is running?

**Tags:** jenkins, jenkins-pipeline, jenkinsfile


---

## 1 answers

### Answer by username_1 on 2020-11-27. Score: 1 | Accepted answer

This should be fairly straightforward to do in a script block.  I don't think there's a declarative-native way to do this.
I use this script block to cancel builds for earlier (i.e. outdated) revisions of PRs:
script {
  // currentBuild is a Pipeline-builtin variable for accessing the Build object
  // https://www.jenkins.io/doc/book/pipeline/getting-started/#global-variable-reference
  def build = currentBuild

  while(true) {
    build = build.getPreviousBuild()

    // exit the loop if we have finished iterating over builds
    // (groovy doesn't have a do...while feature)
    if (build == null) break

    def rawBuild = build.rawBuild

    if (rawBuild.isBuilding()) {
      // abort the build
      rawBuild.doTerm()
    }
  }
}

As you can hopefully see, this iterates over all previous builds and uses the isBuilding() method to find ones that are currently running.
If you want to check later builds instead of previous ones, you can use getNextBuild() instead (I believe the API doc is here, it's been a while since I wrote this so this might be the wrong class).
And if you want to check only one directly adjacent build instead of all earlier/later builds, you can just omit the loop and only call getPreviousBuild()/getNextBuild() once.


---

