Jenkins Git Sparsecheckout multiple repositories

190 Views Asked by At

I'm trying to setup a shared library within Jenkins that would have multiple git repositories and then multiple directory paths? Is this even possible? I've looked around a bit and the example's I have seen were here SparseCheckout in Jenkinsfile pipeline and then here Can I augment scm in Jenkinsfile? which just seems to say the same thing as above. I used the snippet generator to help create it but I do not know how it would call the specific repo and directory within the pipeline. Any advice or assistance is much appreciated. Here is the code from the snippet generator.

I just borrowed the define function from the stack overflow post above.

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

        return checkout([$class: 'GitSCM', 
                         branches: [[name: '${GIT_BRANCH}']], 
                         extensions: [[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [[path: 'repo1/foo/'], [path: 'repo1/bar/'], [path: 'repo1/mike/'], [path: 'repo2/'], [path: 'repo3/']]]], 
                         userRemoteConfigs: [[url: 'repo1'], [url: 'repo2'], [url: 'repo3']]])

    } else {
        // fallback to checkout everything by default
        return checkout(scm)
    }
}
1

There are 1 best solutions below

0
On

You can create a new GitSCM object and pass it to the function. Example below. Also, have a look at this Class. You can use the costructor that suites you and update the checkout function accordingly.

import hudson.plugins.git.*;

pipeline {
    agent any
    stages {
        stage('Stage C') {              
                steps {
                  script {
                        def url = "[email protected]:xxx/sample.git"
                        def credentials = 'xxxxx'
                        def scmLocal = new GitSCM(GitSCM.createRepoList(url, credentials), Collections.singletonList(new BranchSpec("*/main")), false, Collections.<SubmoduleConfig>emptyList(),
                                            null, null, Collections.emptyList())
                                            
                        callCheckout(scmLocal, ['path/to/file.xml'])
                        
                  }
                }
            }
    }
}

def callCheckout(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)
    }
}