Build and push Docker Image to Azure Container Registry using GitHub Action

In this blog post I am going to show how you can build and push Docker Images to Azure Container Registry (ACR) using a GitHub Action.

Creating Azure Container Registry

We will use Az CLI to create the Azure Container Registry, script below:

#!/bin/sh
 
ACR_RESOURCE_GROUP_NAME="tamopsgithubacr-rg"
ACR_NAME="tamopsgithubacr"
 
# Create a resource group to store container registry
az group create --name $ACR_RESOURCE_GROUP_NAME --location uksouth
 
# Create a container registry that will be where you deploy your image to
az acr create --resource-group $ACR_RESOURCE_GROUP_NAME --name $ACR_NAME --sku Basic --admin-enabled

A successful run of the above script will create a resource group & container registry as below screenshot shows

Notice the reference of --admin-enabled above? We will be authorising the GitHub Action using the admin credentials of the Azure Container Registry.

Update GitHub repository with ACR credentials

To allow the GitHub action to successfully Build and deploy the image to the Azure Container Registry, we need to add GitHub repository secrets.

The sample repository I will be using is here

Select Azure Container Registry which you created -> Access Keys tab within settings (note below: shows passwords, this container registry has already been deleted, created only to create this blog post)

Add 3 secrets as below from the access keys tab above:

  • REGISTRY_LOGIN_SERVER – Login server
  • REGISTRY_USERNAME – Username
  • REGISTRY_PASSWORD – Password

Secrets are added to GitHub repository by URL below (update to your repo settings):

https://github.com/thomast1906/Docker-Image-ACR-GitHub-Action/settings/secrets/actions/new

Successful addition of secrets will show in repository secrets as below:

Add GitHub Action to repository

Now add the below GitHub Action to your repository in location: https://github.com/thomast1906/Docker-Image-ACR-GitHub-Action/actions/workflows/main.yaml (please update the URL location to your repository)

  • Update the tags value (highlighted below) with your ACR name
name: docker_build_push_acr

on:
  workflow_dispatch:

jobs:
  docker_build_push_acr:
    name: 'Docker Build and Push to ACR'
    runs-on: ubuntu-latest
    environment: production
 
    # Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
    defaults:
      run:
        shell: bash
 
    steps:
    # Checkout the repository to the GitHub Actions runner
    - name: Checkout
      uses: actions/checkout@v2
 
    - name: 'Docker Login'
      uses: azure/docker-login@v1
      with:
        login-server: ${{ secrets.REGISTRY_LOGIN_SERVER }}
        username: ${{ secrets.REGISTRY_USERNAME }}
        password: ${{ secrets.REGISTRY_PASSWORD }}

    - name: Build the frontend image and push it to ACR
      uses: docker/build-push-action@v2
      with:
        push: true
        tags: tamopsgithubacr.azurecr.io/aspcoresample:${{ github.sha }}
        file: aspnet-core-dotnet-core/Dockerfile

Currently no CI triggers are set to automatically run the pipeline – run the pipeline manually from here:

https://github.com/thomast1906/Docker-Image-ACR-GitHub-Action/actions/workflows/main.yaml

A successful output of docker build and pushing to ACR will look like below:

Reviewing ACR, we can see the newly created image, ready to be deployed

Awesome! This blog post has shown how you can build and push Docker Images to Azure Container Registry (ACR) using a GitHub Action.

GitHub repository containing example here

GitHub repository showing GitHub action here

3 comments

    1. Thanks JB; it can depend on the use case.

      This is an example of using admin user; an alternative can be the likes of managed identity/service principal.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s