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

GitHub Actions工作流:PR合并入main或手动触发的配置问询

GitHub Actions 工作流配置解决方案

问题分析

你当前的配置里,if: github.event.pull_request.merged == true会导致手动触发(workflow_dispatch)时无法执行——因为手动触发事件不存在pull_request上下文,条件永远不成立。你尝试的github.event.workflow_run.requested是无效写法,workflow_dispatch事件的上下文里并没有workflow_run对象。

正确配置写法

要同时实现「PR合并到main分支自动运行」和「支持手动触发」的需求,你可以修改if条件,通过事件类型区分两种场景:

on:
  pull_request_target:
    types: [closed]
    branches: [main]
  workflow_dispatch:

jobs:
  build:
    if: (github.event_name == 'pull_request_target' && github.event.pull_request.merged == true) || github.event_name == 'workflow_dispatch'

配置说明

  • 针对pull_request_target事件:仅当PR确实被合并(merged == true)时才执行,过滤掉「关闭未合并PR」的无效场景。
  • 针对workflow_dispatch事件:直接通过github.event_name判断,确保手动触发时能正常运行。

关于on字段直接限制合并事件的问题

正如你发现的,pull_request_target事件的types选项里没有merged这个值,所以无法直接在on字段中仅监听合并事件,必须通过if条件来过滤无效的closed事件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 01:17:12