To create a CI (Continuous Integration) script in Azure DevOps that caches pip packages, you would typically use Azure Pipelines. Azure Pipelines supports caching through the CacheBeta task, which can help reduce build time by reusing packages saved from previous runs.
Here's an example of how you might write a YAML file for an Azure Pipeline that caches pip packages:
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
PIP_CACHE_DIR: $(Pipeline.Workspace)/.pip
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.8'
addToPath: true
- task: CacheBeta@1
inputs:
key: 'pip | "$(Agent.OS)" | $(Build.SourcesDirectory)/requirements.txt'
restoreKeys: |
pip | "$(Agent.OS)"
path: $(PIP_CACHE_DIR)
displayName: Cache pip packages
- script: |
python -m pip install --upgrade pip
pip install --cache-dir $(PIP_CACHE_DIR) -r requirements.txt
displayName: 'Install dependencies'
Explanation of the key parts of the script:
-
trigger: Defines which branch(es) will trigger the pipeline. In this example, any push to the main branch triggers the pipeline.
-
pool: Specifies the type of agent to run the pipeline. Here, it uses the latest Ubuntu image.
-
variables: Here, we define a variable for the pip cache directory, which Azure Pipelines will use to store and retrieve the cache.
-
UsePythonVersion@0: This task is used to specify the Python version that the pipeline should use.
-
CacheBeta@1: This is the cache task. It uses a key to determine when to restore or save the cache. The key changes if the OS changes or if the requirements.txt file changes, which contains the list of Python packages needed for the project. The restoreKeys option provides a fallback in case the primary key doesn't find a match.
-
script: Executes the pip install command with the specified cache directory and installs the dependencies listed in the requirements.txt file.
Please note that at the time of writing, caching in Azure Pipelines is in beta (CacheBeta@1). The actual task name or usage might change when the feature reaches general availability.
Also, make sure to replace the requirements.txt path with the correct path relative to your repository's structure if it is different. The Python version may also be adjusted as needed.
Remember, this is just an example. Your actual CI pipeline might have additional tasks and parameters depending on the complexity of your project and your specific requirements.