23

我正在使用带有 Jenkinsfile 的 Jenkins 管道插件。

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

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

目前我的 Jenkinsfile 看起来像这样

node {
  stage 'build'
  checkout scm

我正在作业配置中定义 vms.git 存储库。

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

4

2 回答 2

36

您可以使用 签出多个目录checkout,但您必须指定要签出的目录。您可以使用 jenkins(片段生成器波纹管脚本字段)生成片段。选择结帐,下一个 git 存储库并在 Additional Behaviors 中选择:结帐到子目录。

当您将拥有 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'
}
于 2016-05-23T17:00:05.603 回答
28

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

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')
}
于 2017-03-27T06:27:53.423 回答