git sparse checkout within Jenkinsfile

By thomas, 11 July, 2019

I needed to checkout some code from another repository into my project, but I only wanted a specific directory within the other repository.

As usual with Jenkins, the docs are lacking, but reading the source for a bit I came up with the following:


dir('otherrepo') {
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[
$class: 'SparseCheckoutPaths',
sparseCheckoutPaths: [
[ $class: 'SparseCheckoutPath', path: 'goodthing/' ]]
]
],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'ACTIVE_DIRECTORY',
url: 'https://github.com/uphillian/otherrepo.git']]
])
}

According to the code, you should also be able to remove paths with `!` in the path, but I had trouble with this. I believe if the badpath is nested under the good path, it doesn't work.


dir('otherrepo') {
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[
$class: 'SparseCheckoutPaths',
sparseCheckoutPaths: [
[ $class: 'SparseCheckoutPath', path: '!goodthing/badthing/*', path: 'scripts/' ],
]
],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'ACTIVE_DIRECTORY',
url: 'https://github.com/uphillian/otherrepo.git']]
])
}

Doing this I only get the goodthing directory in the otherrepo directory in the workspace.