In this article, I am going to walk through the process of using Azure Container Registry (ACR) tasks to automate building Docker container images when you commit source code to a git repository. By doing this you can save yourself a lot of time.
Prerequisites
- So you will need to have an Azure Container Registry (ACR) https://pixelrobots.co.uk/2019/03/create-an-azure-container-registry-and-allow-aks-access/
- GitHub account. With some code for a container. You can use this one to test with. https://github.com/PixelRobots/ContainerPlay/tree/master/ACRTasks
- You will also need a GitHub personal access token (PAT).
If you do not have a PAT then follow below to create one.
Go to https://github.com/settings/tokens/new
In the description box type something like” ACR Auto Build”
Under the scopes section you will see repo. Here tick repo:status and public_repo

Scroll down and then click Generate token and enter your password if asked.
Save the new token into a safe place like LastPass. You will need this later.
Lets create the build task
That’s the prerequisites out of the way, on to the fun stuff, building the build task.
Open the Azure Cloud Shell via the Azure Portal or via https://shell.azure.com and select Bash.
In here you can set some environment variables. This is an optional step, but it does make following this guide easier. If you skip this step then you will have to enter your details manually with each command.
So to set the variables type the following.
1 2 3 |
DEV_ACR_NAME=registry-name # The name of your Azure container registry GIT_USER=github-username # Your GitHub user account name GIT_PAT=personal-access-token # The PAT you generated in the previous section |

Now we can actually create the task. To do this in the Cloud Shell Bash window make sure you are connected to the subscription your ACR is in and then type the following.
1 2 3 4 5 6 7 8 9 |
az acr task create \ --registry $DEV_ACR_NAME \ --name buildpixelweb \ --image pixelweb:{{.Run.ID}} \ --context https://github.com/$GIT_USER/ContainerPlay.git \ --branch master \ --file ACRTasks/dockerfile \ --git-access-token $GIT_PAT \ --platform windows |

So what did you just do? The above command creates the ACR build task. It sets the context to the ACR, it then calls the task whatever you input for –name. The –image is the section that names the new container image. The {{.Run.ID}} forms the tag value. This ensures the image is tagged uniquely. –Context just links to your git repo. Then you set what branch to use with the –branch switch. –file is the location and name of your docker file. The — git-access-token is the token you created earlier from GitHub.
If you are creating Windows containers than you need the –platform switch. If you are creating Linux containers than you do not need to supply it.
Time to test!
The task has been built, but has not ran yet. You could push some code to your repo and branch, or you could run the task manually.
Run the task manually
In the cloud shell run the following. If you have changed the task name before, just make sure you change it here too.
1 |
az acr task run --registry $DEV_ACR_NAME --name buildpixelweb |

Success, the container created!
Run a build via a commit
If you cloned my repo then just edit the html file using vscode and push the changes back to your repo.
After you have committed your changes, the webhook that ACR Tasks created fires and starts the build process. If you would like to view the logs you can use the following command.
1 |
az acr task logs --registry $DEV_ACR_NAME |

Check your builds
You can view the status of your builds by using the following command.
1 |
az acr task list-runs --registry $DEV_ACR_NAME --output table |

As you can see in the image above, I have two failed builds as the platform mismatched and two successful builds, one manually started and one via commit.
You can also view this information and log files from within the Azure portal. If you navigate to Container Registries, and then your container registry. On the left under Services is Tasks click that and you will have a list of all the tasks and a link to each log file.

There you have it every time you push code to your GitHub repo a new container image is built!
I hope you found this article helpful. If you have any questions please reach out.
0 Comments