Github Actions的setup-python未缓存pip依赖项

3
为什么Github Action的setup-python action的缓存仍然需要pip安装所有依赖项?
我拥有以下简单工作流:
name: Python Testing

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

permissions:
  contents: read

jobs:
  coverage_check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: "3.6"
          cache: 'pip' # caching pip dependencies
      - run: pip install -r requirements.txt --use-deprecated=legacy-resolver

根据日志,这是一次缓存命中

Successfully set up CPython (3.6.15)
/opt/hostedtoolcache/Python/3.6.15/x64/bin/pip cache dir
/home/runner/.cache/pip
Received 123461741 of 123461741 (100.0%), 111.2 MBs/sec
Cache Size: ~118 MB (123461741 B)
/usr/bin/tar --use-compress-program unzstd -xf /home/runner/work/_temp/a5d8f986-ce6b-424f-8de9-c082916cdb2f/cache.tzst -P -C /home/runner/work/backend/backend
Cache restored successfully
Cache restored from key: setup-python-Linux-20.04-Ubuntu-python-3.6.15-pip-d0b220da2d89ebae7a0978e97e9d1ce87061e5e2e0f5d3d242c2770b7f983de6

然而Python依赖已安装。

Collecting Flask-Cors==3.0.9
  Using cached Flask_Cors-3.0.9-py2.py3-none-any.whl (14 kB)
Collecting Flask-GraphQL==2.0.1
....

我希望依赖项已经存在并返回类似于以下内容的结果

Requirement already satisfied: Flask-Cors==3.0.9 in 
....
1个回答

2

在这一步中,我使用它来缓存依赖项。在此之前,您需要检出代码库,加载缓存的依赖项,然后安装或继续执行您的操作。

      - name: Load Cached Virtualenv
        id: cached-pip-wheels
        uses: actions/cache@v3
        with:
          path: ~/.cache
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}

请记住,这是使用 poetry.lock 文件,您需要将该文件替换为您的 requirements.txt

在故障排除后,我意识到 actions/setup-python 自带的开箱即用缓存由于某些原因无法用于我正在使用的 Python 版本。最终,我使用了 actions/cache 以及 Python 的 venv 模块的输出目录和 requirements.txt,如此解决了这个问题。 - Abs
@Abs,如果我的解决方案对您有帮助,您可以将其标记为答案以帮助其他人,干杯! - Corfucinas
我会保持开放几天,以防有人对 actions/setup-python 缓存解决方案有任何想法。如果没有,我将把这个解决方法标记为已接受的答案。 - Abs
@Abs,你发现actions/setup-python缓存不支持哪些Python版本? - web
@web 这是Python 3.6.15版本,使用了actions/setup-python@v3。 - Abs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接