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

16

这个问题与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 repo 中,并且 Jenkins 自动触发对 abc repo 的工作正常运行。但对于其他 repo,它无法正常工作。

有没有办法定义自动触发2个或更多的repo?

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

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

1个回答

10

您可以在流水线作业中使用“来自SCM的Pipeline脚本”选项,并指定多个存储库(单击“添加存储库”按钮),假设您可以监视3个存储库的相同分支,这似乎是您的情况。

enter image description here

通过这种配置(当然激活了“轮询SCM”选项),每次更改其中一个存储库时都会触发构建。

关于此解决方案的一些提示:

  1. 您需要在每个存储库中拥有Jenkinsfile
  2. 如果您在两个“SCM polls”之间提交了多个项目,则结果将是不可预测的(可能最终构建其中一个刚才提交的两个项目),因此您不应该依赖于构建哪个项目。
  3. 为了解决前面的问题并避免代码重复,您应该只从每个Jenkinsfile加载通用脚本,例如:

abc/def/ghi中的Jenkinsfile:

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...
    }
}

1
在我的情况下似乎不可行。不想维护多个Jenkinsfiles。管道作业不允许使用${env.BRANCH_NAME}获取当前分支名称。这种方式只创建单个作业,而我正在寻找根据分支名称拥有3个作业Dev、Stage、Prod。只需将以下代码放在common-pipeline.groovy中即可出现错误。<pre> node("slave"){ sh "git clone github/test.git" sh "git clone github/hello.git" sh "ls -la" } </pre> java.lang.NullPointerException: Cannot invoke method call() on null object at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77) - Nitin
Jenkins job auto trigger with multiple repositories :这不是一个单一的工作吗?否则,您不能使用${env.BRANCH_NAME},文档指定它仅适用于多分支项目。但是,您仍然可以使用一个小的sh脚本来获取它,例如sh "echo 'gitBranch=\$(git branch | cut -d ' ' -f 2)' >> properties.txt"。对于您的最后一个问题,请提出另一个问题,因为您的问题与原始问题无关且缺乏上下文。 - Pom12
感谢您的建议,但在我的情况下这不起作用,我无法转移到管道作业。除了多个存储库的SCM轮询外,“GitHub组织文件夹插件”一切正常运行。 - Nitin
3
我需要为每个代码库建立不同的分支,这种情况怎么处理? - pantonis
请注意,“添加仓库”按钮仍然放置在表单中间(在“要构建的分支”上方),而不是在底部(因此仍然很隐蔽),但它已经从左侧移动到了右侧(与所示的左侧相反)。 - mirekphd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接