我按照 action/cache@v2 的 docs 编写了一个包含缓存的工作流。我正在使用 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
我不知道为什么即使缓存路径正确也找不到依赖项
答案 0 :(得分:0)
过了一会儿,我发现这个 article 来缓存 env python 位置,现在缓存工作正常
- uses: actions/cache@v2
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-pip-${{ hashFiles('requirements.txt') }}
我不知道为什么缓存整个 python 路径有效,但缓存目录没有。也许将来有人可以给出明确的解释,现在这就是我解决的问题