Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TheDruidsKeeper/84c58535d3c381bc352ecc0200185f13 to your computer and use it in GitHub Desktop.
Save TheDruidsKeeper/84c58535d3c381bc352ecc0200185f13 to your computer and use it in GitHub Desktop.
Azure DevOps pipeline with multi-stage docker image layer caching support
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
variables:
imageRepository: 'multi-stage-build-caching'
containerRegistry: 'ACR-Service-Connection-Name'
registryName: '[redacted].azurecr.io'
tag: '$(Build.BuildNumber)'
steps:
- task: Docker@2
displayName: "Registry Login"
inputs:
containerRegistry: '$(containerRegistry)'
command: 'login'
- script: "docker pull $(registryName)/$(imageRepository):latest-build && docker pull $(registryName)/$(imageRepository):latest"
displayName: "Pull latest for layer caching"
condition: ne(variables['USE_BUILD_CACHE'], 'false')
continueOnError: true # for the first build, tags will not exist yet
- task: Docker@2
displayName: "Build - Builder Stage"
inputs:
containerRegistry: '$(containerRegistry)'
repository: '$(imageRepository)'
command: 'build'
Dockerfile: './Dockerfile'
buildContext: '.'
arguments: '--target builder --cache-from=$(registryName)/$(imageRepository):latest-build'
tags: |
$(tag)-build
latest-build
- task: Docker@2
displayName: "Build - Final Stage"
inputs:
containerRegistry: '$(containerRegistry)'
repository: '$(imageRepository)'
command: 'build'
Dockerfile: './Dockerfile'
buildContext: '.'
arguments: '--cache-from=$(registryName)/$(imageRepository):latest-build --cache-from=$(registryName)/$(imageRepository):latest'
tags: |
$(tag)
latest
- task: Docker@2
displayName: "Push image"
inputs:
command: push
containerRegistry: "$(containerRegistry)"
repository: $(imageRepository)
tags: |
$(tag)-build
latest-build
$(tag)
latest
@TheDruidsKeeper
Copy link
Author

Great to hear 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment