如何在Jenkins中为pipelineJob添加lockableResources锁?

m4pnthwp  于 9个月前  发布在  Jenkins
关注(0)|答案(2)|浏览(197)

我正在尝试为我的Jenkins管道添加一个锁,这样管道的多个示例就不会同时运行。
我在plugins.txt中安装了lockable-resources插件,并在casc.yaml中声明了资源

lockableResourcesManager:
    declaredResources:
      - name: "host-1-lock"
        description: "Lock to run jobs on host 1

字符串
我有下面的pipelineJob,我试图添加锁,以便在pipelineJob的持续时间内保持锁,然后在

pipelineJob('Test_Job-2') {
    quietPeriod(0)
    triggers {
        cron('H 6 * * *')
    }
    lockableResources('host-1-lock')
    parameters {
        labelParam('Node_Select') {
            defaultValue('host1')
            allNodes('allCases', 'IgnoreOfflineNodeEligibility')
        }
    }
    definition {
        cps {
            script(readFileFromWorkspace('scripts/mypipeline.jenkinsfile'))
        }
    }
}


然而,我得到的错误

ERROR: (script, line 6) No signature of method: javaposse.jobdsl.dsl.jobs.WorkflowJob.lockableResources() is applicable for argument types: (java.lang.String) values: [host-1-lock]


我试过使用语法锁(“host-1-lock”),如文档https://github.com/jenkinsci/lockable-resources-plugin中所指定的,但这会产生类似的错误。
我也尝试过在定义{ cp {...}}中在script()之前和script()中包含lock(),但没有成功。
谁能举个例子说明如何做到这一点?
最新消息:
看起来我在错误的地方使用了锁,相反,我尝试将其添加到脚本本身。
即scripts/mypipeline.jenkinsfile:

pipeline {
    options {
        ansiColor('xterm')
    }
    agent any
    stages {
        lock('host-1-lock')
        stage ('Build 1') {
            when {
                allOf {
                    expression { return params.RUN_BUILD }
                }
            }
            steps {
                catchError(stageResult: 'FAILURE') {
                    retry(count: 7) {
                        timeout(time: 180, unit: 'MINUTES') {
                            script {
                                def result = buildLabJob(STAGE_BUILD)
                                sh "exit $result"
                            }
                        }
                    }
                }
            }
            post {
                failure {
                    log("warn", "Build failed, skipping remaining stages")
                    script {
                        skipRemainingStages = true
                    }
                }
            }
        }
        stage ('Build Cluster DPU') {
            when {
                allOf {
                    expression { return params.RUN_BUILD }
                }
            }
            steps {
                catchError(stageResult: 'FAILURE') {
                    retry(count: 7) {
                        timeout(time: 360, unit: 'MINUTES') {
                            script {
                                def result = buildLabJob(STAGE_BUILD_JOBS)
                                sh "exit $result"
                            }
                        }
                    }
                }
            }
            post {
                failure {
                    log("warn", "Build failed, skipping remaining stages")
                    script {
                        skipRemainingStages = true
                    }
                }
            }
        }

        stage ('Deploy Custom') {
            when { 
                allOf {
                    expression { !skipRemainingStages }
                    expression { return params.RUN_CUSTOM }
                }
            }
            steps {
                catchError(stageResult: 'FAILURE') {
                    script {
                        def result = buildLabJob(STAGE_CUSTOM_JOBS)
                        sh "exit $result"
                    }
                }
            }
            post {
                failure {
                    script {
                        skipRemainingStages = true
                    }
                }
            }        
        }
}


但是这也会产生一个错误,说锁是未定义的。我看到文档显示锁在一个阶段中使用,但我需要在多个阶段中保持锁。这可能吗?
更新2:
我发现了一种方法,那就是添加实际的stages来获取和返回锁

stage('Acquire Lock') {
        steps {
            lock(resource: 'Lab240-lock') {
                echo 'Lock acquired'
            }
        }
    }
...other stages...
    stage('Release Lock') {
        steps {
            lock(resource: 'Lab240_lock', inversePrecedence: true) {
                echo 'Lock released'
            }
        }
    }


但是,我没有看到所需的行为。(运行相同的脚本),而pipeline 1仍在运行各个阶段,并立即获得锁并开始运行其阶段。由于这些作业使用相同的执行器,因此管道轮流运行阶段,但是,我需要锁来防止pipeline 2运行任何东西,直到pipeline 1完成。

rwqw0loc

rwqw0loc1#

你把它放在了错误的地方。锁的使用不应该是作业的一部分(又名作业定义),而是在实际的管道中,在你的情况下在脚本/mypipeline.jenkinsfile`中。
请仔细查看https://github.com/jenkinsci/lockable-resources-plugin#using-a-resource-in-a-pipeline-job中的示例

相关问题