Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build only latest commit on branch with Jenkins

Tags:

jenkins

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)
like image 733
Stefan Eike Avatar asked Sep 21 '17 11:09

Stefan Eike


People also ask

What is Multibranch in Jenkins?

What's a Jenkins Multibranch Pipeline? A multibranch job is simply a folder of pipeline jobs. For every branch you have, Jenkins will create a folder. So instead of creating a pipeline job for each of the branches you have in a git repository, you could use a multibranch job.

How do I allow concurrent builds in Jenkins?

Concurrent builds are enabled by default. That is why the is the disableConcurrentBuilds() function. So there is no need to add any additional code to your pipeline to enable concurrent builds.

What is the use of Multibranch pipeline in Jenkins?

The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.


1 Answers

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 { ... }
    }
    
like image 145
Marcin Kłopotek Avatar answered Sep 18 '22 11:09

Marcin Kłopotek