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.
Advertisement
Answer
I used the pre-commit
documentation as inspiration:
- https://pre-commit.com/#azure-pipelines-example
- https://github.com/asottile/azure-pipeline-templates/blob/master/job–pre-commit.yml
and configured the following Python pipeline with Anaconda:
JavaScript
x
27
27
1
pool:
2
vmImage: 'ubuntu-latest'
3
4
variables:
5
CONDA_ENV: foobar-env
6
CONDA_HOME: /usr/share/miniconda/envs/$(CONDA_ENV)/
7
8
steps:
9
- script: echo "##vso[task.prependpath]$CONDA/bin"
10
displayName: Add conda to PATH
11
12
- task: Cache@2
13
displayName: Use cached Anaconda environment
14
inputs:
15
key: conda | environment.yml
16
path: $(CONDA_HOME)
17
cacheHitVar: CONDA_CACHE_RESTORED
18
19
- script: conda env create --file environment.yml
20
displayName: Create Anaconda environment (if not restored from cache)
21
condition: eq(variables.CONDA_CACHE_RESTORED, 'false')
22
23
- script: |
24
source activate $(CONDA_ENV)
25
pytest
26
displayName: Run unit tests
27