Build a Docker Image and Push it to ACR Using GitHub Actions

In this post, we will build a Docker image using GitHub Actions and push it to Azure Container Registry (ACR).

In the last few articles about GitHub Actions, we learned how to connect GitHub to Azure using secrets to review these articles to understand what we did fully.

Prerequisite

To connect to ACR, you will need to note down your ACR details, including username, hostname and password and save the username and password as secrets (GitHub Actions -> Settings -> Secrets). In our case, I am using the following secrets to store the ACR username and password.

ACR_USERNAME

ACR_PASSWORD

We used secrets to authentication to Azure in this article. If you need to create an ACR with Terraform, check this post.

Workflow

If your secrets and ACR are ready, go ahead and copy the following workflow to your repository and run it. Make sure you change the ACR server details and image name before saving it.

In my case, my repository containers a Dockerfile that is located at the root of the repository. GitHub Actions will find it and run the build commands. If your image is Windows-based, make sure you change the run-on line to windows and not Ubuntu.

name: Build a Docker image and Push it to ACR

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      
      - uses: azure/docker-login@v1
        with:
          login-server: yourACRname.azurecr.io
          username: ${{ secrets.ACR_USERNAME }}
          password: ${{ secrets.ACR_PASSWORD }}

      - run: |
          docker build . -t yourACRname.azurecr.io/appdb:${{ github.sha }}
          docker push yourACRname.azurecr.io/appdb:${{ github.sha }}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.