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

Viewed 564

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 -fdxvia 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)

1 Answers

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.

Related