在 jenkins pipeline groovy 脚本中有调用 git clean 的正确方法吗?

Is there a proper way to call git clean in jenkins pipeline groovy script?

我不想使用checkout scm,这就是我问的原因。 我想检查 jenkinsfile 中的多个存储库并确保工作区是干净的(git 干净)

对于 checkout scm,有一个复选框可以做到这一点。如何在 groovy 中为 git 结帐功能重现此内容?关于这个主题,我发现的所有内容都是通过 shell 调用来调用 git clean -fdx,但如果可能的话,我更喜欢 groovy 中的干净解决方案而不是 shell 调用.

  def checkoutGit(def cred,def repo, def branch)
  {
    git credentialsId: cred, url: repo, branch: branch
  }

类似于此处描述的内容: https://support.cloudbees.com/hc/en-us/articles/226122247-How-to-customize-Checkout-for-Pipeline-Multibranch

node {
    checkout([
        $class: 'GitSCM',
        branches: scm.branches,
        extensions: scm.extensions + [[$class: 'CleanCheckout']],
        userRemoteConfigs: scm.userRemoteConfigs
    ])
      
    //Build, Test, Stage, Deploy
    [...]
}

但不是用于结帐而是用于 git 功能。 (见上例)

git step method functionality is a subset of the GitSCM class 指定为 workflow-scm-step 插件 checkout 方法中的参数。如果您无法使用 class 实现功能,那么在步骤方法中也无法实现。

在这种情况下,步骤方法的文档确认无法进行清理。如问题中所述,如果您想使用 Git 的 Groovy 绑定,则只能在 checkout 内进行清理。因此,在您的情况下,您确实必须在 shell 步骤方法中使用 git clean -fdx 以获得所需的功能。