Member-only story
Azure DevOps Pipelines — Build and Push a Docker image to AWS ECR

YAML for pipeline
.azure/azure-pipelines.yml
This file is our main pipeline and only triggers on the master branch.
It uses the templates/build.yml
file as a template and will use the stages defined in that file.
trigger:
branches:
include:
- master
pr: none
stages:
- template: templates/build.yml
.azure/templates/build.yml
This template defines a stage that consists of a single job made up of two steps. The first task will essentially run a docker build
to create our image with a Dockerfile in the root directory of our repo and tagging it with the name defined in the variable DOCKER_REPOSITORY_NAME
and with a version equal to the pipeline build ID $(Build.BuildId)
.
The second task will push this image to ECR
stages:
- stage: Docker
displayName: Build & Push Docker image to AWS ECR
jobs:
- job: Build_and_Push
displayName: Build & Push Docker image
pool:
vmImage: ubuntu-latest
steps:
- task: Docker@2
displayName: Build an image
inputs:
command: build
dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
buildContext: '$(Build.SourcesDirectory)'
repository: $(DOCKER_REPOSITORY_NAME)
- task: ECRPushImage@1
inputs:
awsCredentials: 'AWS_ECR'
regionName: $(AWS_REGION)
imageSource: 'imagename'
sourceImageName: $(DOCKER_REPOSITORY_NAME)
sourceImageTag: $(Build.BuildId)
pushTag: latest
repositoryName: $(DOCKER_REPOSITORY_NAME)
Commit and push these files to your repo’s master branch, and we will create the pipeline in Azure DevOps.
Create a project and pipeline in Azure DevOps
Create a new project in Azure Devops. Once created and in the project, click on “Pipelines” and then “new pipeline”.

After clicking “new pipeline”, select GitHub and then choose your repo you would like to…