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 }}

Posted

in

,

by

Comments

One response to “Build a Docker Image and Push it to ACR Using GitHub Actions”

  1. Jhon Avatar
    Jhon

    I’m having an issue on the build part. I built my .NET API just fine but on the docker build part I get this:

    Step 7/17 : COPY [“test/test.csproj”, “test/”]
    COPY failed: file not found in build context or excluded by .dockerignore: stat test/test.csproj: file does not exist

    I don’t have any “*.csproj” file in the .dockerignore file.
    Any ideas on this? I’m working with a monorepo.
    Thanks in advance.