【发布时间】:2019-11-01 14:23:24
【问题描述】:
此 stackoverflow 页面可以帮助您使用通用 webhook 触发器在 Jenkins 中检查 bitbucket 拉取请求。
先决条件:
詹金斯: 1) 在 Jenkins 中安装 Generic Webhook Trigger Plugin 和 Bitbucket 插件
2) 在 Jenkins 配置中配置“Bitbucket Endpoints”。
3) 在 Jenkins 中创建示例“管道”作业(可以配置通用 webhook 插件/配置 bitbucket 存储库/包含自定义 Jenkins 文件路径)
比特桶: 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
【问题讨论】:
-
您也可以尝试使用 GIT SCM 配置:``` 名称:FETCH_HEAD` RefSpec:+refs/heads/*:refs/remotes/origin/* 分支说明符:**/${BRANCH}其他行为: 1. 清除存储库并强制克隆 2. 结帐后清理 ``` 其中 BRANCH 值为 '$.pullRequest.fromRef.displayId' 从通用 Webhook 触发器发布参数中检索到。
标签: jenkins jenkins-pipeline bitbucket jenkins-plugins bitbucket-pipelines