Prevent Jenkins concurrent build job with same parameter

Optional Solutions

Option 1:  Install new Jenkins plugin: block-same-builds

Plugin Information:

 Plugins page: https://plugins.jenkins.io/block-same-builds

 ID: block-same-builds

 Latest Version: 1.3

 Minimum Jenkins requirement: 1.625.3

 Description: A plugin for Jenkins CI that blocks the running builds with the same configured parameters.

 

Screenshots:

 

Job configuration:

 

 

checkBuildJobProperty {

// Enter the parameters you want to check, separated by comma.

checkPars(String value)

on(boolean value)

}


Standalone Jenkins job DSL Example

properties {

    checkBuildJobProperty {

        checkPars('CLIENT_ID')

        on(true)

     }

}

 

How it works

In the example, "CLIENT_ID"  is the parameter set to check.

Build the job, with "CLIENT_ID": 12345, then rebuild the job with same  "CLIENT_ID": 12345.

When the first job is running, the rebuild job will stop immediately and turn to gray. The build will totally stop.

    

 

 

Click into the build 11, we can see the reason why build stopReasonThe job[jenkins-url] which has the same parameters triggered by user [username] is running!

 

 

Option 2: Update already exists Jenkins plugin "throttle-concurrent" to the new version

Plugin Information:

 plugins page: https://plugins.jenkins.io/throttle-concurrents

 plugin GitHub page: https://github.com/jenkinsci/throttle-concurrent-builds-plugin/blob/master/README.md

 ID: throttle-concurrents

 Latest Version: 2.0.1

 Minimum Jenkins requirement: 1.642.3

 Description: This plugin allows for throttling the number of concurrent builds of a project running per node or globally.

 

The new feature we need to solve the problemPrevent multiple jobs with identical parameters from running concurrently

This feature released version: 1.8.5

Current install version at https://lcjenkins.saas.bbpd.io/pluginManager/installed : 1.8.4

 

Screenshots:

 

Job configuration:

 

throttleJobProperty {

 

    // The maximum number of concurrent builds of this project (or category) to be allowed to run per node.

    maxConcurrentPerNode(Integer value)

 

    // The maximum number of concurrent builds of this project (or category) to be allowed to run at any one time, across all nodes.

    maxConcurrentTotal(Integer value)

     

    // Categories can be used to throttle multiple projects.

    categories(Iterable<String> value)

     

    throttleEnabled(boolean value)

     

    // input "project" for Throttle this project alone, input "category", Throttle this project as part of one or more categories

    throttleOption(String value)

     

    // If this box is checked, only one instance of the job with matching parameter values will be allowed to run at a given time.

    limitOneJobWithMatchingParams(boolean value)

    paramsToUseForLimit(String value)

     

    matrixOptions {

        throttleMatrixBuilds(boolean value)

        throttleMatrixConfigurations(boolean value)

 }

}

 

Standalone Jenkins job DSL Example

properties {

     throttleJobProperty {

        maxConcurrentPerNode(10)

        maxConcurrentTotal(10)

        throttleEnabled(true)

        throttleOption('project')

        // 'category'

        limitOneJobWithMatchingParams(true)

        paramsToUseForLimit('CLIENT_ID')

        matrixOptions {

          throttleMatrixBuilds(false)

          throttleMatrixConfigurations(false)

         }

     }

  }

 

How it works:

In the example, "CLIENT_ID"  is the parameter set to check.

Build the job, with "CLIENT_ID": 12345, then rebuild the job with same  "CLIENT_ID": 12345. 

                

When the first job is runningthe rebuild job will become to the pending state and will show why this job is pending

 

However, after the first job finished, the rebuild job will RUN with the same parameter.  This can prevent concurrent build with the same parameter on the one job, which can help to alleviate the pressure on servers

We can MANUALLY cancel this pending job based on this pending info, "A build with matching parameters is already running". 

The positive aspect of this method, no matter we wait for the rebuild finish or cancel the pending job manually,  there are no failed jobs or aborted jobs left on Build history panel, which can leave a clean build history for the operator.

                                After manually cancel the pending job, it will leave a clean build history

                                                     

Option 3: Call Jenkins API firstly in the script

Steps:

  • Call the Jenkins API in bash script shell, before the actual deployment job is running
  • Check the running status and build parameters,
  • List all running builds, abort the build if the duplicated parameter (CLIENT_ID) is found

 

We can list one Jenkins job all build status by curl the Jenkins API. 

For example, list my local Jenkins one job all build from http://192.168.33.12:8080/job/Parameter-Category-Test1/api/json?tree=builds[building,id,number,result]&pretty=true

{

  "_class" "hudson.model.FreeStyleProject",

  "builds" : [

    {

      "_class" "hudson.model.FreeStyleBuild",

      "building" true,                            # The building is true

      "id" "42",

      "number" 42,

      "result" null

    },

    {

      "_class" "hudson.model.FreeStyleBuild",

      "building" false,

      "id" "41",

      "number" 41,

      "result" "SUCCESS"

    },

    {

      "_class" "hudson.model.FreeStyleBuild",

      "building" false,

      "id" "40",

      "number" 40,

      "result" "SUCCESS"

    },

    {

      "_class" "hudson.model.FreeStyleBuild",

      "building" false,

      "id" "39",

      "number" 39,

      "result" "SUCCESS"

    },

    {

      "_class" "hudson.model.FreeStyleBuild",

      "building" false,

      "id" "38",

      "number" 38,

      "result" "SUCCESS"

    },

    {

      "_class" "hudson.model.FreeStyleBuild",

      "building" false,

      "id" "37",

      "number" 37,

      "result" "SUCCESS"

    },

    {

      "_class" "hudson.model.FreeStyleBuild",

      "building" false,

      "id" "36",

      "number" 36,

      "result" "ABORTED"

    },

 

If the building is true and the result is null, means the build is running, we can list all running builds, and check their parameters, for example, "CLIEND_ID"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章