Github Actions缓存Python依赖失效:缓存命中仍重复安装
问题描述
我在个人仓库中配置了Github Actions以运行Python项目,项目依赖第三方库,直接安装依赖时执行效率正常。但尝试缓存依赖后,每次运行虽显示缓存命中,却仍会重复安装依赖,请问是否遗漏了关键步骤?
提供的Workflow配置:
name: Daily Python Script on: schedule: - cron: "0 19 * * *" workflow_dispatch: #Manual trigger jobs: build: runs-on: ubuntu-latest permissions: # Give the default GITHUB_TOKEN write permission to commit and push the # added or changed files to the repository. contents: write steps: - name: Set up Python uses: actions/setup-python@v4 with: python-version: 3.9 - name: Update Node.js to v16 uses: actions/setup-node@v3 with: node-version: 16 - name: Checkout Code uses: actions/checkout@v4 - name: Restore Cached Dependencies uses: actions/cache@v3 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} - name: Debug Cache run: | echo "Cache hit: ${{ steps.cache-dependencies.outputs.cache-hit }}" ls -l ~/.cache/pip - name: Install Dependencies if: steps.cache-dependencies.outputs.cache-hit != 'true' run: | python -m pip install --upgrade pip pip install -r requirements.txt - name: Run Python Script run: python main_v1.py - name: Commit and Push Logs uses: stefanzweifel/git-auto-commit-action@v4 with: commit_message: Automated Change repository: . commit_user_name: My GitHub Actions Bot # defaults to "github-actions[bot]" commit_user_email: my-github-actions-bot@example.org # defaults to "41898282+github-actions[bot]@users.noreply.github.com" commit_author: Author <actions@github.com> # defaults to author of the commit that triggered the run
requirements.txt内容:
numpy==1.21.6 oauth2client==4.1.3 pandas==1.5.3 pytz==2022.7.1 Requests==2.31.0 selenium==4.9.0 trycourier==4.4.0
问题原因
核心问题是缓存步骤未设置id,导致缓存命中状态的判断失效:
- 你的
Install Dependencies步骤使用steps.cache-dependencies.outputs.cache-hit判断是否跳过安装,但缓存步骤仅设置了名称Restore Cached Dependencies,并未指定id: cache-dependencies,因此steps.cache-dependencies这个引用不存在,cache-hit的值为空,条件!= 'true'永远成立,所以每次都会执行依赖安装。
另外,缓存~/.cache/pip只是缓存pip下载的包文件,并非已经安装到Python环境的依赖。即使缓存命中,若Python的site-packages目录中没有对应依赖,仍然需要执行安装操作(只是无需重新下载,速度会更快)。
解决方案
1. 给缓存步骤添加正确的id
修改缓存步骤,添加id: cache-dependencies,让后续步骤能正确获取缓存命中状态:
- name: Restore Cached Dependencies id: cache-dependencies # 添加这一行 uses: actions/cache@v3 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
2. 优化依赖安装逻辑(可选)
如果你想完全跳过安装步骤,需要缓存已经安装好的依赖目录(比如虚拟环境),但更简单的方式是保留pip install命令,利用缓存的包文件加速安装——即使执行安装,因为包已经缓存,速度会和跳过安装差不多。可以去掉if条件,让每次都执行安装,pip会自动从~/.cache/pip读取缓存的包,无需从网络下载:
- name: Install Dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt
修正后的完整Workflow
name: Daily Python Script on: schedule: - cron: "0 19 * * *" workflow_dispatch: #Manual trigger jobs: build: runs-on: ubuntu-latest permissions: contents: write steps: - name: Set up Python uses: actions/setup-python@v4 with: python-version: 3.9 - name: Update Node.js to v16 uses: actions/setup-node@v3 with: node-version: 16 - name: Checkout Code uses: actions/checkout@v4 - name: Restore Cached Dependencies id: cache-dependencies uses: actions/cache@v3 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} - name: Debug Cache run: | echo "Cache hit: ${{ steps.cache-dependencies.outputs.cache-hit }}" ls -l ~/.cache/pip - name: Install Dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - name: Run Python Script run: python main_v1.py - name: Commit and Push Logs uses: stefanzweifel/git-auto-commit-action@v4 with: commit_message: Automated Change repository: . commit_user_name: My GitHub Actions Bot commit_user_email: my-github-actions-bot@example.org commit_author: Author <actions@github.com>
内容的提问来源于stack exchange,提问作者tatojunior
相关产品推荐
相关产品推荐

