Search code examples
pythoncachingpipazure-pipelines

How to cache pip packages within Azure Pipelines


Although this source provides a lot of information on caching within Azure pipelines, it is not clear how to cache Python pip packages for a Python project.

How to proceed if one is willing to cache Pip packages on an Azure pipelines build?

According to this, it may be so that pip cache will be enabled by default in the future. As far as I know it is not yet the case.


Solution

  • I used the pre-commit documentation as inspiration:

    and configured the following Python pipeline with Anaconda:

    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