如何为 GitHub Actions 缓存诗歌安装

nor*_*ree 6 continuous-integration github-actions python-poetry

我试图用它actions/cache@v2来缓存诗歌 venv。只有两个库pylintpytest已安装。安装似乎已被缓存(缓存大小 ~ 26MB)。但是,在缓存命中后无法检索它们。

运行时找不到缓存安装的库

诗歌运行点列表

Package    Version
---------- -------
pip        20.1.1
setuptools 41.2.0 
Run Code Online (Sandbox Code Playgroud)

https://github.com/northtree/poetry-github-actions/runs/875926237?check_suite_focus=true#step:9:1

YAML 在这里

我可以知道如何使用actions/cache@v2缓存诗歌安装 / virturalenv 以避免重新安装依赖项。

Syl*_*age 27

缓存原生集成到actions/setup-pythonhttps://github.com/actions/setup-python#caching-packages-dependency):

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
Run Code Online (Sandbox Code Playgroud)

  • 这是截至 2022 年 5 月的正确答案 (3认同)

小智 10

@northtree 的回答是正确的,但是对于任何浏览的人,您应该知道 hte 引用的操作已不再维护。

对于使用版本 >= 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'
...
Run Code Online (Sandbox Code Playgroud)

此处记录更完整的示例:)


Mat*_*ohn 6

在您用于dschep/install-poetry-action@v1.3安装 Poetry 的 YAML 文件中,它设置了poetry config virtualenvs.create false,这意味着使用当前的 python 解释器/virtualenv。因为你没有在任何诗歌只是使用系统 python 的地方激活 virtualenv 并且目录中没有 virtualenv ~/.poetry

如果你设置它应该可以工作poetry config virtualenvs.create 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
Run Code Online (Sandbox Code Playgroud)

注意:根据文档,dschep/install-poetry-action有一个在安装过程中设置的选项poetry config virtualenvs.create true,但目前似乎已损坏(请参阅https://github.com/dschep/install-poetry-action/issues/11)。无论如何,我个人更喜欢在与其他所有内容相同的配置块中执行此操作。

  • 请注意。这些答案中提供的解决方案是正确的。但该操作已不再维护。检查 @Sondre 的答案以获得最新的解决方案。 (3认同)