SparseCheckout in Jenkinsfile pipeline

前端 未结 3 1545
猫巷女王i
猫巷女王i 2020-12-09 20:34

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

相关标签:
3条回答
  • 2020-12-09 21:01

    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])
    
    0 讨论(0)
  • 2020-12-09 21:11

    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

    0 讨论(0)
  • 2020-12-09 21:26

    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?

    0 讨论(0)
提交回复
热议问题