如何在 Jenkinsfile 中签出存储库之前清理管道

Mik*_* P. 10 jenkins jenkins-plugins jenkins-pipeline jenkins-declarative-pipeline jenkins-git-plugin

我想进行Jenkins git 插件文档clean before checkout中描述的操作:

签出前清理 在每次签出之前通过删除所有未跟踪的文件和目录(包括 .gitignore 中指定的文件和目录)来清理工作区。...

但是如何将此选项添加到作为第一步执行的默认结账步骤中?

我觉得它应该是 git 插件扩展的一个选项,可以包含到optionsJenkinsfile 块中,如文档中所述:

options 指令允许从 Pipeline 本身配置特定于 Pipeline 的选项。Pipeline 提供了许多这样的选项,例如 buildDiscarder,但它们也可能由 插件提供......

但是如何知道这个插件提供了哪些选项及其名称呢?在文档中没有找到它,我也可能是错误的,clean before checkout应该放在optionsJenkinsfile 块中。

请帮忙。

mke*_*erz 13

正如评论中已经提到的,方法是在管道选项中使用skipDefaultCheckout()( Source ),以便在管道启动时不检出存储库。

跳过默认结账

默认情况下,在代理指令中跳过从源代码管理中检出代码。

要手动获取存储库,您可以使用checkout scmSource

pipeline {
    agent any
    options {
        skipDefaultCheckout()
    }
    stages {
        stage('Example') {
            steps {
                // Cleanup before starting the stage
                // deleteDir() / cleanWs() or your own way of cleaning up

                // Checkout the repository
                checkout scm 

                // do whatever you like
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)