AZ

Azure Pipelines Optimization

Caching, pipeline triggers, and agent pool management

Details

Language / Topic
azure-devopsAzure DevOps Pipelines
Category
Performance

Rules

balanced
- Use Cache@2 task with lockfile hash as key: cache node_modules, NuGet packages, pip, Maven local repo
- Parallelize independent jobs: lint, unit test, and integration test as separate jobs with no dependsOn
- Use path filters on triggers to skip pipelines when only docs or config files change
- Choose self-hosted agents for large builds with persistent caches; hosted agents for stateless CI jobs
- Publish build artifacts once, download in subsequent stages — never rebuild the same code twice
- Cache@2 task: key based on lockfile hash; path points to dependency directory (node_modules, .nuget, .m2)
- restoreKeys for fallback: if exact cache misses, restore most recent cache with matching prefix for partial hit
- Parallelize jobs: lint | unit-test | security-scan run concurrently; build job depends on all with dependsOn
- Path trigger filters: trigger: paths: include: ['src/**'] / exclude: ['docs/**', '*.md'] to skip irrelevant runs
- PR triggers: pr: paths filters to run only relevant checks on pull requests; drafts: false to skip draft PRs
- Self-hosted agents: persistent tool caches, pre-installed SDKs, warm Docker layer caches for large builds
- Microsoft-hosted agents: clean environment each run, no cache state, ideal for reproducible stateless builds
- Publish artifacts with PublishPipelineArtifact@1; download with DownloadPipelineArtifact@2 in deploy stages
- Use pipeline triggers (resources: pipelines:) to chain pipelines instead of monolithic multi-stage YAML
- Batch CI triggers: batch: true collapses rapid pushes into one run, preventing queue flooding on active branches
- Cache Docker layers: cache the Docker build context or use BuildKit inline cache for container builds