You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bitbucket Cloud中对合并到main/master的squash commit强制规范提交?

解决Bitbucket Cloud中Squash Commit强制规范提交的方案

你提到的merge checks确实是核心方向,Bitbucket Cloud默认没有内置conventional commit检查项,但可以通过以下几种方式实现:

1. 结合Bitbucket Pipelines + Merge Checks做自定义验证

这是最直接的方案,利用Pipeline编写脚本验证squash后的提交信息,再通过merge checks强制验证通过才能合并:

  • 步骤1:配置commitlint规则
    在仓库根目录添加commitlint.config.js,定义你的conventional commit规范:

    module.exports = {
      extends: ['@commitlint/config-conventional']
    };
    
  • 步骤2:编写Bitbucket Pipeline脚本
    创建bitbucket-pipelines.yml,添加合并前检查步骤验证squash提交信息:

    pipelines:
      pull-requests:
        'main,master':
          - step:
              name: Validate Squash Commit Message
              image: node:18
              script:
                - npm install @commitlint/cli @commitlint/config-conventional
                # 获取PR标题和描述(Bitbucket默认将其作为squash提交信息)
                - PR_TITLE=$(curl -s -u "$BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD" "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID" | jq -r '.title')
                - PR_DESCRIPTION=$(curl -s -u "$BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD" "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID" | jq -r '.description')
                - SQUASH_MESSAGE="$PR_TITLE\n\n$PR_DESCRIPTION"
                # 用commitlint验证提交格式
                - echo "$SQUASH_MESSAGE" | npx commitlint
    

    注意:需在仓库设置中创建带PR读取权限的App Password,作为环境变量BITBUCKET_APP_PASSWORD注入Pipeline。

  • 步骤3:启用Merge Checks
    进入仓库「Repository settings」→「Merge checks」,开启**"Require pipelines to pass before merging"**,确保只有目标分支为main/master时触发该检查。

2. 用Webhooks自定义合并拦截

如果不想用Pipeline,可以写一个简单后端服务,监听Bitbucket的pullrequest:merged webhook事件:

  • 触发时检查合并类型是否为squash、目标分支是否为main/master
  • 通过Bitbucket API获取合并后的提交信息,验证是否符合conventional规范
  • 若不符合,调用API撤销合并(需服务有仓库写入权限)

这个方案需要自行维护服务,适合具备开发能力的团队。

3. 强制PR描述同步为Squash提交信息

通过分支保护规则要求所有合并必须通过PR,团队内约定PR标题必须符合conventional commit格式,同时开启Bitbucket的「Squash commit message」设置为「Use pull request title and description」。配合Pipeline验证PR标题格式,间接确保squash后的提交信息合规。

内容的提问来源于stack exchange,提问作者Dave982

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 07:52:37