pluginsjenkins-pipelinedeclarative

Jenkinsfile declarative script git plugin: how to set shallow clone and depth attributes


I've a declarative pipeline in Jenkinsfile where I'd line to add the shallow clone option, depth = 1, timeout = 30 in the git plugin. Currently my setup that is working is:

            git(
                credentialsId: 'MY_GIT_CREDENTIALS', 
                branch: "${params.BRANCH}", 
                url: "${env.BBSCM}"
            )

Could somebody help me adding to the statement the three desired parameters ?


Solution

  • To enable these additional customization options, you need to utilize the full and recommended SCM Step checkout method with the GitSCM class. After consulting the documentation, we see that the syntax and usage for your current and desired arguments would appear like:

    checkout([
      $class: 'GitSCM',
      branches: [[name: "*/${params.BRANCH}"]],
      extensions: [[
        $class: 'CloneOption',
        shallow: true,
        depth:   1,
        timeout: 30
      ]],
      userRemoteConfigs: [[
        url:           params.SCM_URL,
        credentialsId: 'MY_GIT_CREDENTIALS'
      ]]
    ])
    

    If you are new to Jenkins Pipeline, and this syntax and usage appears intimidating, note that in the future you can also use the Pipeline Syntax Snippet Generator for assistance.