Last active
November 9, 2024 12:35
-
-
Save TheDruidsKeeper/84c58535d3c381bc352ecc0200185f13 to your computer and use it in GitHub Desktop.
Azure DevOps pipeline with multi-stage docker image layer caching support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great to hear 👍