如何在作业开始之前清除Jenkins管道中的工作区

时间:2017-09-08 14:29:06

标签: jenkins jenkins-pipeline

我需要在构建开始之前清除工作区。我尝试分阶段使用cleanDir(),但是在声明性管道中,首先检出并且当cleadDir的阶段运行时,检出代码也会被清除,这是不希望的。如何在声明性管道中签出之前清除工作区?

6 个答案:

答案 0 :(得分:9)

实际上,我必须根据最近对管道插件的更改来修改我的答案,例如:带有JENKINS-43507的GitHub Branch Source Plugin 2.2.0。

除了可以配置的不同分支发现行为之外,现在可以定义要采取的其他步骤,包括结账前清理(以及结账后清理):< / p>

enter image description here

管道执行中的结果输出将是

Cleaning workspace
  > git rev-parse --verify HEAD # timeout=10
Resetting working tree
 > git reset --hard # timeout=10
 > git clean -fdx # timeout=10

所以,非常接近你自己的呼叫git clean

答案 1 :(得分:6)

stage('Git') {
            steps {
                step([$class: 'WsCleanup'])
                checkout scm
            }
        }

WsCleanup可以解决这个问题

答案 2 :(得分:5)

使用VCS的方法,使用Git运行

ArrayList

答案 3 :(得分:0)

这样的事情应该有效:

            env.WORKSPACE = pwd()
            sh "rm ${env.WORKSPACE}/* -fr"

答案 4 :(得分:0)

稍微不同的方法是使用stash step首先存储您的代码:

stash includes: 'src/**', name: 'source-code'

之后,您可以删除当前工作区中的所有内容。在稍后阶段,您可以再次简单地unstash源代码:

unstash 'source-code'

stash / unstash的另一个优点是你可以用它在多个jenkins节点之间共享文件。

答案 5 :(得分:0)

在SCM配置的Additional Behaviours中添加Wipe out repository & force clone,如下图:

enter image description here

相关问题