从输入验证提交哈希 (workflow_dispatch)

问题描述

我想验证来自输入 (workflow_dispatch) 的提交哈希:

runs-on: ubuntu-latest
steps:
  - name: Checkout Project
    uses: actions/checkout@v2
    
  - name: Run only if input exist (Validate input hash)
    if: ${{ github.event.inputs.sha != '' }}
    run: git cat-file -e ${{ github.event.inputs.sha }}^{commit}

问题是它只适用于最新的提交。

如果我使用任何其他提交,它会说:

fatal: Not a valid object name COMMIT_HASH^{commit}
Error: Process completed with exit code 128.

但它在本地工作。我也试过这种方式:

git cat-file -e ${{ github.event.inputs.sha }}
git cat-file commit ${{ github.event.inputs.sha }}

解决方法

我尝试运行 git rev-list HEAD 以检查提交历史记录,结果它只显示最新提交。

这是因为 checkout action 在默认情况下仅检索单个提交,用于触发工作流的 ref/SHA。我们可以设置 fetch-depth: 0 来获取所有分支和标签的所有历史记录:

  - name: Checkout Project
    uses: actions/checkout@v2
    with:
      fetch-depth: 0

这样,我的问题中的代码就会起作用。