首页 > 解决方案 > 使用通用 Webhook 参数检出 bitbucket 拉取请求的 GIT SCM 配置

问题描述

这个 stackoverflow 页面可以帮助您使用通用 webhook 触发器在 Jenkins 中签出 bitbucket 拉取请求。

先决条件:

Jenkins: 1) 在 Jenkins 中安装 Generic Webhook Trigger Plugin 和 Bitbucket 插件

2) 在 Jenkins 配置中配置“Bitbucket Endpoints”。

3) 在 Jenkins 中创建示例“管道”作业(可以配置通用 webhook 插件/配置 bitbucket 存储库/包含自定义 Jenkins 文件路径)

Bitbucket:4) 在 bitbucket repo 中配置 Webhook 以连接到 Jenkins webhook 插件并检查“事件” - 推送,添加评论。

现在假设您可以通过评论从您的 bitbucket PR 中触发 Jenkins 作业。

我在触发后克隆 bitbucket 拉取请求时遇到问题。它与 Git SCM 配置有关。

以下是我解决此问题的方法。

1) 在您的示例管道作业中,选中 Generic Webhook 插件下的“打印帖子内容”。您可以在作业控制台输出中看到 json 内容。

2)在“发布内容参数”下使用表达式值创建变量“ BRANCH ”:

$.pullRequest.fromRef.displayId 

(可以从控制台输出中的帖子内容派生)并检查“JSON 路径”。

下面是管道 Git SCM 配置:

Name: origin
RefSpec: +refs/heads/${BRANCH}
Branches to build
  Branch Specifier (blank for 'any'): **/pull-requests/**

以上述方式配置后,我成功解决了这个克隆 PR 的问题。

我尝试使用 bitbucket pr id,但当我尝试如下检查 PR 时仅在“管道脚本”中工作:

checkout([$class: 'GitSCM',
                        branches: [[name: 'FETCH_HEAD']],
                        extensions: [[$class: 'LocalBranch']],
                        userRemoteConfigs: [[refspec: "+refs/pull-requests/${PR_ID}/from:pr/${PR_ID}", credentialsId: '*****', url: 'https://stash***************.git']]
                        ])

其中 PR_ID 具有表达式值

$.pullRequest.id

标签: jenkinsjenkins-pipelinebitbucketjenkins-pluginsbitbucket-pipelines

解决方案


您也可以尝试使用 GIT SCM 配置:

Name: FETCH_HEAD`
RefSpec: +refs/heads/*:refs/remotes/origin/*
Branch Specifier: **/${BRANCH}
Additional Behaviours:
1. Wipe out repository & force clone
2. Clean after checkout 

其中 BRANCH 值是从通用 Webhook 触发器发布参数中检索到的“$.pullRequest.fromRef.displayId”。


推荐阅读