组织级 Required workflow 未被 pull_request edited 触发
从现象看,标题校验逻辑本身没问题,问题出在组织级 required workflow 的调度方式:pull_request 的 edited activity type 没有重新调度组织级 required workflow,这与仓库内普通 workflow 的行为不一致。GitHub 上相关 issue 已关闭,不代表该行为已经修复;仍要以实际行为和官方文档为准。
可行的变通做法是把“触发器”和“校验逻辑”分开:校验逻辑仍放在组织 .github 仓库的 reusable workflow 中集中管理;每个仓库只放一个很小的触发 workflow,监听 pull_request 的 edited(建议同时保留 opened、synchronize、reopened),用它调用组织级 reusable workflow。然后把调用产生的 check 配到 ruleset 的 required status checks 中,不要只依赖原来那个不响应 edited 的组织级 required workflow。如果不允许每个仓库放文件,可以让 GitHub App 订阅 pull_request.edited webhook,并创建 check run。
方案一:组织级 reusable workflow + 仓库最小触发器
- 在组织
.github仓库新增 reusable workflow,例如.github/workflows/pr-title-check.yml:
name: PR Title Check Reusable
on:
workflow_call:
inputs:
pr_title:
description: Pull request title
required: true
type: string
permissions:
contents: read
pull-requests: read
jobs:
check-title:
runs-on: self-hosted
steps:
- name: Validate PR title
env:
PR_TITLE: ${{ inputs.pr_title }}
run: |
echo "PR title: $PR_TITLE"
if [ -z "$PR_TITLE" ]; then
echo "PR title must not be empty"
exit 1
fi
# 在这里放入组织统一的标题规则
- 在需要强制校验的仓库中新增触发器 workflow,例如
.github/workflows/pr-title-check-trigger.yml:
name: PR Title Check
on:
pull_request:
types:
- opened
- edited
- synchronize
- reopened
permissions:
contents: read
pull-requests: read
jobs:
call-pr-title-check:
uses: my-org/.github/.github/workflows/pr-title-check.yml@main
with:
pr_title: ${{ github.event.pull_request.title }}
把 my-org 换成你的组织名,@main 建议固定到组织 .github 仓库的稳定分支或 commit。如果 reusable workflow 在私有仓库,需要在组织设置中允许仓库访问。
- 在 ruleset 中把该调用产生的 check 设为 required。实际名称以 Actions 页面显示为准,常见形式是调用 job 与被调用 job 的组合,例如
call-pr-title-check / check-title。不要只把原来那个组织级 required workflow 的 check 设为唯一 required;否则edited后它不会重新运行,标题修改也就无法重新强制校验。
方案二:GitHub App 创建 check run
如果完全不想在每个仓库放 workflow,可以维护一个组织级 GitHub App,订阅 pull_request 的 edited、opened、reopened、synchronize 等 action。收到 webhook 后读取 payload.pull_request.title,按组织规则校验,再通过 Checks API 创建或更新 check run,返回 success 或 failure。然后在 ruleset 中 require 这个 check 名称。这个方案集中度最高,但需要维护 App、webhook 接收端和凭证。
注意事项
- 先检查实际 YAML 的缩进。如果按原样保存你贴出的示例,
pull_request、types、jobs等层级可能不合法;on:下的pull_request:应缩进,types:再缩进。能响应opened说明实际文件可能只是粘贴时丢了缩进,但仍建议核对。 pull_request事件对 fork PR 默认没有写权限和 secrets,适合只读标题校验。不要为了拿写权限改用pull_request_target后 checkout 不可信 PR 代码。edited也会在描述修改时触发,标题校验会跟着重跑;如果只想在标题变化时重跑,可以基于 event payload 的changes判断,但上线前要验证实际 payload。- 如果使用 self-hosted runner,注意不要在该 runner 上执行 fork PR 的不可信代码;标题校验只读 event payload 时风险较低。
- required workflow 和 required status checks 是两套机制。要解决
edited后重新强制校验的问题,关键是让一个能被edited触发的 workflow 产生 required check。
备注:内容仅供参考。
