Github动作中的Git历史 [英] Git history in a Github Action

查看:78
本文介绍了Github动作中的Git历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Github Action中记录git历史。但是似乎该Action的环境不同:

I would like to log the git history in a Github Action. But it seems that the Action's environment has a different one:

echo $(git log -5 --oneline)



shell:/ bin / bash -e {0}

shell: /bin/bash -e {0}

7c0faa6合并c245982a87ef5538d42ab905706faa08f4d67ce9变成8a939ef1f71eaecac0ae52d625dad3e3c9fa4c16

7c0faa6 Merge c245982a87ef5538d42ab905706faa08f4d67ce9 into 8a939ef1f71eaecac0ae52d625dad3e3c9fa4c16

这不是git日志,而且这些哈希都不符合我的存储库中的值。

This is not a git log and none of these hashes matches what I have in my repo.

为什么?

如何从Github Action环境访问提交历史记录?

How to access the commit history from the environment of a Github Action?

推荐答案

您正在遇到此问题,因为您的工作流正在运行 pull_request 事件。在这些事件期间, GITHUB_REF 是从head分支到基本分支的合并提交。这样做的目的是针对合并提交运行CI,以在它实际合并之前检查它是否通过。

You are experiencing this because your workflow is running on pull_request events. During those events, GITHUB_REF is a merge commit from the head branch to the base. The intention of this is to run CI against the merge commit to check it passes before it's actually merged.

如果您不希望合并提交,而是想检出head提交,则必须将HEAD的sha作为 ref传递给到结帐。您可以按以下方式更改结帐方式(取自此示例)。

If you don't want the merge commit and want to checkout the head commit instead, you have to pass the HEAD's sha as ref to the checkout. You can change the checkout as follows (taken from this example).

此外,默认情况下该结帐是浅表的,这意味着它只有最后一次提交。要读取比上一次提交更多的内容,请将非零数字传递给 fetch-depth 或零,这将获取整个历史记录(默认为 1 ):

Also, that checkout is shallow by default, meaning it only has the last commit. To read more than the last commit, pass a non zero number to fetch-depth, or zero as well, which will fetch the entire history (default is 1):

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

请参见此处是 pull_request 事件的文档。

这篇关于Github动作中的Git历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆