首页 > 解决方案 > 验证来自输入的提交哈希 (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 }}

标签: gitgithub-actionsgit-commit

解决方案


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

这是因为对于触发工作流的 ref/SHA, checkout 操作默认只检索一个提交。我们可以设置fetch-depth: 0为获取所有分支和标签的所有历史记录:

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

这样,我的问题上的代码就可以工作了。


推荐阅读