具有来自不同存储库的Jenkinsfile的Jenkins多分支管道 [英] Jenkins multibranch pipeline with Jenkinsfile from different repository

查看:257
本文介绍了具有来自不同存储库的Jenkinsfile的Jenkins多分支管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Git存储库,其中包含我要构建的代码,但我不允许"root"在其根目录中添加Jenkinsfile(这是Debian软件包,因此我无法将文件添加至上游源代码) .有没有一种方法可以将Jenkinsfile存储在一个存储库中,并从另一个存储库构建代码?由于我的代码存储库有多个分支要构建(每个Debian版本一个),因此应该是一个多分支管道.在代码或Jenkinsfile存储库中的提交应触发构建.

I have a Git repository with code I'd like to build but I'm not "allowed" to add a Jenkinsfile in its root (it is a Debian package so I can't add files to upstream source). Is there a way to store the Jenkinsfile in one repository and have it build code from another repository? Since my code repository has several branches to build (one for each Debian release) this should be a multibranch pipeline. Commits in either the code or Jenkinsfile repositories should trigger a build.

奖励复杂性:我有几个这样的代码/打包存储库,我想对所有这些重复使用相同的Jenkinsfile.因此,它应该以某种方式动态地获取要使用的正确的Git URL.在所有存储库中,要建立的分支名称相同.

Bonus complexity: I have several code/packaging repositories like this and I'd like to reuse the same Jenkinsfile for all of them. Thus it should somehow dynamically fetch the right Git URL to use. The branches to build have the same names across all repositories.

推荐答案

简单的答案是:您不能使用multibranch pipeline做到这一点.多分支管道仅被设计(至少到目前为止),以执行Pipeline script from SCM样式的特定管道,并在项目的根部使用固定的Jenkinsfile.

Short answer is : you cannot do that with a multibranch pipeline. Multibranch pipelines are only designed (at least for now) to execute a specific pipeline in Pipeline script from SCM style, with a fixed Jenkinsfile at the root of the project.

不过,您可以使用多分支项目为多分支 freestyle 项目制作的插件.首先,您需要像定义multibranch pipeline configuration一样定义multibranch freestyle configuration. 选择此新项目,如下所示:

You can however use the Multi-Branch Project plugin made for multibranch freestyle projects. First, you need to define your multibranch freestyle configuration just like you would with a multibranch pipeline configuration. Select this new item like shown below :

这种类型的配置与multibranch pipeline类型的行为完全相同,即,它将为您创建一个文件夹,其中包含您的配置名称以及每个自动检测到的分支的子项目.

This type of configuration will behave exactly same as the multibranch pipeline type, i.e. it will create you a folder with the name of your configuration and a sub-project for each branch it automatically detected.

那么实现应该是小菜一碟:

The implementation should then be a piece of cake :

  1. 在多分支配置中指定您的SCM存储库
  2. 像在标准的自由样式项目中一样,将另一个构建作为构建/后构建的一部分进行调用,除了必须调用一个参数化的作业(将其命名为build-job)并为其提供存储库信息,即Git URL和当前分支(您可以为此使用预定义的变量$GIT_URL$GIT_BRANCH)
  3. 在您的build-job中,只需定义从SCM中检出的内联管道或管道脚本,然后在此脚本中执行SCM检出并继续进行所需的构建步骤. build-job管道内容示例:
  1. Specify your SCM repository in the multibranch configuration
  2. Call another build as part of your build/post-build as you would do in a standard freestyle project, except that you have to call a parameterized job (let's call it build-job) and give it your repository information, i.e. Git URL and current branch (you can use the pre-defined variables $GIT_URL and $GIT_BRANCH for this purpose)
  3. In your build-job, just define either an inline pipeline or a pipeline script checked out from SCM, and inside this script do a SCM checkout and go on with the steps you need to build. Example of build-job pipeline content :

.

node() {
  stage 'Checkout'
  checkout scm: [$class: 'GitSCM', branches: [[name: '*/${GIT_BRANCH}']], userRemoteConfigs: [[url: '${GIT_URL}']]]

  stage 'Build'
  // Build steps...
}

当然,如果您需要对不同的多分支项目进行一些区别对待,则还可以使用中间项目(例如build-project-Abuild-project-B,...),这些项目又会调用通用的build-job管道)

Of course if your different multibranches projects need to be treated a bit differently, you could also use intermediate projects (let's say build-project-A, build-project-B, ...) that would in turn call the generic build-job pipeline)

该解决方案的一个主要缺点是,您将只有一份工作负责所有构建,因此调试起来更加困难.万一成功/出错,您的多分支项目仍将变成蓝色/红色,但是您必须返回到build-job来查找构建的真正问题.

The one, major drawback of this solution is that you will only have one job responsible for all of your builds, making it harder to debug. You would still have your multibranch projects going blue/red in case of success/error but you will have to go back to called build-job to find the real problem of your build.

这篇关于具有来自不同存储库的Jenkinsfile的Jenkins多分支管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆