You can use Azure DevOps caching capability for dependencies to avoid redundant installations in the build stage.
You can updare your YAML by incorporating caching for Poetry and dependencies:
pr:
- main
pool:
vmImage: ubuntu-latest
variables:
python_version: "3.10"
package_name: "hatutu"
notebooks_location: "notebooks"
cache_key: poetry | "$(Agent.OS)" | "$(python_version)" | poetry.lock
stages:
- stage: Test
jobs:
- job: Test
displayName: Test
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(python_version)'
displayName: 'Use Python $(python_version)'
# Restore the cache
- task: Cache@2
inputs:
key: '$(cache_key)'
path: $(PIPELINE_WORKSPACE)/.poetry_cache
displayName: 'Restore Poetry and dependencies cache'
- script: |
curl -sSL https://install.python-poetry.org | python3 - --version=1.5.1
echo "##vso[task.setvariable variable=PATH]${PATH}:${HOME}/.poetry/bin"
displayName: 'Install Poetry'
- script: make install
displayName: 'Install dependencies'
# Save to the cache
- task: Cache@2
inputs:
key: '$(cache_key)'
path: $(PIPELINE_WORKSPACE)/.poetry_cache
cacheHitVar: CACHE_RESTORED
displayName: 'Save Poetry and dependencies to cache'
condition: ne(variables.CACHE_RESTORED, 'true')
# ... [Other steps remain unchanged]
- stage: Build
dependsOn: Test
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
jobs:
- job: Build
displayName: Build
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(python_version)'
displayName: 'Use Python $(python_version)'
# Restore the cache
- task: Cache@2
inputs:
key: '$(cache_key)'
path: $(PIPELINE_WORKSPACE)/.poetry_cache
displayName: 'Restore Poetry and dependencies cache'
# You can skip the installation of Poetry and dependencies in the Build stage if the cache is restored
- script: make build
displayName: "Build Package"
# ... [Other steps remain unchanged]
Changes I made:
- Introduced
cache_key
variable for generating a consistent cache key. - Introduced
Cache@2
tasks in theTest
stage to restore and save the Poetry installation and dependencies to/from cache. - Introduced
Cache@2
task in theBuild
stage to restore Poetry installation and dependencies from the cache.