Lesson 2 of 2 Building Images with ACR Tasks

Automating Builds with Triggered and Multi-Step Tasks

This lesson explains the triggers that run saved ACR tasks, how base image triggers patch images automatically, and how multi-step YAML tasks build, test and push.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the four task triggers and their defaults.
  • Explain how base image triggers rebuild dependent images.
  • Describe the build, push and cmd step types.
  • Choose between quick, triggered and multi-step tasks.

Saved tasks and their triggers

A quick task runs once. A task created with az acr task create is a saved resource in the registry that holds a build definition and one or more triggers. Each time a trigger fires, the task runs and its run is recorded. Tasks can also be run manually with az acr task run.

Four kinds of trigger are available, and a single task can combine them.

  • Source code commit: a webhook on a GitHub or Azure Repos repository starts the task when code is pushed. It is enabled by default when the context is a Git repository, and it needs a personal access token so that ACR can create the webhook.
  • Pull request: starts the task when a pull request is opened or updated. It is disabled by default.
  • Base image update: starts the task when the base image named in the Dockerfile's FROM line is updated in the same registry, another ACR, or a public registry.
  • Timer: starts the task on a cron schedule in UTC, for example nightly rebuilds or clean-up jobs.
Match the pairs

Task triggers

Exam skills 1.1.2

Match each event with the trigger that reacts to it.

Log in to try this activity Interactive activities are free with an account.

Automatic patching with base image triggers

Security fixes for operating system packages and language runtimes arrive in base images. Without automation, every application image built on a patched base remains vulnerable until someone remembers to rebuild it. ACR Tasks tracks the base image dependency of each task it builds: when it builds an image, it records the base image's digest, and when that base image is updated, it queues a rebuild of every dependent image.

A common design keeps a curated base image, such as contoso.azurecr.io/base/python:3.12, imported from Microsoft Artifact Registry and patched centrally. Application tasks reference it in their FROM line. When the platform team updates the base, every application image is rebuilt with a new unique tag, ready for deployment through the normal release process.

  1. Create the base image task

    A task builds or imports the curated base image and pushes it with a stable tag.

  2. Create application tasks

    Each application task's Dockerfile uses FROM REGISTRY.azurecr.io/base/python:3.12, and the base image trigger is enabled.

  3. Update the base

    The platform team pushes a patched base image with the same stable tag.

  4. Rebuild automatically

    ACR detects the new base digest and runs every dependent application task, producing new images.

The following command creates a task that builds from a GitHub repository, with both commit and base image triggers. The token is a GitHub personal access token with repository access.

az acr task create --registry REGISTRY --name build-orders-api \
  --image "orders-api:{{.Run.ID}}" \
  --context https://github.com/contoso/orders-api.git#main \
  --file Dockerfile --git-access-token GITHUB_TOKEN \
  --base-image-trigger-enabled true --commit-trigger-enabled true

The following diagram shows how one base image update rebuilds every service image.

Automatic patching with base image triggers. When the platform team pushes a patched base image, ACR detects the update and runs the saved multi-step task of each dependent service. Each task builds, tests and pushes with a unique run ID tag, and an image whose tests fail is not pushed. Timer and commit triggers would not react to the base image change.
Only images whose tests pass are pushed.
Put in order

How a base image update spreads

Exam skills 1.1.2

Put the events of automatic patching in order.

Log in to try this activity Interactive activities are free with an account.

Multi-step tasks

Many builds need more than one step: build the image, run its unit tests inside it, push it only if the tests pass, and perhaps build a second image. A multi-step task describes these steps in a YAML file, usually named acr-task.yaml, and runs them in order or in parallel. Each step is one of three types.

Step typeWhat it doesExample
buildBuilds an image with Docker build syntaxbuild: -t $Registry/api:$ID -f Dockerfile .
pushPushes one or more built imagespush: ["$Registry/api:$ID"]
cmdRuns a container as a step, such as a test runner or a CLIcmd: $Registry/api:$ID python -m pytest

The following file builds an API image, runs its tests in the image, and pushes the image only if the test step succeeds. $Registry and $ID are aliases for the registry login server and the run ID.

version: v1.1.0
steps:
  - id: build
    build: -t $Registry/orders-api:$ID -f Dockerfile .
  - id: test
    cmd: $Registry/orders-api:$ID python -m pytest -q
    when: ["build"]
  - id: push
    push:
      - $Registry/orders-api:$ID
    when: ["test"]

The when property sets dependencies, so steps without dependencies on each other can run in parallel. A step that fails stops the task, which is what keeps untested images out of the registry. The file is run with az acr run --file acr-task.yaml . or saved as a task with az acr task create --file acr-task.yaml.

Sort into groups

Build, push or cmd?

Exam skills 1.1.2

Decide which step type of a multi-step YAML task does each job.

Log in to try this activity Interactive activities are free with an account.

Choosing the right kind of task

The same build can be expressed in several ways, and exam scenarios usually describe the trigger or the workflow that decides the choice.

Quick task
Started byA person or a pipeline, on demand
DefinitionCommand-line options
Typical useInner loop, Cloud Shell, CI pipeline step
Choose when A one-off build without Docker
Triggered task
Started byCommit, pull request, base image update or timer
DefinitionSaved task resource
Typical useAutomatic rebuilds and patching
Choose when Images must rebuild by themselves
Multi-step task
Started byOn demand or by any trigger
DefinitionYAML file with steps
Typical useBuild, test and push in one run
Choose when Tests must pass before a push
Scenario questions

Quick, triggered or multi-step?

Exam skills 1.1.2

Choose the kind of ACR task each team needs.

Log in to try this activity Interactive activities are free with an account.
Worked example

Patching dozens of images automatically

An organization runs 35 Python microservices, all built on a shared base image in its registry. Last month a critical OpenSSL fix took two weeks to reach production because each team rebuilt manually. The organization wants every service image rebuilt within minutes of a base image update, with tests run before any new image is pushed.

Each service should have a saved ACR task defined by a multi-step YAML file that builds, tests and then pushes, with the base image trigger enabled. When the platform team pushes the patched base, ACR detects the change and runs all 35 tasks; an image is pushed only when its tests pass, with a unique tag from the run ID. Deployment pipelines can then pick up the new tags.

A timer trigger would rebuild nightly whether or not the base changed, and could still leave a gap of up to a day. A quick task in each team's pipeline would still depend on someone starting it. A commit trigger fires only when the application code changes, not when the base image does.

Check yourself

Which ACR task trigger is enabled by default for a Git context, and which is disabled by default?

Show answer

The source code commit trigger is enabled by default; the pull request trigger is disabled by default. Base image and timer triggers are configured as needed.

Exam Tips

Remember for the exam

  • Commit triggers are on by default for Git contexts; pull request triggers are off.
  • Base image triggers rebuild dependent images when the FROM image is updated.
  • Multi-step YAML has build, push and cmd steps; when sets dependencies.
  • Timer triggers use cron schedules in UTC.