首页 > 解决方案 > 有没有在詹金斯管道常规脚本中调用 git clean 的正确方法?

问题描述

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

因为checkout scm有一个复选框可以做到这一点。如何在 groovy 中为 git checkout 功能重现这个?我发现关于这个主题的所有内容都是git clean -fdx通过 shell 调用进行调用,但如果可能的话,我更喜欢 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 功能。(见上面的例子)

标签: gitjenkinsgroovyjenkins-pipeline

解决方案


git step 方法功能是在插件方法中指定为参数的GitSCM类的子集。如果您无法通过类实现功能,那么在 step 方法中也是不可能的。workflow-scm-stepcheckout

在这种情况下,step 方法的文档确认无法进行清洁。checkout如问题中所述,只有在您想使用 Groovy 绑定到 Git 时才能进行清理。因此,在您的情况下,您确实必须git clean -fdx在 shell step 方法中使用所需的功能。


推荐阅读