GitHub Actions to build and push images to Azure Container Registry

Muhammad Haris
3 min readSep 18, 2023

--

In today’s fast-paced software development landscape, deploying applications swiftly and securely is paramount. With the rise of containerization, Docker has become a go-to solution for packaging applications and their dependencies in portable containers. Managing these containers efficiently is where container registries come into play, and Azure Container Registry (ACR) is a robust choice. In this article, we’ll explore how GitHub Actions can be harnessed to automate the process of building and pushing Docker images to Azure Container Registry, enhancing your CI/CD pipeline.

Introduction

Containers have revolutionized how we develop, package, and deploy applications. They provide a consistent environment that includes all the dependencies required to run an application, ensuring that what runs on a developer’s laptop is the same as what runs in production. Docker has emerged as the de facto standard for containerization, making it easier to build, package, and distribute applications.

Azure Container Registry (ACR)

Azure Container Registry is a managed Docker registry service provided by Microsoft Azure. It allows developers to store and manage Docker container images, making it a valuable component of a container-based CI/CD pipeline. ACR provides secure, scalable, and high-performance storage for Docker images, enabling teams to efficiently manage container deployments.

GitHub Actions for CI/CD

GitHub Actions is a powerful automation platform integrated directly into GitHub. It allows developers to define custom workflows that automate various tasks, such as building, testing, and deploying code. GitHub Actions is highly flexible and can be used to automate virtually any aspect of your software development lifecycle.

Building and Pushing Docker Images to ACR

Here’s a step-by-step guide on how to use GitHub Actions to build and push Docker images to Azure Container Registry:

  1. Set Up Azure Container Registry: If you don’t already have an Azure Container Registry, create one in your Azure portal. Make sure to note down the registry name, resource group, login server, and enable admin login to get the password.
  2. Configure GitHub Secrets: To securely store sensitive information like your Azure credentials, use GitHub Secrets. Create secrets for your Azure Service Principal credentials, ACR username, and ACR password.
  3. Create a GitHub Action Workflow: In your GitHub repository, create a .github/workflows directory if it doesn't already exist. Inside this directory, create a YAML file (e.g., build-and-push.yml) to define your GitHub Action workflow.
  4. Define Your Workflow: Define the workflow to trigger when a specific event occurs, such as pushing to a specific branch. Use the Azure CLI to log in to Azure with your service principal credentials.
name: Build and Push to ACR
on:
push:
branches:
- 'main'
jobs:
build:
name: 'Build and Push to ACR'
runs-on: ubuntu-latest

defaults:
run:
shell: bash

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Docker Login
uses: azure/docker-login@v1
with:
login-server: ${{ secrets.AZURE_URL }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}

- name: Build and Push to ACR
uses: docker/build-push-action@v2
with:
push: true
tags: ${{ secrets.IMAGE_TAG }}
file: Dockerfile

Push Code to Trigger Workflow

Push your code to the main branch to trigger the GitHub Action workflow. You can monitor the workflow’s progress directly in your GitHub repository.

Conclusion

Leveraging GitHub Actions to automate the building and pushing of Docker images to Azure Container Registry can significantly enhance your CI/CD pipeline. This automation reduces manual intervention, streamlines the deployment process, and ensures consistency across environments. With this robust setup, you can confidently and efficiently manage your containerized applications in Azure.

By embracing the power of GitHub Actions and Azure Container Registry, you’ll be well-equipped to tackle the challenges of modern software development, allowing you to focus on what truly matters: delivering high-quality software to your users.

In our next installment, we’ll explore more advanced techniques for orchestrating containerized workloads, so stay tuned for an even deeper dive into the world of containerization and automation.

--

--

Muhammad Haris

I am a Software Engineer working with Python, Django. I am here to share my knowledge to my fellow developers so we all grow together.