12

This seems too trivial to ask, but I could not find an answer:

In a Multibranch Pipeline project (controlled by Jenkinsfile): How can I skip previous builds on a branch, if there is a newer one and only build the latest commit and/or the tip of the branch?

     Build is running        
  ├────────────────────┤

o────────o────────o────────o
↑        ↑        ↑        ↑
Build    Skip     Skip     Build this


──────────────────────────────────>
                              (t)
Stefan Jung
  • 1,209
  • 11
  • 20
  • Possible duplicate of [How do I prevent two pipeline jenkins jobs of the same type to run in parallel on the same node?](https://stackoverflow.com/questions/36454130/how-do-i-prevent-two-pipeline-jenkins-jobs-of-the-same-type-to-run-in-parallel-o) – StephenKing Sep 21 '17 at 12:53
  • No, parallel jobs from the same repository is wanted behavior. But only one build per branch. – Stefan Jung Sep 21 '17 at 13:07
  • Lockable resource with the lock name including `env.BRANCH_NAME`? – StephenKing Sep 21 '17 at 13:12
  • @StephenKing you mean by using this plugin: https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin Wouldn't this also result in skipping the intermediate commits? – Stefan Jung Sep 21 '17 at 14:31
  • Your problem seems to be too trivial to understand. I don't get your intention. What do you mean by "skip a commit"? – StephenKing Sep 21 '17 at 15:01
  • 3
    The builds are quite complex. One build takes 2 hours to run, so the queue runs full. Therefore previous commits on the same branch should be skipped. – Stefan Jung Sep 21 '17 at 17:34
  • Ah, I see, of course the build will just be queued up with locks. Maybe together with the milestone step, but I'm not sure if that would help to only build the latest commit, once an older one is finished. – StephenKing Sep 21 '17 at 20:20
  • There was some throttle build plugin... – StephenKing Sep 21 '17 at 20:21
  • Did you find a solution for this? – CodeMonkey Jan 21 '20 at 16:36
  • I have left the company and do not use Jenkins anymore. I have not found a solution. – Stefan Jung Jan 22 '20 at 17:23
  • Does this answer your question? [Jenkins - abort running build if new one is started](https://stackoverflow.com/questions/40760716/jenkins-abort-running-build-if-new-one-is-started) – haridsv Jun 15 '21 at 05:06
  • 1
    A solution involves cancelling current running builds when a new commit is pushed, see: https://stackoverflow.com/questions/40760716/jenkins-abort-running-build-if-new-one-is-started/44326216 – haridsv Jun 15 '21 at 05:07

1 Answers1

2

Jenkins built-in workflow-job-plugin has disableConcurrentBuilds directive that takes one boolean parameter named abortPrevious. The parameter is undocumented in the official Jenkins documentation but is mentioned in the PR to the workflow-job-plugin and available since version 2.42 of the aforementioned plugin.

Having that in mind the feature to cancel previous builds can be enabled in Jenkins by:

  • using Scripted Pipeline:

    node {
        properties([
            disableConcurrentBuilds(abortPrevious: true)
        ])
        [...]
    }
    
  • using Declarative Pipeline:

    pipeline {
        agent any
        options {
            disableConcurrentBuilds(abortPrevious: true)
        }
        stages { ... }
    }
    
Marcin Kłopotek
  • 5,271
  • 4
  • 31
  • 51