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

如何让GitHub Actions中pip安装git源码逻辑同时适配推送和PR事件

问题原因

Pull Request 触发的 Workflow 中,默认的 GITHUB_SHA 指向的是源分支与目标分支合并后的临时提交哈希,该提交仅存在于 GitHub 运行 Action 的临时环境中,不存在于公开仓库中,因此通过 Git 拉取安装包时无法找到对应哈希,触发not a tree报错。Push 场景下的 GITHUB_SHA 是实际推送到仓库的提交哈希,因此运行正常。

修复后的配置
name: CI test

on: [push, pull_request]

jobs:
  tests:
    name: Pip test
    runs-on: ubuntu-20.04

    steps:
      - name: Setup basic environment
        run: |
          sudo apt-get update
          sudo apt-get install --no-install-recommends python3-dev python3-pip

      - name: Install my package via pip
        run: |
          pip3 install git+${{ github.event.pull_request.head.repo.html_url || github.server_url }}/${{ github.repository }}@${{ github.event.pull_request.head.sha || github.sha }}

      - name: Run tests
        run: |
          python3 -m pytest -s --pyargs mypkg
改动说明
  • 用 GitHub Actions 内置表达式自动适配两种事件场景:
    • Pull Request 场景下,自动取PR头提交的真实哈希 ${{ github.event.pull_request.head.sha }},同时如果PR来自Fork仓库,会自动取Fork仓库的地址拉取代码
    • Push 场景下,自动回退到原有默认的仓库地址和提交哈希
  • 不需要额外新增步骤,也不需要硬编码仓库地址,同时兼容跨仓库Fork提交的PR场景

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 18:48:02