How do I prevent two pipeline jenkins jobs of the same type to run in parallel on the same node?

后端 未结 10 1952
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 02:18

I do not want to allow two jobs of the same type (same repository) to run in parallel on the same node.

How can I do this using groovy inside Jenkinsfile ?

10条回答
  •  伪装坚强ぢ
    2020-12-01 03:03

    Another way is to use the Lockable Resources plugin: https://wiki.jenkins-ci.org/display/JENKINS/Lockable+Resources+Plugin

    You can define locks (mutexes) however you want and can put variables in the names. E.g. to prevent multiple jobs from using a compiler concurrently on a build node:

    stage('Build') {
        lock(resource: "compiler_${env.NODE_NAME}", inversePrecedence: true) {
          milestone 1
          sh "fastlane build_release"
        }
    }
    

    So if you wanted to prevent more than one job of the same branch running concurrently per node you could do something like

    stage('Build') {
        lock(resource: "lock_${env.NODE_NAME}_${env.BRANCH_NAME}", inversePrecedence: true) {
          milestone 1
          sh "fastlane build_release"
        }
    }
    

    From: https://www.quernus.co.uk/2016/10/19/lockable-resources-jenkins-pipeline-builds/

提交回复
热议问题