Search code examples
jenkinsjenkins-pipelinejenkins-pluginsjenkins-declarative-pipelinejenkins-git-plugin

How to cleanup pipeline before checkout of repository in Jenkinsfile


I want to make a clean before checkout operation which is described in Jenkins git plugin documentation:

Clean before checkout Clean the workspace before every checkout by deleting all untracked files and directories, including those which are specified in .gitignore. ...

But how can add this option to default checkout step which is doing as first step?

I feel that it should be an option extended by git plugin which can be included to options block of Jenkinsfile as described in docs:

The options directive allows configuring Pipeline-specific options from within the Pipeline itself. Pipeline provides a number of these options, such as buildDiscarder, but they may also be provided by plugins...

But how one should know which options and their names this plugin offer? Didn't find it in docs, also i may be wrong that clean before checkout should be placed in options block of Jenkinsfile.

Please help.


Solution

  • As already mentioned in the comments the way to go is to use skipDefaultCheckout() (Source) in your pipeline-options to not checkout the repository if the pipeline starts.

    skipDefaultCheckout

    Skip checking out code from source control by default in the agent directive.

    To get the repository manually you can use checkout scm (Source)

    pipeline {
        agent any
        options {
            skipDefaultCheckout()
        }
        stages {
            stage('Example') {
                steps {
                    // Cleanup before starting the stage
                    // deleteDir() / cleanWs() or your own way of cleaning up
    
                    // Checkout the repository
                    checkout scm 
    
                    // do whatever you like
                }
            }
        }
    }