Bitbucket Pipeline如何在PR关闭/合并时添加清理步骤?
解决Bitbucket Pipeline特性环境清理问题
Bitbucket Pipelines确实没有专门针对PR关闭/合并的直接触发事件,但可以通过以下两种方案实现特性环境的清理:
方案1:基于分支删除的自动清理(推荐,适用于合并后删除特性分支的场景)
当PR合并后通常会删除对应的feature/*分支,我们可以利用分支删除事件触发清理步骤:
修改你的Pipeline YAML文件,添加分支删除触发的步骤:
image: atlassian/default-image:3 pipelines: pull-requests: feature/*: - step: runs-on: - self.hosted - linux.shell name: 部署特性环境 script: - echo "deploy my feature branch for review" # 新增分支删除触发的清理步骤 branches: feature/*: - step: runs-on: - self.hosted - linux.shell name: 清理特性环境 script: - echo "clean up the feature environment" # 仅在分支被删除时触发 when: event: deleted
方案2:手动触发清理(适用于PR关闭但保留分支的场景)
如果PR关闭后不删除特性分支,可以在PR的Pipeline里添加一个手动触发的清理步骤,或者在分支管道中配置手动触发选项:
方式A:PR管道内添加手动清理步骤
image: atlassian/default-image:3 pipelines: pull-requests: feature/*: - step: runs-on: - self.hosted - linux.shell name: 部署特性环境 script: - echo "deploy my feature branch for review" - step: runs-on: - self.hosted - linux.shell name: 手动清理特性环境 script: - echo "clean up the feature environment" # 设置为手动触发,PR关闭后手动运行此步骤 trigger: manual
方式B:分支管道内配置手动清理
image: atlassian/default-image:3 pipelines: pull-requests: feature/*: - step: runs-on: - self.hosted - linux.shell name: 部署特性环境 script: - echo "deploy my feature branch for review" branches: feature/*: - step: runs-on: - self.hosted - linux.shell name: 手动清理特性环境 script: - echo "clean up the feature environment" trigger: manual
补充说明
- 方案1的前提是团队有合并PR后删除特性分支的规范,这样能实现全自动清理
- 如果需要更精准的PR关闭触发(无论分支是否删除),可以结合Bitbucket的Webhook,监听PR的
closed状态事件,再调用Pipeline的触发API,但此方式需要额外的服务支持(比如自己写一个简单的中间服务处理Webhook请求)
内容的提问来源于stack exchange,提问作者gharbiBdr
相关产品推荐
相关产品推荐

