【问题标题】:How to cache pip packages within Azure Pipelines如何在 Azure Pipelines 中缓存 pip 包
【发布时间】:2020-10-06 18:45:52
【问题描述】:

虽然this source 提供了大量关于 Azure 管道中缓存的信息,但目前尚不清楚如何为 Python 项目缓存 Python pip 包。

如果愿意在 Azure 管道构建中缓存 Pip 包,如何进行?

根据this的说法,可能是以后默认开启pip缓存。据我所知,目前还没有。

【问题讨论】:

  • 您想在 Azure 管道上使用什么样的 pip 包?您是否尝试使用私人包。它能够使用 Azure Artifacts 直接托管私有 pip 包
  • 我希望有与docs.travis-ci.com/user/caching/#pip-cache类似的行为。
  • @PatrickLu-MSFT,我的印象是默认情况下已经缓存了 pip 包。你能证实吗?例如 pip 包被安装到 /home/vsts/.cache/pip 文件夹。

标签: python caching pip azure-pipelines


【解决方案1】:

我使用pre-commit 文档作为灵感:

并使用 Anaconda 配置以下 Python 管道:

pool:
  vmImage: 'ubuntu-latest'

variables:
  CONDA_ENV: foobar-env
  CONDA_HOME: /usr/share/miniconda/envs/$(CONDA_ENV)/

steps:
- script: echo "##vso[task.prependpath]$CONDA/bin"
  displayName: Add conda to PATH

- task: Cache@2
  displayName: Use cached Anaconda environment
  inputs:
    key: conda | environment.yml
    path: $(CONDA_HOME)
    cacheHitVar: CONDA_CACHE_RESTORED

- script: conda env create --file environment.yml
  displayName: Create Anaconda environment (if not restored from cache)
  condition: eq(variables.CONDA_CACHE_RESTORED, 'false')

- script: |
    source activate $(CONDA_ENV)
    pytest
  displayName: Run unit tests

【讨论】:

  • 感谢您的回答@marek-grzenkowicz。您还可以提供非 Conda 解决方案吗?也许使用 pip-tools pip-sync 命令是一种模拟 conda 环境同步的方法。
  • 我使用cacheHitVar 简化了管道。现在您可以简单地将conda env create ... 替换为任何其他命令来安装依赖项(例如pip install -r requirements.txt),更改要缓存的路径即可。
  • 我们现在需要了解 pip 包是否已经默认缓存在 Azure 管道中。
  • 即使包文件缓存在 Azure 基础结构中,安装它们也可能需要很长时间。在我的例子中,缓存一个完整的 Anaconda 环境将管道执行时间从 ~5 分钟缩短到 ~1:30。
【解决方案2】:

要缓存标准 pip 安装,请使用:

variables:
  # variables are automatically exported as environment variables
  # so this will override pip's default cache dir
  - name: pip_cache_dir
    value: $(Pipeline.Workspace)/.pip

steps:
  - task: Cache@2
    inputs:
      key: 'pip | "$(Agent.OS)" | requirements.txt'
      restoreKeys: |
        pip | "$(Agent.OS)"
      path: $(pip_cache_dir)
    displayName: Cache pip

  - script: |
      pip install -r requirements.txt
     displayName: "pip install"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 2021-05-25
    • 2022-10-05
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多