如何在Jenkins声明式流水线中配置Bitbucket PR触发器?
Bitbucket PR 创建/更新触发Jenkins声明式流水线配置方案
前置准备
- 确保Jenkins已安装Bitbucket Server Integration插件(注意区分Bitbucket Cloud插件,你的配置针对Bitbucket Server)
- 在Bitbucket仓库的「设置→Webhooks」中配置指向Jenkins的webhook:
- URL填
http://<你的Jenkins地址>/bitbucket-hook/ - 勾选事件:
Pull Request Created和Pull Request Updated
- URL填
修正后的流水线配置
你的原配置不生效大概率是isToApprove参数设置错误,或分支匹配规则、webhook未配置导致。以下是可直接使用的配置:
pipeline { agent any triggers { bitBucketTrigger([ // PR创建时触发 pullRequestServerCreatedAction( allowedBranches: 'origin/.*', // 匹配所有origin下的分支,可按需修改正则 isToApprove: false // 设为false则PR一创建就触发,设为true需审批后触发 ), // PR更新时触发 pullRequestServerUpdatedAction( allowedBranches: 'origin/.*', isToApprove: false ) ]) } stages { stage('触发验证') { steps { echo "Bitbucket PR 触发成功,分支: ${env.BRANCH_NAME}" echo "PR ID: ${env.BITBUCKET_PR_ID}" } } // 你的其他阶段... } }
常见问题排查
- 如果仍不触发,先检查webhook是否正常:在Bitbucket的webhook日志中查看请求是否发送成功,Jenkins的系统日志中是否有webhook接收记录
- 分支匹配规则可按需调整,比如只允许feature分支触发:
origin/feature/.* - 若
isToApprove设为true,需确保PR已通过审批才会触发流水线 - 确认Jenkins服务账号有Bitbucket仓库的读取权限,且Jenkins未拦截webhook请求
内容的提问来源于stack exchange,提问作者trophi20
相关产品推荐
相关产品推荐

