gpt4 book ai didi

pip - 找到缓存后,github 操作 pip 依赖项无法正常工作

转载 作者:行者123 更新时间:2023-12-02 02:06:33 25 4
gpt4 key购买 nike

我写了一个包含缓存的工作流程,遵循 docs Action /缓存@v2。我正在使用 docker-compose 托管我的 postgres 数据库并使用 django 框架(不在 docker 容器中)进行测试。

这是我的步骤:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.8'

- name: Cache Dependencies
uses: actions/cache@v2
id: cache
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: |
echo "Installing dependencies and caching them."
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Start containers
run: |
pip cache dir
cp .env.example .env
docker-compose -f "docker-compose.yml" up -d --build
- name: Run Tests
run: |
sleep 20
coverage run manage.py test -v 2 && coverage report
flake8
- name: Stop containers
if: always()
run: docker-compose -f "docker-compose.yml" down

我的 requirements.txt 已经包含了正确的包,这就是为什么第一次运行构建而不缓存它成功而没有任何问题。问题是第一个之后的任何构建,在找到缓存之后:

Run actions/cache@v2   with:
path: ~/.cache/pip
key: Linux-pip-0066acd4cae425b2654d95aae80b2bb4fda1c99d40c709cc1fa8a1595759a2c5
restore-keys: Linux-pip-
env:
pythonLocation: /opt/hostedtoolcache/Python/3.8.10/x64
LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.8.10/x64/lib Received 0 of 63168059 (0.0%), 0.0 MBs/sec Received 63168059 of 63168059 (100.0%), 33.1 MBs/sec Cache Size: ~60 MB (63168059 B) /usr/bin/tar --use-compress-program zstd -d -xf /home/runner/work/_temp/3f7aab5a-e81f-4039-889e-c0879488e8e8/cache.tzst
-P -C /home/runner/work/base.django/base.django Cache restored successfully Cache restored from key: Linux-pip-0066acd4cae425b2654d95aae80b2bb4fda1c99d40c709cc1fa8a1595759a2c5

由于未找到来自 requirements.txt 的包,测试返回错误:

Run sleep 20
sleep 20
coverage run manage.py test -v 2 && coverage report
flake8
shell: /usr/bin/bash -e {0}
env:
pythonLocation: /opt/hostedtoolcache/Python/3.8.10/x64
LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.8.10/x64/lib
/home/runner/work/_temp/62e260b4-7d93-4a7c-a3e1-e930c4ea36b1.sh: line 2: coverage: command not found
/home/runner/work/_temp/62e260b4-7d93-4a7c-a3e1-e930c4ea36b1.sh: line 3: flake8: command not found
Error: Process completed with exit code 127.

即使我指定了与缓存文档相同的缓存路径并且它对于 pip 文档也是正确的 https://pip.pypa.io/en/stable/cli/pip_install/#caching

这里 pip cache dir 打印出来:

/home/runner/.cache/pip

我不知道为什么缓存路径是正确的却找不到依赖项

最佳答案

总结:

~/.cache/pip 不包含已安装的依赖项,site-packages/ 包含。要么删除 if: steps.cache.outputs.cache-hit != 'true' 要么缓存 ${{ env.pythonLocation }} (如 accepted answer )到使用缓存。

说明:

~/.cache/pip 实际上是正确的,如果你只想缓存下载的 Assets pip documentation指定它缓存 HTTP 响应和本地构建的轮子。这就是您将在 ~/.cache/pip 中找到的内容。 pip 不缓存的是已安装的包 等等。

以下指令出现问题:

    - name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: |
...

如果删除 if: steps.cache.outputs.cache-hit != 'true' 行,一切都应该正常。下次运行该操作时,pip 将不会尝试下载 Assets ,而是使用缓存的 Assets (但仍会从缓存的 wheels/tarballs 安装它们)。

本质上,上面的行告诉 GitHub 运行器执行以下操作:

  • 如果我们从缓存中恢复了 pip 缓存,则跳过安装依赖项
  • 如果没有pip cache的缓存,安装依赖

第一次运行操作时,runner 获取缓存未命中。因此,它会安装依赖项。然而,第二次它获得了缓存命中,并跳过了依赖项安装。

为什么这是个问题?因为在缓存命中时,我们恢复由 path: option:

指定的文件

The cache action will attempt to restore a cache based on the key you provide. When the action finds a cache, the action restores the cached files to the path you configure.

Source:
https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#using-the-cache-action

这是 ~/.cache/pip。此目录不是安装的 Python 包所在的目录。该目录是 site-packages/,它包含在 ${{ env.pythonLocation }} 中。因此,当 ${{ env.pythonLocation }} 被缓存时,我们也恢复安装的包,一切都按预期工作。

关于pip - 找到缓存后,github 操作 pip 依赖项无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68372063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com