如何为 GitHub 操作缓存诗歌安装

How to cache poetry install for GitHub Actions

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

在 运行

时找不到缓存安装的库

poetry run pip list

Package    Version
---------- -------
pip        20.1.1
setuptools 41.2.0 

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

YAML 是 here

请问如何使用actions/cache@v2缓存poetry安装/virtualenv以避免重装依赖。

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

设置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

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

@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'
...

记录了更完整的示例here :)

您不需要使用外部操作来安装 Poetry 或设置虚拟环境。 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

理论上应该也可以缓存安装到 ~/.poetrypoetry 的安装。不过我找不到路了。

但是,当您阅读本文时,this PR might have already landed 所以您需要缓存 Poetry deps 的只是 actions/setup-python

缓存本机集成到 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