Last active
February 18, 2025 20:00
-
-
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 hidden or 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 |
@TheDruidsKeeper Thanks for the reply. You are right. In my case I had to build test and production separately(ideally I wanted the test to use production as base). Using the buildkit works. I just have to set the environment variable DOCKER_BUILDKIT=1
. Docker recent versions support skipping stages, but we were using custom agent pools where the docker version was old. Yes I did enable docker caching and it works. Once again thanks a lot for responding.
Great to hear 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@girip11 this solution definitely did work for me, and I believe it's still in use at the company I did it for. The problem is that the issue you're running into is different from what I was solving here.
Your sample (similar to the SO thread) is expecting to skip building the
test
target because it's not referenced by theproduction
final target. As the only answer on SO points out - docker doesn't support skipping stages, but if you use buildkit it looks to be possible (I haven't tried this myself).My sample here is a workaround to MS Build Agents being ephemeral - so there are no layers cached each time a new build runs. By tagging, and pushing/pulling the
latest-build
tag I was able to avoid time spent rebuilding all the layers every time the build ran because docker would see the layer as not having any changes vs what was already built. Properly implemented you'll see messages in your build log on those steps saying "using cache" and the work won't be redone.