How to clone a directory from a remote repo in a Jenkins pipeline script?

I have a large repo in GitHub Enterprise and need to clone a sub-directory from it on my Jenkins build server and just build that sub-directory. I am using a pipeline script and have this right now:

node {
    stage ('checkout') {
        git url: 'git@github.devops.mycompany.local:Org/MyLargeRepo.git'
    }
}

What I want is to clone from github.devops.mycompany.local:Org/MyLargeRepo/path/to/subproject

I know I probably need to use sparse checkouts, but can’t seem to work out how to configure that in a Jenkins pipeline script. Any ideas?

  • How to know which user answered a Jenkins-Pipeline input step?
  • How to get the git latest commit message and prevent the jenkins build if the commit message contains ?
  • How to disable command output in jenkins pipeline build logs
  • Jenkinsfile build log
  • Issue with Jenkins pipeline and java.nio.file.* methods
  • Job DSL to create “Pipeline” type job
  • Jenkins Copy Artifact unable to find folder/multiProjectPipeline/branchWithSlash
  • How to send “back to normal” notifications in Jenkins Declarative Pipeline?
  • One Solution collect form web for “How to clone a directory from a remote repo in a Jenkins pipeline script?”

    That looks like declarative pipeline, and not scripted pipeline

    With the latter, you can use the syntax seen in this answer, based on the hudson.plugins.git.extensions.impl.SparseCheckoutPaths class from The Jenkins Git Plugin:

    checkout([$class: 'GitSCM', 
        branches: [[name: '*/branchName']],
        doGenerateSubmoduleConfigurations: false,
        extensions: [
            [$class: 'SparseCheckoutPaths',  sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'folderName/']]]
                    ],
        submoduleCfg: [],
        userRemoteConfigs: [[credentialsId: 'someID',
        url: 'git@link.git']]])
    

    Don’t forget the .git/info/sparse-checkout file

    Git Baby is a git and github fan, let's start git clone.