【问题标题】:Getting base branch SHA on pull request in Github Action Workflow在 Github Action Workflow 中根据拉取请求获取基本分支 SHA
【发布时间】:2020-08-07 04:15:11
【问题描述】:

在拉取请求的 GitHub 操作中,我需要在“当前 master”的上下文中运行一些代码,然后在 PR 分支的上下文中重新运行相同的代码。

我可以检查将拉取请求与它正在 PR-ed 的基础进行比较。我如何找到基础分支的 SHA(例如,如果 PR 反对 master,则为当前 master)?

jobs:
  job_on_base:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: "${{ github.base_ref }}"
      - run: |
          # Seems like I can get it here with $(git log -1 --format="%H")
          echo "My current SHA is ... ?"

  job_on_pr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: "${{ github.ref }}"
      - run: |
          echo "My current SHA is $GITHUB_SHA"
          echo "The BASE SHA is ?"

【问题讨论】:

    标签: github github-actions


    【解决方案1】:

    原来这是一个git 问题,而不是 Github 操作。 actions/checkout@v2 创建了一个浅层 --depth=1 克隆,因此要获得 PR 的父级,可以将 git cat-file -p 输出解析为 described here。可以使用

    访问第一个(基本)父级
    git cat-file -p <SHA> | awk 'NR > 1 {if(/^parent/){print $2; exit}}'
    

    事实证明,更好的方法是使用fetch-depth: 2 参数。它只允许一个作业处理拉取请求和主合并案例,也可以与HEAD^1 一起使用以到达父级。

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 2
    

    【讨论】:

      【解决方案2】:

      如果作业在 pull_request 事件上运行,则基本 sha 可用作 ${{ github.event.pull_request.base.sha }}

      【讨论】:

      • 如果有人在这里寻找分支机构的 sha,那就是:${{github.event.pull_request.head.sha}}github.com/actions/…
      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 2021-08-14
      • 2014-11-30
      • 2014-04-15
      • 2020-06-25
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多