简体   繁体   中英

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. I want to checkout multiple repositories in the jenkinsfile and make sure that the workspace is clean (git clean)

For checkout scm there's a checkbox to do exactly that. How can I reproduce this for the git checkout function in groovy? 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

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. (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. If you cannot achieve the functionality with the class, then it would also not be possible within the step method.

In this situation, the documentation for the step method confirms that a clean is not possible. 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. Therefore, in your situation you would indeed have to use git clean -fdx within the shell step method for the desired functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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