Search code examples
jenkins

Jenkins disableConcurrentBuilds have multiple builds in queue


In our pipeline we want to execute the builds 1 by 1, so we do not want to run multiple builds of one specific pipeline at the same time, for all other pipelines it is no problem. We added options { disableConcurrentBuilds() } like:

pipeline {
    agent any
    options { disableConcurrentBuilds() }

When we trigger multiple builds, only 1 build is running at the time as excpected. However there is only 1 build queued (we can see this in blue ocean) after the 1st build. So if we start 10 builds after each other, build 1 is executed right away, build 2 is queued, all other builds are not queued and are never started. All builds we start should be executed 1 by 1, none should be skipped. How can we queue multiple builds?


Solution

  • Use Throttle Concurrent Builds Plugin.

    This plugin helps manage the concurrency of builds more flexibly than the disableConcurrentBuilds() option.

    Firstly install the Throttle Concurrent Builds Plugin from Jenkins Plugin Manager if not already installed.

    Configure Throttling:

    Go to the job configuration page of your pipeline. Scroll down to the Throttle Concurrent Builds section and check Throttle this project.

    Set the Maximum Total Concurrent Builds to 1 to ensure builds run one at a time.

    Optionally, you can configure Categories and assign the job to a category if you want to throttle builds across multiple jobs.

    Pipeline Configuration:

    Use the throttle() step in your pipeline script to apply the throttling settings.

    Here's an example of how you can configure your Jenkins pipeline script using the Throttle Concurrent Builds Plugin:

    pipeline {
    agent any
    options { 
        throttle(['category-name'])
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
                // Your build steps here
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
                // Your test steps here
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying...'
                // Your deploy steps here
            }
        }
    }
    

    }

    Replace ['category-name'] with the actual category name you configured in the Throttle Concurrent Builds settings.