Jenkinsfile 有两个 git 个存储库

Jenkinsfile with two git repositories

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

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

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

目前我的 Jenkinsfile 看起来像这样

node {
  stage 'build'
  checkout scm

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

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

您可以使用 checkout 检出多个目录,但您必须指定要检出的目录。您可以使用 jenkins 生成代码片段(代码片段生成器在脚本字段下方)。 选择结帐,下一个 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'
}

可以找到另一个在单个管道中处理多个 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')
}