Jenkins Git稀疏检出多个存储库

rmbxnbpk  于 2023-03-01  发布在  Jenkins
关注(0)|答案(1)|浏览(200)

我试图在Jenkins中建立一个共享库,它将有多个git仓库和多个目录路径?这可能吗?我四处查看了一下,我看到的例子是这里的SparseCheckout in Jenkinsfile pipeline,然后这里的Can I augment scm in Jenkinsfile?,看起来和上面说的一样。我使用了snippet生成器来帮助创建它,但我不知道它将如何调用管道中的特定repo和目录。任何建议或帮助都非常感谢。以下是snippet生成器的代码。
我只是从上面的堆栈溢出帖子中借用了define函数。

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)
    }
}
dwthyt8l

dwthyt8l1#

你可以创建一个新的GitSCM对象并将其传递给函数。示例如下。另外,看看这个Class。你可以使用适合你的构造函数并相应地更新checkout函数。

import hudson.plugins.git.*;

pipeline {
    agent any
    stages {
        stage('Stage C') {              
                steps {
                  script {
                        def url = "git@github.com: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)
    }
}

相关问题