sec*_*sec 10 continuous-integration github-actions
我在 Github Actions 中使用缓存的 pip 时遇到了一些问题。我的工作流程设置是
name: tests
on: [push, pull_request]
jobs:
linux:
runs-on: ubuntu-18.04
strategy:
max-parallel: 4
matrix:
python-version: [3.6, 3.7, 3.8]
steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v1
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install
if: steps.cache.outputs.cache-hit != 'true'
run: |
pip install pytest pytest-cov bandit sphinx recommonmark
python -m pip install --upgrade pip
pip install .
- name: Test with pytest
run: |
pytest --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
coverage report -m
- name: Test with bandit
run: |
bandit --recursive chepy/ --ignore-nosec --skip B101,B413,B303,B310,B112,B304,B320,B410,B404
- name: Test docs
run: |
make -C docs/ clean html
- name: Upload to codecov
uses: codecov/codecov-action@v1.0.3
with:
token: ${{secrets.CODECOV_TOKEN}}
file: ./coverage.xml
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,在触发的后续操作中,它不会使用缓存,而是从 pip 重新安装。
我怎样才能让它使用缓存?
kol*_*pto 10
您还可以缓存 virtualenv,如下所示:
- uses: actions/cache@v2
id: cache-venv # name for referring later
with:
path: ./.venv/ # what we cache: the virtualenv
# The cache key depends on requirements.txt
key: ${{ runner.os }}-venv-${{ hashFiles('**/requirements*.txt') }}
restore-keys: |
${{ runner.os }}-venv-
# Build a virtualenv, but only if it doesn't already exist
- run: python -m venv ./.venv && . ./.venv/bin/activate &&
pip install -r requirements.txt
if: steps.cache-venv.outputs.cache-hit != 'true'
# Run tests
# Note that you have to activate the virtualenv in every step
# because GitHub actions doesn't preserve the environment
- run: . ./.venv/bin/activate && nosetests tests/
Run Code Online (Sandbox Code Playgroud)
您忘记了id在步骤中添加使用actions/cache. 这是Install逐步工作的条件所必需的。
- uses: actions/cache@v1
id: cache
with:
...
Run Code Online (Sandbox Code Playgroud)
我在为Rikai项目实现缓存时遇到了类似的问题。
最后,我找到了如果我只是按照github actions cache文档中的示例进行操作,它不会按我预期工作的原因
每次pip install执行实际上都需要两个步骤。
这些年互联网速度提高了很多。因此,今天,第二步可能比第一步花费更多时间,特别是当第二步包含编译项目等复杂行为时C/C++。但~/.cache/pip只缓存下载的包,不缓存安装的包。下载缓存的包(从Github)也需要时间,也许与从 下载没有什么不同PyPI。所以你对缓存没有明显的感觉。
现在有我的解决方案。这是缓存已安装的包,而不是下载的包。要知道已安装的软件包在哪里,pip install请再次运行,您将看到类似的内容
Requirement already satisfied: scipy>=1.1.0 in /.../site-packages (from scikit-learn->rikai==0.0.19.dev0) (1.7.3)
Run Code Online (Sandbox Code Playgroud)
这/.../site-packages是您的软件包最终安装的地方。该目录因环境而异,因此您必须自己尝试才能知道。
尝试新目录后,我发现它确实有效,每个安装命令都会显示一个日志
Requirement already satisfied: xxx>=xx in xxx/site-packages (from xxx)
Run Code Online (Sandbox Code Playgroud)
默认site-packages目录还包含Python解释器的标准库,实际上你不需要缓存它,因为它包含在actions/setup-python. 我能找到的最好方法是通过 并将包安装到用户命名空间中,pip install --user并缓存用户的site-packages目录。如果您不确定它在哪里,可以使用环境变量PYTHONUSERBASE。
然后您可以以非常有效的方式缓存必要的包。
这个文件中有一个完整的可行示例
| 归档时间: |
|
| 查看次数: |
3584 次 |
| 最近记录: |