关于 GitHub Action 中历史记录丢失的问题

简介: 前不久在 RVPress 中添加了自动获取文章创建和修改时间的功能,其中使用 git log 来获取文章文件的 history,而后发现该功能在 GitHub Action 中无法正常工作。这里记录一下 📝。

测试

先尝试找下问题,创建一个测试用的 action,查看是否可以正常获取 history:

name: test-history
on:
    push:
        branches: ['master']
    pull_request:
        branches: ['master']
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v3
            - name: Run log
              run: git log
复制代码

然后查看是否可以正常获取 history:

Run git log
commit c8122bbd1b30de53610bd95bf31fe2f0f4439172
Author: ZxBing0066 <ZxBing0066@gmail.com>
Date:   Sun Jul 24 15:27:51 2022 +0800
    Update test-history.yml
复制代码

执行日志可以看这里:git log

然而发现只有本次的 commit 信息,没有之前的 commit 信息,确定是 GitHub Action 导致的问题。

解决方案

在 GitHub Action 中使用 checkout 检出代码时,添加 fetch-depth: 0 参数即可。

name: test-history
on:
    push:
        branches: ['master']
    pull_request:
        branches: ['master']
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v3
              with:
                  fetch-depth: 0
            - name: Run log
              run: git log
复制代码

修改后的执行日志可以看这里:git log

原因

一般我们在 action 中都会先使用 checkout 来检出代码,然后进行操作,checkout action 在检出代码时默认只检出当前事件触发的 commit,而不是所有的 commit,从而导致 history 的丢失。参考 checkout 中的 fetch-depth 说明,如果没有指定 fetch-depth,默认为 1,即只检出最近的一次 commit(参考 git clone --depth 参数)。

# Number of commits to fetch. 0 indicates all history for all branches and tags.
# Default: 1
fetch-depth: ''
复制代码

其实该参数就是 git clone 中的 depth 参数,可以看下 git 中关于该参数的说明:

Create a shallow clone with a history truncated to the specified number of commits. Implies --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches. If you want to clone submodules shallowly, also pass --shallow-submodules.

简单理解说就是浅 clone 指定数量的 commit,主要是为了用来减轻服务器压力的,因为一般在 CI/CD 中无需关注历史记录,只需要关注最新的状态即可,所以默认都是只浅 clone 最近的一次 commit。有兴趣的同学可以尝试下使用 --depth 参数来 clone 一些比较流行的开源库,如 React、babel 等,可以明显感觉到差距。

相关问题

除了上面说的历史记录丢失的问题外,在 pull request 中还存在另一个问题:log 中最近的一条记录为一条不存在的 commit,类似 xxx Merge xxx into xxx,原因是 pull request 中会创建一个新的 merge commit,如果需要 head branch 的 log,可以添加 ref 参数:

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


相关文章
|
2月前
|
Linux C++ Docker
【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image
【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image
|
2月前
|
数据安全/隐私保护
【Azure Developer】Github Action使用Azure/login@v1插件登录遇见错误的替代方案
【Azure Developer】Github Action使用Azure/login@v1插件登录遇见错误的替代方案
|
2月前
|
存储
【Azure Developer】Github Action部署资源(ARM模板)到Azure中国区时,遇见登录问题的解决办法
【Azure Developer】Github Action部署资源(ARM模板)到Azure中国区时,遇见登录问题的解决办法
|
4月前
|
开发工具 git
GitHub显示无法在此仓库中合并不相关的历史记录
GitHub显示无法在此仓库中合并不相关的历史记录
27 2
|
4月前
|
数据安全/隐私保护 开发者 Docker
国内docker公开镜像站的关闭!别急,docker_image_pusher 使用Github Action将国外的Docker镜像转存到阿里云私有仓库
通过使用 docker_image_pusher 这样的开源项目,我们能够轻松地解决国内访问 Docker 镜像拉取速度慢及拉去失败的问题,同时保证了镜像的稳定性和安全性。利用 Github Action 的自动化功能,使得这一过程更加简单和高效。
1455 2
|
4月前
【完美解决】Github action报错remote: Write access to repository not granted.
【完美解决】Github action报错remote: Write access to repository not granted.
593 1
|
4月前
|
jenkins 物联网 测试技术
干货分享!基于 Github Action 的 taosX CI 搭建
去年随着 3.1.1.0 版本的发布,TDengine 数据接入工具 taosX 正式推出。该工具具备强大的数据抓取、清洗、转换及加载(ETL)功能。它不仅能无缝对接物联网中的 MQTT 协议,更重要的是能够连接到工业数据源如 OPC-UA、OPC-DA、PI System 等。借助这一模块,工业场景中常用的 SCADA、DCS 等系统无需编写任何代码,仅需通过简单配置即可实现数据的实时、持续导入至 TDengine。
51 1
|
5月前
|
存储 JavaScript 测试技术
github action
github action
60 0
|
5月前
|
供应链 安全 jenkins
|
缓存 前端开发 持续交付
白嫖github的Action做定时任务
白嫖github的Action做定时任务
白嫖github的Action做定时任务
下一篇
无影云桌面