12

In a jenkinsfile, I have specified the folderName through SparseCheckoutPaths which I want to checkout. But I am getting a whole branch checkout instead.

   checkout([$class: 'GitSCM', 
       branches: [[name: '*/branchName']],
       extensions: [[$class: 'SparseCheckoutPaths', path: 'FolderName']],
       userRemoteConfigs: [[credentialsId: 'someID',
       url: 'git@link.git']]])
HAr
  • 461
  • 1
  • 3
  • 9

3 Answers3

21

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: 'git@link.git']]])

for more info, here comes the github-link

Community
  • 1
  • 1
HAr
  • 461
  • 1
  • 3
  • 9
  • 1
    Thanks @HAr, that helps a lot. Would you happen to know if it is possible to do a sparse checkout and exclude a subdirectory at the same time? – Bernie Lenz Apr 30 '20 at 20:40
  • suppose if I have folder structure in the hub as - src/pub/java/app/ and in my workspace i need only the content of app/ . But I am getting the whole path in my workspace. How do I get the content of app/ directly into my jenkins workspace without the need to add dir( src/pub/java/app) in my pipeline – srikar kulkarni May 01 '20 at 10:30
  • For me at least, this still does a fetch of the complete repo first, and after that, sets the sparse checkout setting in the newly cloned repo. – sinned Apr 12 '21 at 12:04
4

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', 'another/path/'])
Chadi
  • 737
  • 1
  • 6
  • 21
1

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?

Nick Jones
  • 4,395
  • 6
  • 33
  • 44
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks for your response. Since, the checkout will be done by the Jenkins in one the slave node. So, I'm not sure how this config flag can be set. As far as I can remember, git conig flags can only be defined locally and we can't push such info to the repo. – HAr Apr 08 '17 at 15:46
  • @HAr Yes, I was thinking yabout the Git Plugin, which has a sparse checkout in its additionnal behaviors options (as seen in https://issues.jenkins-ci.org/browse/JENKINS-29278). Not sure it is compatible with pipelines though – VonC Apr 08 '17 at 15:48
  • @HAr See http://stackoverflow.com/questions/35873902/accessing-scm-git-variables-on-a-jenkins-pipeline-job: you could add as a first step in your pipeline a `git config core.sparsecheckout true` – VonC Apr 08 '17 at 15:50