首页 > 解决方案 > 使用 Jenkins 管道将多个 git 存储库检出到子目录中

问题描述

我正在使用 Jenkins Multiple SCM 插件将两个 git 存储库检出到我的 Jenkins 作业中的两个子目录中。然后我执行一组命令来构建一组工件,其中包含从所有三个存储库中提取的信息和代码。但是分支测试被检出两次?工作区具有当前结构:

root
-.git
-all other files
-test
-res

我希望工作区具有这种结构。

root
-test
-res

           node (label: 'YYY'){
     checkout([  
                $class: 'GitSCM', 
                branches: [[name: '*/test']], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'test']], 
                submoduleCfg: [], 
                userRemoteConfigs: [[url: 'http://myrepo.git']]
            ])          
        
    dir('res') {
            git branch: 'master',  url: 'http://newrepo.git'
        }
    }
    
    pipeline {
        agent { label 'YYY' } 
        
        stages {
            xxx
        }
    }

标签: jenkinsjenkins-pipeline

解决方案


我为此使用了一个函数:

def checkoutGitRepo(final String repoUrl, final String repoBranch, final String folder) {
    //checkout in folder
    dir ("${folder}") {
        git(url: "${repoUrl}",
            credentialsId: 'jenkins-git-credential-id-here',
            branch: "${repoBranch}"
        )
    }
}

您可以将该函数放在共享库中,并在您需要的任何构建中调用它。


推荐阅读