【问题标题】:How to cache poetry install for GitHub Actions如何为 GitHub Actions 缓存诗歌安装
【发布时间】:2020-11-08 16:01:35
【问题描述】:

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

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

诗歌运行点子列表

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 来缓存诗歌安装/virturalenv 以避免重新安装依赖项。

【问题讨论】:

    标签: continuous-integration github-actions python-poetry


    【解决方案1】:

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

    对于使用版本 >= 1.1.0 的 Poetry 安装,我建议使用此 sn-p 来缓存您的 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 :)

    【讨论】:

    • 感谢您的工作。我已经改变接受你的回答,请继续保持你的GH行为:)
    【解决方案2】:

    在您的 YAML 文件中,您使用 dschep/install-poetry-action@v1.3 安装 Poetry,它设置了 poetry config virtualenvs.create false,这意味着使用当前的 python 解释器/virtualenv。因为您没有在任何诗歌只是使用系统 python 并且~/.poetry 目录中没有 virtualenv 的地方激活 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)。无论如何,我个人更喜欢在与其他所有内容相同的配置块中执行此操作。

    【讨论】:

    • 请注意。这些答案中提供的解决方案是正确的。但是该 Action 不再保留。查看@Sondre 的答案以获得最新的解决方案。
    【解决方案3】:

    您无需使用外部操作来安装 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
    
    

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

    然而,当你读到这篇文章时,this PR might have already landed 所以你需要缓存 Poetry deps 就是 actions/setup-python

    【讨论】:

    • Poetry 安装到 $HOME/.local/bin,因此您可以缓存该路径并在后续运行中使用它。
    猜你喜欢
    • 2023-01-14
    • 2021-04-24
    • 2022-08-11
    • 2021-07-12
    • 1970-01-01
    • 2023-01-27
    • 2020-07-15
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多