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

How to cleanup pipeline before checkout of repository in Jenkinsfile

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

Clean before checkout Clean the workspace before every checkout by deleting all untracked files and directories, including those which are specified in .gitignore. ...

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

我觉得它应该是一个由 git 插件扩展的选项,它可以包含到 Jenkinsfile 的 options 块中,如 docs:

中所述

The options directive allows configuring Pipeline-specific options from within the Pipeline itself. Pipeline provides a number of these options, such as buildDiscarder, but they may also be provided by plugins...

但是应该怎么知道这个插件提供了哪些选项和它们的名称呢?没有在文档中找到它,我也可能错了 clean before checkout 应该放在 Jenkinsfile 的 options 块中。

请帮忙。

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

skipDefaultCheckout

Skip checking out code from source control by default in the agent directive.

要手动获取存储库,您可以使用 checkout scm (Source)

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
            }
        }
    }
}