简体   繁体   English

是否有正确的方法在 jenkins 管道 groovy 脚本中调用 git clean ?

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

I do not want to use checkout scm , that's the reason why I ask.我不想使用checkout scm ,这就是我问的原因。 I want to checkout multiple repositories in the jenkinsfile and make sure that the workspace is clean (git clean)我想检查 jenkinsfile 中的多个存储库并确保工作区是干净的(git clean)

For checkout scm there's a checkbox to do exactly that.对于checkout scm ,有一个复选框可以做到这一点。 How can I reproduce this for the git checkout function in groovy?如何在 groovy 中为 git 结帐 function 重现此问题? All i've found regarding this topic is to call git clean -fdx via shell call, but I'd prefer a clean solution in groovy over a shell call, if that's possible. All i've found regarding this topic is to call git clean -fdx via shell call, but I'd prefer a clean solution in groovy over a shell call, if that's possible.

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

Something like described here: https://support.cloudbees.com/hc/en-us/articles/226122247-How-to-customize-Checkout-for-Pipeline-Multibranch类似于此处描述的内容: 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
    [...]
}

but not for checkout but for the git function.但不是用于结帐,而是用于 git function。 (see the example above) (见上面的例子)

The git step method functionality is a subset of the GitSCM class specified as an argument within the workflow-scm-step plugin checkout method. git 步骤方法功能是GitSCM class 的子集,指定为workflow-scm-step插件checkout方法中的参数。 If you cannot achieve the functionality with the class, then it would also not be possible within the step method.如果您无法使用 class 实现该功能,那么在 step 方法中也是不可能的。

In this situation, the documentation for the step method confirms that a clean is not possible.在这种情况下,step 方法的文档确认无法进行清洁。 As mentioned in the question, the clean is only possible within the checkout if you want to make use of the Groovy bindings to Git.如问题中所述,如果您想使用 Groovy 与 Git 的绑定,则只能在checkout时进行清理。 Therefore, in your situation you would indeed have to use git clean -fdx within the shell step method for the desired functionality.因此,在您的情况下,您确实必须在 shell 步骤方法中使用git clean -fdx以获得所需的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM