2016-05-23 153 views
12

我使用Jenkins管道插件和Jenkinsfile。Jenkinsfile与两个git存储库

在一个名为vms.git的仓库中,我有Jenkinsfile和它构建的应用程序。

我有另一个名为deploy.git的存储库,其中包含我想用于在vms.git中部署应用程序的脚本。

此刻我Jenkinsfile只是看起来像这样

node { 
    stage 'build' 
    checkout scm 

和我定义的作业配置的vms.git回购。

所以我想要做的是检查这两个存储库,然后使用vms.git中的Jenkinsfile来定义构建的其余部分。我想在其他管道中重用deploy.git脚本,所以我不想在其中放置Jenkinsfile。

回答

23

您可以使用checkout检出多个目录,但是您必须指定要检出的目录。你可以使用jenkins生成片段(Snippet generator bellow script field)。 选择结帐,下一个git存储库,并在其他行为选择:结帐到子目录。

当您将有2个存储库时,您可以从您想要使用的存储库加载脚本load。示例:

node { 
    // first repository 
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory1']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo1.git']]]) 
    // second repository 
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory2']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo2.git']]]) 
    // run first script 
    load 'subdirectory1/Jenkinsfile' 
    // run second script 
    load 'subdirectory2/Jenkinsfile' 
} 
+1

的'*/master'的被更充分地在Jenkinsfile DSL参考在'HTTP说明的含义:// jenkinshost:8080 /工作流程的CPS-snippetizer/dslReference' –

14

另一个在单个管道中处理多个Git存储库的优雅解决方案可以在at this thread找到。

node { 
    dir('RepoOne') { 
     git url: 'https://github.com/somewhere/RepoOne.git' 
    } 
    dir('RepoTwo') { 
     git url: 'https://github.com/somewhere/RepoTwo.git' 
    } 

    sh('. RepoOne/build.sh') 
    sh('. RepoTwo/build.sh') 
}