美文网首页
Jenkins workspace @2 问题解决

Jenkins workspace @2 问题解决

作者: 偷油考拉 | 来源:发表于2023-06-19 11:36 被阅读0次

背景:
在某次jenkins执行流水线任务时,多个人同时运行了某一任务。随后,其工作空间就变成 xxx@2了,导致编写的脚本调用路径失效。

一、@2 的成因

https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#ws-allocate-workspace
https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#allocating-workspaces

In addition to waiting to allocate an executor on a node, the node step also automatically allocates a workspace: a directory specific to this job — where you can check out sources, run commands, and do other work. Workspaces are locked for the duration of the step: only one build at a time can use a given workspace. If multiple builds need a workspace on the same node, additional workspaces are allocated.

If concurrent builds ask for the same workspace, a directory with a suffix such as @2 may be locked instead. Currently there is no option to wait to lock the exact directory requested; if you need to enforce that behavior, you can either fail (error) when pwd indicates that you got a different directory, or you may enforce serial execution of this part of the build by some other means such as the lock step.

If you do not care about locking, just use the dir step to change current directory.

二、@2 临时解决方案

重命名该任务,会以新任务名定义 workspace

三、 如何避免 @2 的现象

https://www.jenkins.io/doc/book/pipeline/syntax/#options

options { disableConcurrentBuilds() }
options { disableConcurrentBuilds(abortPrevious: true) }

避免并行任务的执行,如下范例:

pipeline {
    agent any    

    options {
        disableConcurrentBuilds()
    }

    stages {
        stage('testStage') {
            steps {
                script {sh "echo EXECUTOR_NUMBER is ${EXECUTOR_NUMBER}"}
                script {sh "echo WORKSPACE is ${WORKSPACE}"}
            }
        stage("Wait") {
            steps {
                sleep time: 45, unit: 'SECONDS' 
            }
        }
    }
}

相关文章

网友评论

      本文标题:Jenkins workspace @2 问题解决

      本文链接:https://www.haomeiwen.com/subject/nrxoddtx.html