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

GitHub Actions:如何检出PR评论对应的分支而非master

解决GitHub Actions在PR评论事件中检出对应分支的问题

问题原因

当通过issue_comment事件触发工作流时,github.ref指向的是仓库的默认分支(比如master/main),而非评论所在PR的源分支,所以当前配置会检出默认分支而非目标PR分支。

修改后的工作流配置

on:
  issue_comment:
    types: [created]

jobs:
  check-whether-pr-should-be-merged:
    # 仅处理PR的评论(排除普通Issue评论)
    if: github.event_name == 'issue_comment' && github.event.issue.pull_request
    name: check if the pr should be merged based on comment and its author's team
    runs-on: ubuntu-latest
    steps:
      - name: 获取PR的源分支引用
        id: get-pr-ref
        uses: actions/github-script@v6
        with:
          script: |
            const pr = await github.rest.pulls.get({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.issue.number
            });
            return pr.data.head.ref;
          result-encoding: string

      - name: 检出项目到PR对应分支
        uses: actions/checkout@v3
        with:
          ref: ${{ steps.get-pr-ref.outputs.result }}

关键说明

  • PR评论过滤:在job的if条件中加入github.event.issue.pull_request,确保工作流只响应PR的评论,避免处理普通Issue的评论。
  • 获取源分支引用:通过actions/github-script调用GitHub API,获取当前PR提交者的源分支名称。
  • 指定分支检出:将actions/checkout的ref参数设置为上一步获取到的分支引用,实现检出PR对应的分支。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 00:15:33