【问题标题】:GitHub Workflows - Pull request, get branch nameGitHub Workflows - 拉取请求,获取分支名称
【发布时间】:2023-02-24 22:41:20
【问题描述】:

我有一个 GitHub 工作流,当在 Pull Request 中发表评论时触发

if: ${{ github.event.comment.body == '/some_comment'}} && ${{ github.event.issue.pull_request }}

执行工作流时,checkout 操作会克隆 master 分支,而不是 PR 中的实际分支。

    - uses: actions/checkout@v3

我尝试了各种输出但没有运气 :( 对于下面的示例,所有回声都是空的。

    - run: "echo ${{github.event.pull_request.head.sha}}"
    - run: "echo ${{github.event.workflow_run.head.sha}}"
    - run: "echo ${{github.event.workflow_run.base.sha}}"
    - run: "echo ${{github.event.pull_request.base.sha}}"

你们有谁知道如何在 Github Action 中查看 PR 中的源代码分支吗?

谢谢

【问题讨论】:

    标签: git github github-actions branch


    【解决方案1】:

    要在 GitHub 操作中检出拉取请求中的源分支,您可以使用 github.event.pull_request.head.ref 上下文。此上下文包含拉取请求来自的分支的名称。

    下面是一个示例,说明如何修改操作/签出步骤以签出拉取请求中的源分支:

    - uses: actions/checkout@v2
      with:
        ref: ${{ github.event.pull_request.head.ref }}
    

    这将检出拉取请求的源分支而不是主分支。请注意,actions/checkout 的 v3 版本也可能有效,但我在此示例中使用了 v2,因为它在某些情况下更稳定。

    另外,请确保该操作是由 pull_request 事件而不是 issue_comment 事件触发的,否则 github.event.pull_request.head.ref 将不可用。

    【讨论】:

    • v2 使用已弃用的 nodeJs 版本,我建议尽快升级
    • 也试过这个,但它仍然检查主分支。当我回应${{ github.event.pull_request.head.ref }}时,它仍然是空的。示例:codefile.io/f/QDVCUTrb8CSkwEGSX2sD
    猜你喜欢
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2014-11-30
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多