Jenkins 管道代码通过 GitHub 组织文件夹插件自动触发多个存储库

Jenkins pipeline code auto trigger with multiple repositories through GitHub Organization Folder Plugin

此问题与具有多个存储库的 Jenkins 作业自动触发有关。

在 Jenkinsfile 中定义了 3 个要签出的回购协议。

 node('slave'){
 git clone github.com/owner/abc.git -b ${env.BRANCH_NAME}
 git clone github.com/owner/def.git -b ${env.BRANCH_NAME}
 git clone github.com/owner/ghi.git -b ${env.BRANCH_NAME}
 }

使用 Github 组织插件配置 Jenkins 作业。

在这种情况下,我的 Jenkinsfile 在 abc 存储库中,Jenkins 自动触发器在 abc 存储库中运行良好。它不适用于其他回购协议。

是否有为 2 个或更多 repo 定义自动触发器的方法?

是否有任何插件可以自动触发 2 个或更多存储库的作业?

我需要在 Jenkinsfile 中定义 "checkout scm" 不同的方式吗?

是的,您可以通过指定多个存储库(单击 Add Repository 按钮)在管道作业中使用 Pipeline script from SCM 选项来做到这一点,假设您可以为 3 个存储库监视同一个分支,这似乎是你的情况。

使用此配置(当然还有 Poll SCM 选项已激活),每次对三个存储库之一进行更改时都会触发构建。

关于此解决方案的更多提示:

  1. 您需要在每个存储库中 一个 Jenkinsfile
  2. 如果你在两个 SCM polls 之间承诺了一个以上的项目,结果将是不可预测的(你刚刚承诺的两个项目中的任何一个都可能最终建成)所以你不应该 取决于构建的项目.
  3. 为了解决前面的问题并避免代码重复,您可能应该只从每个 Jenkinsfile 加载一个通用脚本,例如:

abc/def/ghi 中的 Jenkins 文件:

node {
    // --- Load the generic pipeline ---
    checkout scm: [$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://github/owner/pipeline-repo.git']]]
    load 'common-pipeline.groovy'
}()

common-pipeline.groovy 脚本:

{ ->
    node() {
       git clone github.com/owner/abc.git
       git clone github.com/owner/def.git
       git clone github.com/owner/ghi.git            

       // Whatever you do with your 3 repos...
    }
}