Jenkins 使用Multiple Pipeline 和 Bitbucket Branch Source Plugin

Pipeline Multibranch Plugin
此插件实现:当有新分支被推送到代码库,就会在jenkins中自动创建与分支对应的job。
Bitbucket Branch Source Plugin
此插件支持Bitbucket与多分支pipeline的集成。它允许你用Bitbucket Cloud 或者Bitbucket server作为多分支project的源码库。具体有两种方法:

  • 单个仓库源: 监测指定的源码仓库,为分支和pull requests自动创建Job。当分支被删除后,也支持自动删除对应的job。

  • Team/Project folders: 根据指定的Bitbucket Team(Cloud)或 Project(Server)自动为所有可见的(有权限的)代码库创建多分支项目。

在使用多分支项目时,如果你想要提交代码自动触发对应的分支job build,则使用Bitbucket Branch Source Plugin是非常简单的。

Jenkins 配置(Bitbucket Branch Source Plugin)

Configure a Bitbucket Endpoint under Manage Jenkins > Configure System > Bitbucket Endpoints:
Jenkins 使用Multiple Pipeline 和 Bitbucket Branch Source Plugin

The option Manage Hooks may be enabled for a specific endpoint under Manage Jenkins > Configure System > Bitbucket Endpoints to allow Jenkins to automatically register webhooks to that endpoint. This is the recommended, quicker, easiest configuration.
Jenkins 使用Multiple Pipeline 和 Bitbucket Branch Source Plugin

The option Manage Hooks may be enabled for a specific endpoint under Manage Jenkins > Configure System > Bitbucket Endpoints to allow Jenkins to automatically register webhooks to that endpoint. This is the recommended, quicker, easiest configuration.

Notice that there are two different implementations to use for managing webhooks

  • Native: It will automatically create the webhook in the Webhooks section, which is the implementation of the native Bitbucket Webhooks
  • Plugin: It will automatically create the webhook in the Post Webhooks section, which is the implementation of the Bitbucket WebPost Hooks Plugin

Jenkins 多分支项目配置

要跟踪单个代码仓库,创建项目类型为Multibranch Pipeline 的item,并且使用Bitbucket branch source
Jenkins 使用Multiple Pipeline 和 Bitbucket Branch Source Plugin
配置SCM Source
Jenkins 使用Multiple Pipeline 和 Bitbucket Branch Source Plugin
一旦点击"保存"按钮,将通过webhooks自动监听事件,监听webhook的endpoint URL为 /bitbucket-scmsource-hook/notify.

配置Bitbucket Server

详细配置可参照此文档:How to Trigger Multibranch Jobs from Bitbucket Server?

以下直接上配置截屏
为repository (就是bitbucket 仓库)创建webhook:
Post Webhooks 是Bitbucket的一个插件,需要额外安装。安装后有Post Webhooks选项
Jenkins 使用Multiple Pipeline 和 Bitbucket Branch Source Plugin
在这个repository上创建Post Webhooks,配置如下

Jenkins 使用Multiple Pipeline 和 Bitbucket Branch Source Plugin

以上配置全部完成后,Jenkins将自动发现你指定的repository中包含的分支,并且如果分支代码中包含Jenkinsfile(配置jenkins item时指定的),则将自动为每一个分支创建一个对应的pipeline,并去构建。当你新建分支,或者删除分支,并且push到这个Bitbucket server 的repository中之后,jenkins将自动创建或删除与分支对应的job。