如何在 Azure Pipelines 中缓存 pip 包

Dan*_*uza 4 python caching pip azure-pipelines

尽管此源提供了有关 Azure 管道中缓存的大量信息,但尚不清楚如何为 Python 项目缓存 Python pip 包。

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

据此推测未来可能会默认启用pip缓存。据我所知目前情况还不是这样。

Ted*_*ott 7

要缓存标准 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"
Run Code Online (Sandbox Code Playgroud)


Mar*_*icz 5

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

  • 我通过使用“cacheHitVar”简化了管道。现在,您只需将“conda env create ...”替换为任何其他命令即可安装依赖项(例如“pip install -rrequirements.txt”),更改要缓存的路径,它将起作用。 (2认同)