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

如何避免Pull Request未合并关闭时触发Terraform Plan任务

解决GitHub Workflow触发问题

你的问题出在Plan任务的条件判断里没有限定触发事件类型,导致PR关闭(closed事件)时只要满足无标签、目标分支是main就会执行该任务。只需给Plan任务的if条件增加事件类型判断即可。

修改后的完整Workflow代码如下:

name: Plan and merge to Main
on:
  pull_request: 
    types: [ labeled, closed, opened ]

jobs:
  Plan:
    name: "Terraform Plan"
    if: github.base_ref == 'main' && github.event.action == 'opened' && join(github.event.pull_request.labels.*.name, '') == ''

  Apply:
    name: "Run Terraform Apply"
    if: github.base_ref == 'main' && github.event.pull_request.merged == true && join(github.event.pull_request.labels.*.name, '') == ''

关键修改说明

  • 给Plan任务的if条件添加github.event.action == 'opened',明确只有当PR**创建(opened事件)**时才执行该任务,排除了关闭、加标签等其他触发场景。
  • Apply任务的条件无需修改,因为github.event.pull_request.merged == true已经确保只有PR合并(属于closed事件的子场景)时才会执行。

如果不需要监听labeled事件(比如PR加标签时无需触发Workflow),可以把on.pull_request.types里的labeled去掉,进一步精简触发条件:

on:
  pull_request: 
    types: [ closed, opened ]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 15:56:01