Azure Devops deploy docker image to ACR using deployment job
-
I'm trying to deploy a docker image to Azure Container Registry via an Azure Devops pipeline. Now this works fine when I run it with this script:
trigger: - master
variables:
Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'some_id'
imageRepository: 'worker'
containerRegistry: 'microcontainerapptest.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and push stage
jobs:- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
- task: Docker@2
- job: Build
However, I want to actually use a deployment job so I can run the pipeline in different environments. Therefore I updated the .yml like this:
trigger: - master
variables:
Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'some_id'
imageRepository: 'worker'
containerRegistry: 'microcontainerapptest.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and push stage
jobs:- deployment:
displayName: Build docker image
environment: $(DEPLOY_ENVIRONMENT)
strategy:
runOnce:
deploy:
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(Build.SourcesDirectory)/Dockerfile
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
- deployment:
Making a deployment job out of this. Now when I run the pipeline like this I get an error
Unhandled: No Dockerfile matching /home/vsts/work/1/s/Dockerfile was found.
So it seems like the dockerfilePath is not valid anymore when I run it as a deployment job. I have searched but don't have a clue as to why this would be the case, does anybody know the answer to this?
- stage: Build
-
You need to explicitly check out your source code, https://docs.microsoft.com/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops :
A deployment job doesn't automatically clone the source repo. You can checkout the source repo within your job with
checkout: self
. Deployment jobs only support one checkout step.strategy: runOnce: deploy: steps:
- checkout: self - task: Docker@2 displayName: Build and push an image to container registry inputs: command: buildAndPush repository: $(imageRepository) dockerfile: $(Build.SourcesDirectory)/Dockerfile containerRegistry: $(dockerRegistryServiceConnection) tags: | $(tag)