SparseCheckout in Jenkinsfile pipeline

前端 未结 3 1548
猫巷女王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])
    

提交回复
热议问题