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

  • tortoisegit clone option is unavailable in right-click context menu
  • Git push --force from IntelliJ IDEA
  • Restore my application with git
  • Try to add library to gradle
  • Compare git branch with rebased branch
  • How do I get the interdiff between these two git commits?
  • 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?

  • Git submodules vs Nuget packages
  • Clone a git repository all the way back to a specific commit, but not everything
  • git replace a commit by a commit from another branch
  • Plastic SCM with git in bitbucket
  • Replace working copy of git repo with actual contents of repo in .git?
  • Ignore future modifications of a .gitignore'd file which was added with git add -f
  • 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.