Create an Automatically Triggered Task with Azure Container Registry
Azure Container Registry Automatically Triggered Tasks offers you the ability to automatically trigger updates to your container images within the registry through the use of three different types of triggers.
- On a source code update
- On a base image update
- On a schedule
In this tutorial we will create an Azure Container Registry and then we will use all three types of automated triggers to update a container image within your Azure Container Registry.
This content is available in video form on the Cloud Engineer Skills YouTube channel.
Prerequisites
Before we get started for this tutorial you will need the following:
Creating an Automatically Triggered Task using Azure Container Registry
Before we look at the three ways to use automatically trigger tasks we will start by creating our Azure Container Registry.
Creating the Azure Container Registry
We will start by creating an Azure Container Registry inside a resource group that we will re-use for all three different types of automatically triggered tasks.
1
2
3
az login
az group create --name cloudengineerskills-acr-cli-rg --location eastus
az acr create --resource-group cloudengineerskills-acr-cli-rg --name cloudengineerskillscliacr --sku Premium
Source Code Update Automatically Triggered Task
The Azure Container Registry source code update automatically triggered task allows you to monitor a specific branch in a Git repo in GitHub or Azure DevOps for new commits or pull requests and then trigger an update to your container image.
You will use the az acr task create
command to create this type of automatically triggered task. This command will setup a webhook in the Azure Container Registry to monitor for source code updates.
To disable/enable updates on commits use the --commit-trigger-enabled
flag in az acr task create
, which defaults to true if not present.
To disable/enable updates on pull requests use the --pull-trigger-enabled
flag in az acr task create
.
To specify a Git repo URL, use the --context
flag in az acr task create
, and to specify a specific branch append a hash symbol with the branch name to Git repo URL.
The task requires a personal access token, in GitHub you can generate this personal access token by following these indepth instructions.
Create a Public GitHub Repo
You will need a GitHub or Azure DevOps to connect with your source update automatically triggered ACR task.
You can create your own repo or form this repo I have created.
Build a Container Image and Push it into a Repository in ACR
1
2
3
az acr build --image node-hello-world:v1 --registry cloudengineerskillscliacr --file Dockerfile .
az acr repository list --name cloudengineerskillscliacr --output table
az acr run --registry cloudengineerskillscliacr --cmd 'cloudengineerskillscliacr.azurecr.io/node-hello-world:v1' /dev/null
Create the ACR Task
- Make a personal access token
- https://github.com/settings/tokens
1
2
3
4
5
6
7
8
9
az acr task create
--registry cloudengineerskillscliacr
--name nodehelloworldtask
--image node-hello-world
--context https://github.com/jarrodlilkendey/acr-trigger-on-source.git#master
--file Dockerfile
--git-access-token <GIT-ACCESS-TOKEN>
az acr task list --registry cloudengineerskillscliacr
Test Task Run
1
2
3
az acr task list-runs -r cloudengineerskillscliacr -o table
az acr task run --registry cloudengineerskillscliacr --name nodehelloworldtask
az acr task list-runs -r cloudengineerskillscliacr -o table
Make a Commit or Pull Request to the Repo and Verify the Update in ACR
1
2
3
az acr run --registry cloudengineerskillscliacr --cmd 'cloudengineerskillscliacr.azurecr.io/node-hello-world:latest' /dev/null
az acr task list-runs -r cloudengineerskillscliacr -o table
az acr task logs --registry cloudengineerskillscliacr --run-id ca3
Base Image Update Automatically Triggered Task
- Build the base image and push to ACR
- inside the base directory in the lab
1
az acr build --registry cloudengineerskillscliacr --image baseimages/node:20-alpine --file Dockerfile-base .
- Create the task
1
2
3
4
5
6
7
az acr task create
--registry cloudengineerskillscliacr
--name nodehelloworldusesbasetask
--image node-hello-world-uses-base
--context https://github.com/jarrodlilkendey/acr-trigger-on-base-image.git#master
--file Dockerfile
--git-access-token <GIT-ACCESS-TOKEN>
- az acr task run to manually trigger the task and build the application image
- this makes ACR aware of the application image’s dependency on the base image
1
az acr task run --registry cloudengineerskillscliacr --name nodehelloworldusesbasetask
- Update the base image, run a quick task on it with az acr build
- inside the base directory in the lab
1
2
3
az acr task list-runs --registry cloudengineerskillscliacr --output table
az acr build --registry cloudengineerskillscliacr --image baseimages/node:20-alpine --file Dockerfile-base-update .
az acr task list-runs --registry cloudengineerskillscliacr --output table
Scheduled Automatically Triggered Task
Azure Container Registry scheduled automatically trigger tasks allow you to update your container images within your registry at a given schedule defined using a Cron expression.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
az acr task create
--registry cloudengineerskillscliacr
--name nodehelloworldtimertask
--image node-hello-world
--context https://github.com/jarrodlilkendey/acr-trigger-on-source.git#master
--file Dockerfile
--git-access-token <GIT-ACCESS-TOKEN>
--schedule "* * * * *"
az acr task show --name nodehelloworldtimertask --registry cloudengineerskillscliacr --output table
az acr task timer list --name nodehelloworldtimertask --registry cloudengineerskillscliacr
az acr task timer add
--name nodehelloworldtimertask
--registry cloudengineerskillscliacr
--timer-name nodehelloworldtimertask2
--schedule "*/15 * * * *"
az acr task timer list --name nodehelloworldtimertask --registry cloudengineerskillscliacr
az acr task timer update
--name nodehelloworldtimertask
--registry cloudengineerskillscliacr
--timer-name nodehelloworldtimertask2
--schedule "*/5 * * * *"
az acr task timer list --name nodehelloworldtimertask --registry cloudengineerskillscliacr
az acr task timer remove
--name nodehelloworldtimertask
--registry cloudengineerskillscliacr
--timer-name t1
az acr task list-runs -r cloudengineerskillscliacr -o table
Cleaning up the Resources
1
az group delete --resource-group cloudengineerskills-acr-cli-rg
Further Reading
- https://learn.microsoft.com/en-us/azure/container-registry/container-registry-tasks-scheduled
- https://learn.microsoft.com/en-us/cli/azure/acr/task?view=azure-cli-latest#az-acr-task-create
- https://learn.microsoft.com/en-us/azure/container-registry/container-registry-tutorial-base-image-update
- https://learn.microsoft.com/en-us/azure/container-registry/tasks-consume-public-content