You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取上次PUSH至本次PUSH间所有新提交的文件?

解决方案:获取两次Push之间的所有变更文件

核心思路

借助GitHub Actions推送事件提供的before和after提交哈希,对比这两个哈希区间内的所有变更文件,替代仅对比最后两次commit的逻辑,覆盖单次Push包含多个commit的场景。

具体实现步骤

1. 调整代码拉取策略

修改actions/checkout配置,拉取全量仓库历史,确保能定位到本次Push前的基准提交:

- name: checkout repo content
  uses: actions/checkout@v4
  with:
    fetch-depth: 0  # 拉取完整仓库历史,避免因历史缺失无法找到目标提交

2. 获取并遍历两次Push间的所有变更文件

利用GitHub事件上下文的github.event.before和github.event.after生成文件列表,循环执行目标脚本:

- name: Identify and execute different Configuration files
  run: |
    echo "Check-in configuration files are:"
    # 处理首次Push的特殊情况(before为全0哈希)
    if [ "${{ github.event.before }}" = "0000000000000000000000000000000000000000" ]; then
      files=$(git diff --name-only ${{ github.event.after }} --)
    else
      files=$(git diff --name-only ${{ github.event.before }}..${{ github.event.after }})
    fi

    # 循环遍历每个文件并执行脚本
    for file in $files; do
      # 替换为你的实际脚本执行逻辑,例如:
      # ./your-processing-script.sh "$file"
      echo "Executing script for file: $file"
    done

关键说明

  • github.event.before/github.event.after:这两个是Push事件自带的变量,分别对应本次推送前分支的最后一个提交哈希、推送后的最新提交哈希。
  • 全量历史拉取:fetch-depth: 0确保本地仓库包含所有提交记录,避免因历史截断导致无法找到before对应的基准提交。
  • 首次Push兼容:仓库首次推送时before为全0哈希,此时通过git diff --name-only <after-sha> --获取所有新增文件。

内容的提问来源于stack exchange,提问作者Karki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 20:40:05