如何在Github Actions中基于author_association限制自定义指令权限?
自定义指令的协作者权限控制配置方案
实现逻辑
GitHub触发issue_comment事件时,会在事件负载中自带评论发布者的author_association元数据,访问路径为github.event.comment.author_association,无需额外调用接口拉取,可直接在工作流的执行条件中做权限判断,完全不依赖PullApprove即可实现目标效果。
关键提醒:协作者对应的字段固定值为全大写的
COLLABORATOR,大小写不匹配会导致判断规则完全失效。
具体配置修改
在原有revert-commit任务的if判断中,追加权限校验逻辑,将「评论包含/revert指令」和「触发者为协作者」两个条件通过与逻辑绑定,只有同时满足才会启动后续流程,修改后的完整配置如下:
name: Command Management on: issue_comment: types: [created, edited] jobs: # Automatically reverts commits on request revert-commit: runs-on: ubuntu-latest # 原有指令识别逻辑 + 新增协作者权限校验 if: > contains(github.event.comment.body, '/revert') && github.event.comment.author_association == 'COLLABORATOR' steps: - name: Checkout uses: actions/checkout@v2.3.1 - name: Automatic Revert uses: srt32/revert@v0.0.1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
灵活调整说明
- 如果需要把仓库所有者、组织核心成员也纳入可触发范围,可在判断条件中追加对应值的或逻辑,示例规则:
github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'OWNER' || github.event.comment.author_association == 'MEMBER' - 权限判断放置在job级别执行,不满足条件的请求不会启动后续运行流程,不会产生多余的运行资源消耗
内容的提问来源于stack exchange,提问作者Jonathan Stevens
相关产品推荐
相关产品推荐

