How to publish docker image as an artifact in azure devops - azure

I am building a Docker image in my Azure pipeline. Now, I want to push this image to multiple aws accounts(dev, stage, prod) depending on the configuration parameters. The problem is, image is not available in publish artifact. I came across this and this article during my research. I am confused about the solution regarding saving the docker image so it can be available in publish artifact. I have two specific questions:
How will I use the docker save command in Azure pipeline task after docker build. The available docker task doesn't have this command.
Is there any better way of doing this apart from saving an image.

How will I use the docker save command in Azure pipeline task after docker build. The available docker task doesn't have this command.
This related to the task version.
Steps: Add task docker->switch the task version to 0->select the option Run a Docker command, then we could run the docker save command, check the pic below.
Is there any better way of doing this apart from saving an image.
We recommend that you use this to upload the docker image as an artifact.

Nowadays the preferred way of writing pipelines is with yaml.
Please see my code on how to do it in a multistage pipeline with Artifactory docker registry.
If we are in master the the MY_ADDITIONAL_TAG will be also pushed.
trigger:
- master
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build DevOps base image
jobs:
- job: Build_and_Push
steps:
- task: Docker#2
displayName: Build
inputs:
containerRegistry: ''
repository: 'MY_REPO/IMAGE'
command: 'build'
Dockerfile: 'PATH_TO_MY_DOCKERFILE'
tags: $(tag)
- task: Bash#3
displayName: Save Docker Image
inputs:
targetType: 'inline'
script: |
docker save MY_DOCKER_REPO_IMAGE_NAME:$(tag) -o $(Pipeline.Workspace)/MY_IMAGE_FILE.tar
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(Pipeline.Workspace)/MY_IMAGE_FILE.tar'
artifact: 'MY_ARTIFACT'
publishLocation: 'pipeline'
- stage: Push
displayName: Push DevOps base image
jobs:
- job: Push
steps:
- task: DownloadPipelineArtifact#2
inputs:
buildType: 'current'
artifactName: 'MY_ARTIFACT'
targetPath: '$(Pipeline.Workspace)'
- task: Bash#3
displayName: Load Docker Image
inputs:
targetType: 'inline'
script: |
docker load --input $(Pipeline.Workspace)/MY_IMAGE_FILE.tar
docker tag MY_DOCKER_REPO_IMAGE_NAME:$(tag) MY_DOCKER_REPO_IMAGE_NAME:MY_ADDITIONAL_TAG
- task: Docker#2
displayName: push development tags
inputs:
containerRegistry: 'MY_DOCKER_REGISTRY'
repository: 'MY_REPO/IMAGE'
command: 'push'
tags: |
$(tag)
- task: Docker#2
condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
displayName: push 0-devops tag
inputs:
containerRegistry: 'MY_ARTIFACT'
repository: 'MY_REPO/IMAGE'
command: 'push'
tags: |
MY_ADDITIONAL_TAG

After a lot of reserach I found this article which resolved my issue https://dev.to/n3wt0n/container-image-promotion-across-environments-build-artifact-5887
Note: We can manually add commands which are not present in the Docker task snippet available in Azure DevOps. This is also mentioned in the article and steps to do this.

Related

Run a command in container after deployment in Azure

I'm using Azure for hosting and Azure Pipelines for CI/CD operations
I have an image build and deploy operations defined like that:
- stage: Package
displayName: 'Package app'
jobs:
- job:
steps:
- task: Docker#2
displayName: 'Build image'
inputs:
containerRegistry: '$(containerRegistry)'
repository: '$(containerRepository)'
command: 'build'
Dockerfile: './Dockerfile'
buildContext: '.'
tags: |
$(Build.BuildId)
- task: Docker#2
displayName: 'Push image'
inputs:
command: push
containerRegistry: '$(containerRegistry)'
repository: '$(containerRepository)'
tags: |
$(Build.BuildId)
- stage: Deploy
displayName: 'Deploy'
jobs:
- job:
steps:
- task: AzureWebAppContainer#1
inputs:
azureSubscription: $(subscription)
appName: $(appName)
What should I do to execute some operations in my container after task AzureWebAppContainer is finished? I have to make some database updates after the deploy operation.
I've tried to find documentation for Azure and search for some SO topics, but didn't find any solutions yet, except usage of entrypoint / cmd for database updates, which is not working for me
I think there should be some Azure pipelines mechanism to perform such actions
You can use the startup command in the AzureWebAppContainer#1 or the AzureAppServiceSettings to manage the afterward operation.
By the way, you could also refer to this doc for Azure Web App for Containers to get more details.

Azure DevOps pipeline error: No builds currently exist in the pipeline definition supplied

I am trying to grab a schema from another artifact build and then build and push the artifact. The error I am getting no builds currently exist in the pipeline definition, I know that is usually from not having a pipeline run on that branch yet. I have run the pipeline and pushed the artifact to the repo on that branch so there is an image already there. The schema was published from the other build. I am not sure why it is not grabbing the artifact. Any help would be appreciated.
jobs:
- job: Build_Push_Artifact
steps:
- task: Docker#2
inputs:
containerRegistry: $(azureContainerRegistry)
command: "login"
- task: DownloadPipelineArtifact#2
inputs:
source: specific
project: $(projectId)
pipeline: $(schemaPublishPipelineId)
runVersion: latestFromBranch
runBranch: $(Build.SourceBranch)
artifact: $(schemaArtifact)
patterns: $(schemaArtifactPattern)
path: $(Build.SourcesDirectory)/src
A possible situation that I can reproduce on my side:
If the build pipeline runs failed in some steps, then even the artifact be published successfully, nothing will be catched.
And please try this simple structure:
trigger:
- none
pool:
vmImage: ubuntu-latest
steps:
- task: DownloadPipelineArtifact#2
inputs:
buildType: 'specific'
project: '<Project Name>'
definition: '<Pipeline Name>'
buildVersionToDownload: 'latest'
targetPath: '$(Pipeline.Workspace)'

Azure devops Pipelines: Cannot find Docker Image when Pushing to Azure Registry, 'tag ***/appname'

We build our docker image with a custom .sh file. Then we would like to push it to our azure registry. We have the following step in our azure pipeline:
- task: Docker#2
inputs:
containerRegistry: 'someazureregistry'
repository: 'appname'
command: 'push'
tags: 'latest'
The build is successfull, and as can be seen below it is registered in docker as appname:latest , however for some reason azure does not find it, and asks for an image with the tag ***/appname.
/usr/bin/docker images
/usr/bin/docker push ***/appname:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
appname latest f1e11b4fc19c Less than a second ago 680MB
ubuntu 20.04 a0ce5a295b63 7 days ago 72.8MB
...
The push refers to repository [***/appname]
An image does not exist locally with the tag: ***/appname
##[error]An image does not exist locally with the tag: ***/appname
We have tried different combinations of tags. What is azure expecting here?
It looks like you are building the Docker image separately. Please note that the container image needs to be tag locally for the private container registry before it can be pushed. As Shayki Abramczyk mentioned you need to build the image with containerRegistry.
To resolve this issue you must Tag the image using the "docker tag" command with the specific private container registry.
For example:
docker tag image youoregistryhost:port/xxx/image-name:latest
Besides, you can build and push the Docker image together in Azure DevOps pipeline. The following YAML file for your reference:
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'testacr'
imageRepository: 'appname'
containerRegistry: 'xxx.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/app/Dockerfile'
tag: 'latest'
system.debug : true
# 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)
Of course you can also build and push the image separately in Azure DevOps pipeline like this:
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker#2
displayName: Build an image for container registry
inputs:
containerRegistry: '$(dockerRegistryServiceConnection)'
repository: '$(imageRepository)'
command: 'build'
Dockerfile: '$(dockerfilePath)'
tags: 'latest'
- task: Docker#2
displayName: Push an image to container registry
inputs:
containerRegistry: '$(dockerRegistryServiceConnection)'
repository: '$(imageRepository)'
command: 'push'
tags: 'latest'

Unable to find the pushed image in the azure container registry

I have pushed a new image in the azure container registry
But i am unable to find the image in azure container registry
Please find the deplyment yaml file
name: Dotnet Code Build and Push
jobs:
- job: Job_1
displayName: Agent job 1
pool:
vmImage: ubuntu-18.04
steps:
- checkout: self
- task: Docker#0
displayName: Build an image
inputs:
azureSubscription: 'sc-***dp'
azureContainerRegistry:
loginServer: *****aksacr.azurecr.io
id: "/subscriptions/4f76bb2f-c521-45d1-b311-b87bf**747ff/resourceGroups/eus-***dp-rg/providers/Microsoft.ContainerRegistry/registries/*****aksacr"
imageName: ***dpacr.azurecr.io/ims-***dp/$(Build.Repository.Name):$(Build.BuildId)
- task: Docker#2
displayName: Login to ACR
inputs:
command: login
containerRegistry:'sc-***dp'
- task: Docker#2
displayName: Push an image
inputs:
azureSubscription: 'sc-***dp'
repository: ims-***dp
azureContainerRegistry: '{"loginServer":"*****aksacr.azurecr.io", "id" : "/subscriptions/4f76bb2f-c521-45d1-b311-b87bf**747ff/resourceGroups/eus-***dp-rg/providers/Microsoft.ContainerRegistry/registries/*****aksacr"}'
action: Push an image
I would recommend to create a new service connection with the container registry if one does not exist.
and select Azure Container Registry.
Then you can push in your registry using the below task. Keep in mind that ${{container}} should have a notation as xxx.azurecr.io/imagename:tag
- task: Docker#2
displayName: pushing image ${{container}}
inputs:
containerRegistry: 'serviceConnectionName'
repository: '${{container}}'
command: 'push'
tags: |
mytag
Try to push the code in azure devops pipeline so you can directly push image in azure container registry
Here we can docker task it can build up and push the image with help of setting and it take the image from repository and then it can push into the azure container registry
task: Docker#2
displayName: Push an image
inputs:
command: buildAndpush
repository: $(imageRepository)
dockerfile: $(dockerfilepath)
ContainerRegistry: $(dockerRepositoryserviceconnectio)
tags:
$(tag)
Save and run.
Note : After running you can't see Repository please refresh again because in azure pipeline the job may be still running once the job is completed & successfully run and refresh you can able see image is pushed
Please refer this document by rajaniesh kaushikk has given detail information
Please check the repository and containerRegistry value in your Docker#2 task, make sure they points to target resource. My script for your reference below. Please note the parameters in Docker#2 task, no azureContainerRegistry and command needed.
variables:
dockerRegistryServiceConnection: acrconn1
imageRepository: 'acrlearn'
containerRegistry: 'test.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
# Agent VM image name
vmImageName: 'windows-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)

Can't see container

I'm trying to push a built docker image in a release pipeline.
My docker build task yaml is:
steps:
- task: Docker#2
displayName: Build
inputs:
containerRegistry: MyRegistry
repository: myrepo/containername
command: build
Dockerfile: '$(System.DefaultWorkingDirectory)/My.dockerfile'
buildContext: '$(System.DefaultWorkingDirectory)'
arguments: '--build-arg FILE_NAME=myfile.zip'
My docker push task yaml is:
steps:
- task: Docker#2
displayName: Push
inputs:
containerRegistry: MyRegistry
repository: myrepo/containername
command: push
The log says it runs this command:
/usr/bin/docker push ***/myrepo/containername:tag
The tasks reports success, but I can't see the resulting image in dockerhub.
I wonder if the *** has anything to do with this?
I ended up not using the Azure DevOps tasks and writing the commands via a python script. It was important to docker login inside the python script also.

Resources