SparseCheckout in Jenkinsfile pipeline

Here comes the answer to my own question. For a bit of background how does it work, there is flag/configuration for git client called sparsecheckout which is responsible for this kind of checkout. Additionally, a sparse-checkout named file is also required. For more info look here.

My problem was the syntax for the Jenkinsfile and correct one is as follows:

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

for more info, here comes the github-link


Your syntax looks good, but, as seen in "jenkinsci/plugins/gitclient/CliGitAPIImpl.java", did you specify the right configuration?

private void sparseCheckout(@NonNull List<String> paths) throws GitException, InterruptedException {

    boolean coreSparseCheckoutConfigEnable;
    try {
        coreSparseCheckoutConfigEnable = launchCommand("config", "core.sparsecheckout").contains("true");
    } catch (GitException ge) {
        coreSparseCheckoutConfigEnable = false;
    }

In other words, is git config core.sparsecheckout equal to true in the repo you are about to checkout?


You can define a custom step sparseCheckout in a shared library that adds on top of the existing checkout scm.

vars/sparseCheckout.groovy:

def call(scm, files) {
    if (scm.class.simpleName == 'GitSCM') {
        def filesAsPaths = files.collect {
            [path: it]
        }

        return checkout([$class                           : 'GitSCM',
                         branches                         : scm.branches,
                         doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
                         extensions                       : scm.extensions +
                                 [[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: filesAsPaths]],
                         submoduleCfg                     : scm.submoduleCfg,
                         userRemoteConfigs                : scm.userRemoteConfigs
        ])
    } else {
        // fallback to checkout everything by default
        return checkout(scm)
    }
}

Then you call it with:

sparseCheckout(scm, ['path/to/file.xml', 'path2])