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

  • make git branch the master branch
  • View git history for folder
  • How to get a copy of an older version of a file in a git repository?
  • How can I tell, from a script, if “git cherry-pick” fails?
  • How to diagnose and fix git fatal: unable to read tree
  • Find Git Revision of a Working Directory Missing the .git Directory
  • 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?

  • Jenkins: How to execute shell script in a Windows slave
  • I push my project to gitlab to test git ci,but it built fail
  • Git - Difference between amend and squash commands
  • Git: Recover the deleted remote branch
  • I get a command not found from my bashrc
  • git stash blunder: git stash pop and ended up with merge conflicts
  • 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.