简体   繁体   中英

Jenkins lock/exclusion based on job parameters?

I have some Jenkins jobs (say job-A and job-B), but need to prevent any of them running simultaneously if a particular parameter is the same. So if job-A is started with parameter foo=42, and an attempt is made to run job-B with parameter foo=42, that attempt must be prevented. But running job-B with parameter foo=17 (while job-A|foo=42) is running is fine (as is running job-A|foo=17).

I can figure out a way to do this using shell scripts and lock files, but wondering if it is possible within Jenkins itself or using a plugin.

Here is how you can get this done with the Lockable Resource Plugin , which allows you to acquire locks on resources and wait until they are released to perform certain pipeline operations.

First, create a lock called foo42lock , and the following are two sample pipelines. Since you don't want to wait until the lock is released you can set skipIfLocked: true flag when acquiring the lock, so the pipeline will continue if the lock is not acquirable.

JobA

pipeline {
    agent any
    parameters { string(name: 'foo', defaultValue: '', description: '') }
    stages {
        stage("StageA") {
            steps {
                script {
                    if(foo == "42") {
                        lock(resource: 'foo42lock', skipIfLocked: true) {
                            echo "Foo is 42 Lock aquired and Job A Running"
                            runSteps()
                        }
                    } else {
                        echo "No lock needed"
                        runSteps()
                    }
                        
                }
            }

        }
    }
}

def runSteps(){
    echo "Storing the steps externally to reuse. Running Job A"
    sleep 30
}

JobB

pipeline {
    agent any
    parameters { string(name: 'foo', defaultValue: '', description: '') }
    stages {
        stage("StageB") {
            steps {
                script {
                    if(foo == "42") {
                        lock(resource: 'foo42lock', skipIfLocked: true) {
                            echo "Foo is 42 Lock aquired and Job B Running"
                            runSteps()
                        }
                    } else {
                        echo "No lock needed"
                        runSteps()
                    }
                        
                }
            }

        }
    }
}

def runSteps(){
    echo "Storing the steps externally to reuse. Running Job B steps"
    sleep 30
}

Another way to achieve this would be, whenever either JobA or JobB is triggered if foo==42 then JobA can check if there is a build happening for JobB and whether the parameter is set to foo=42 and if a build is happening it can stop the build, and vice versa. Implementing this is also not that complex and can use Groovy scripts to implement the logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM