@northtree回答是正确的,但对于任何浏览者,您应该知道,所引用的操作不再得到维护。
对于使用版本>=1.1.0的Poetry安装,我建议使用此片段来缓存您的Poetry依赖项:
...
- name: Install poetry
uses: snok/install-poetry@v1.0.0
with:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: poetry install
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
...
更完整的示例文档可以在这里找到 :)
缓存被集成到 actions/setup-python
中(https://github.com/actions/setup-python#caching-packages-dependencies):
steps:
- uses: actions/checkout@v3
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v3
with:
python-version: '3.9'
cache: 'poetry'
- run: poetry install
- run: poetry run pytest
dschep/install-poetry-action@v1.3
来安装Poetry,这会设置poetry config virtualenvs.create false
,这意味着当前的python解释器/虚拟环境将被使用。由于您没有在任何地方激活虚拟环境,因此Poetry只是使用系统Python,并且~/.poetry
目录中没有虚拟环境。poetry config virtualenvs.create true
,例如:设置为true,它应该可以正常工作。 - name: Install poetry
uses: dschep/install-poetry-action@v1.3
- name: Configure poetry
run: |
poetry config virtualenvs.create true
poetry config virtualenvs.in-project false
poetry config cache-dir ~/.poetry
poetry config virtualenvs.path ~/.poetry/venv
注意:根据dschep/install-poetry-action
文档,安装期间有一个选项可以设置poetry config virtualenvs.create true
,但目前似乎存在问题(请参见https://github.com/dschep/install-poetry-action/issues/11)。无论如何,我个人更喜欢在与其他所有内容相同的配置块中完成此操作。
poetry
会将模块安装到~/.cache/pypoetry
,因此: - name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v2
with:
path: |
~/.cache/pypoetry
.venv
key: poetry-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Install poetry no matter what
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "$HOME/.local/bin" >> $GITHUB_PATH
- name: Install requirements
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install
理论上来说,缓存poetry
的安装也是可能的,它被安装到~/.poetry
中。但我找不到方法。
然而,在您阅读此内容时,这个PR可能已经被合并,所以您只需要使用actions/setup-python
即可缓存Poetry依赖项。
$HOME/.local/bin
,因此您可以缓存该路径并在后续运行中使用它。 - jidicula