教程:按定义的计划运行 ACR 任务Tutorial: Run an ACR task on a defined schedule

本教程介绍如何按计划运行 ACR 任务This tutorial shows you how to run an ACR Task on a schedule. 通过设置一个或多个计时器触发器来计划任务。 Schedule a task by setting up one or more timer triggers. 计时器触发器可以单独使用,也可以与其他任务触发器结合使用。Timer triggers can be used alone, or in combination with other task triggers.

本教程介绍如何计划任务及完成以下任务:In this tutorial, learn about scheduling tasks and:

  • 创建具有计时器触发器的任务Create a task with a timer trigger
  • 管理计时器触发器Manage timer triggers

任务计划适用于如下所述的方案:Scheduling a task is useful for scenarios like the following:

  • 运行容器工作负荷来执行计划性维护操作。Run a container workload for scheduled maintenance operations. 例如,运行容器化应用以从注册表中删除不需要的映像。For example, run a containerized app to remove unneeded images from your registry.
  • 在工作日针对要在现场监视的生产映像运行一组测试。Run a set of tests on a production image during the workday as part of your live-site monitoring.


  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

关于计划任务About scheduling a task

  • 使用 cron 表达式的触发器 - 任务的计时器触发器使用 cron 表达式。 Trigger with cron expression - The timer trigger for a task uses a cron expression. 该表达式是包含五个字段的字符串,这些字段指定要触发任务的分钟、小时、日期、月份和星期。The expression is a string with five fields specifying the minute, hour, day, month, and day of week to trigger the task. 支持的最高频率为每分钟一次。Frequencies of up to once per minute are supported.

    例如,表达式 "0 12 * * Mon-Fri" 在每个工作日的中午(UTC 时间)触发任务。For example, the expression "0 12 * * Mon-Fri" triggers a task at noon UTC on each weekday. 请参阅本文稍后所述的详细信息See details later in this article.

  • 多个计时器触发器 - 允许将多个计时器添加到g wh 任务,前提是计划不同。Multiple timer triggers - Adding multiple timers to a task is allowed, as long as the schedules differ.

    • 可在创建任务时指定多个计时器触发器,也可以在以后添加。Specify multiple timer triggers when you create the task, or add them later.
    • (可选)为触发器命名以便于管理,否则 ACR 任务将提供默认的触发器名称。Optionally name the triggers for easier management, or ACR Tasks will provide default trigger names.
    • 如果多个计时器计划在某个时间发生重叠,ACR 任务将在每个计时器的计划时间触发任务。If timer schedules overlap at a time, ACR Tasks triggers the task at the scheduled time for each timer.
  • 其他任务触发器 - 在计时器触发的任务中,还可以基于 源代码提交基础映像更新启用触发器。Other task triggers - In a timer-triggered task, you can also enable triggers based on source code commit or base image updates. 与其他 ACR 任务一样,也可以手动运行计划任务。Like other ACR tasks, you can also manually run a scheduled task.

创建具有计时器触发器的任务Create a task with a timer trigger

任务命令Task command

首先,使用适用于环境的值填充以下 shell 环境变量。First, populate the following shell environment variable with a value appropriate for your environment. 此步骤并非必须执行的步骤,但它能让在此教程中执行多个 Azure CLI 命令更容易。This step isn't strictly required, but makes executing the multiline Azure CLI commands in this tutorial a bit easier. 如果未填充环境变量,则每当示例命令中出现一个值,都必须手动替换该值。If you don't populate the environment variable, you must manually replace each value wherever it appears in the example commands.

ACR_NAME=<registry-name>        # The name of your Azure container registry

使用 az acr task create 命令创建任务时,可以选择性地添加计时器触发器。When you create a task with the az acr task create command, you can optionally add a timer trigger. 添加 --schedule 参数并为计时器传递 cron 表达式。Add the --schedule parameter and pass a cron expression for the timer.

举个简单的例子,以下任务会触发每天 21:00(UTC 时间)从 Azure 容器注册表运行 hello-world 映像的操作。As a simple example, the following task triggers running the hello-world image from Azure Container Registry every day at 21:00 UTC. 该任务无需源代码上下文即可运行。The task runs without a source code context.

az acr task create \
  --name timertask \
  --registry $ACR_NAME \
  --cmd mcr.microsoft.com/hello-world \
  --schedule "0 21 * * *" \
  --context /dev/null

运行 az acr task show 命令查看该计时器触发器是否已配置。Run the az acr task show command to see that the timer trigger is configured. 默认情况下,还启用了基础映像更新触发器。By default, the base image update trigger is also enabled.

az acr task show --name timertask --registry $ACR_NAME --output table
--------  ----------  --------  -------------------     -----------------
timertask linux       Enabled                           BASE_IMAGE, TIMER

触发任务Trigger the task

使用 az acr task run 手动触发任务,以确保正确设置该任务:Trigger the task manually with az acr task run to ensure that it is set up properly:

az acr task run --name timertask --registry $ACR_NAME

如果容器成功运行,输出将如下所示。If the container runs successfully, the output is similar to the following. 该输出经过简化,只显示关键步骤The output is condensed to show key steps

Queued a run with ID: cf2a
Waiting for an agent...
2020/11/20 21:03:36 Using acb_vol_2ca23c46-a9ac-4224-b0c6-9fde44eb42d2 as the home volume
2020/11/20 21:03:36 Creating Docker network: acb_default_network, driver: 'bridge'
2020/11/20 21:03:38 Launching container with name: acb_step_0

Hello from Docker!
This message shows that your installation appears to be working correctly.

在计划的时间之后,运行 az acr task list-runs 命令验证计时器是否按预期触发了任务:After the scheduled time, run the az acr task list-runs command to verify that the timer triggered the task as expected:

az acr task list-runs --name timertask --registry $ACR_NAME --output table

如果计时器成功,输出将如下所示:When the timer is successful, output is similar to the following:

--------  -------- ----------  ---------  ---------  --------------------  ----------
ca15      timertask  linux       Succeeded  Timer      2020-11-20T21:00:23Z  00:00:06
ca14      timertask  linux       Succeeded  Manual     2020-11-20T20:53:35Z  00:00:06

管理计时器触发器Manage timer triggers

使用 az acr task timer 命令管理 ACR 任务的计时器触发器。Use the az acr task timer commands to manage the timer triggers for an ACR task.

添加或更新计时器触发器Add or update a timer trigger

创建任务后,可以选择性地使用 az acr task timer add 命令添加计时器触发器。After a task is created, optionally add a timer trigger by using the az acr task timer add command. 以下示例将名为 timer2 的计时器触发器添加到前面创建的 timertask。The following example adds a timer trigger name timer2 to timertask created previously. 此计时器每日 10:30(UTC 时间)触发任务。This timer triggers the task every day at 10:30 UTC.

az acr task timer add \
  --name timertask \
  --registry $ACR_NAME \
  --timer-name timer2 \
  --schedule "30 10 * * *"

使用 az acr task timer update 命令更新现有触发器的计划或更改其状态。Update the schedule of an existing trigger, or change its status, by using the az acr task timer update command. 例如,更新名为 timer2 的触发器,以在 11:30(UTC 时间)触发任务:For example, update the trigger named timer2 to trigger the task at 11:30 UTC:

az acr task timer update \
  --name timertask \
  --registry $ACR_NAME \
  --timer-name timer2 \
  --schedule "30 11 * * *"

列出计时器触发器List timer triggers

az acr task timer list 命令可显示针对任务设置的计时器触发器:The az acr task timer list command shows the timer triggers set up for a task:

az acr task timer list --name timertask --registry $ACR_NAME

示例输出:Example output:

    "name": "timer2",
    "schedule": "30 11 * * *",
    "status": "Enabled"
    "name": "t1",
    "schedule": "0 21 * * *",
    "status": "Enabled"

删除计时器触发器Remove a timer trigger

使用 az acr task timer remove 命令从任务中删除计时器触发器。Use the az acr task timer remove command to remove a timer trigger from a task. 以下示例从 timertask 中删除 timer2 触发器:The following example removes the timer2 trigger from timertask:

az acr task timer remove \
  --name timertask \
  --registry $ACR_NAME \
  --timer-name timer2

Cron 表达式Cron expressions

ACR 任务使用 NCronTab 库来解释 cron 表达式。ACR Tasks uses the NCronTab library to interpret cron expressions. ACR 任务支持的表达式包含空格分隔的五个必需字段:Supported expressions in ACR Tasks have five required fields separated by white space:

{minute} {hour} {day} {month} {day-of-week}

cron 表达式使用的时区为协调世界时 (UTC)。The time zone used with the cron expressions is Coordinated Universal Time (UTC). 时间为 24 小时制。Hours are in 24-hour format.


ACR 任务不支持在 cron 表达式中使用 {second}{year} 字段。ACR Tasks does not support the {second} or {year} field in cron expressions. 如果复制其他系统中使用的 cron 表达式,请确保删除这些字段(如果已使用)。If you copy a cron expression used in another system, be sure to remove those fields, if they are used.

每个字段可以具有下列类型之一的值:Each field can have one of the following types of values:

类型Type 示例Example 何时触发When triggered
一个具体值A specific value "5 * * * *" 每小时的第 5 分钟every hour at 5 minutes past the hour
所有值 (*)All values (*) "* 5 * * *" 从 5:00 UTC 开始每隔一分钟(每天 60 次)every minute of the hour beginning 5:00 UTC (60 times a day)
一个范围(- 运算符)A range (- operator) "0 1-3 * * *" 每天 1:00、2:00 和 3:00 UTC,触发 3 次3 times per day, at 1:00, 2:00, and 3:00 UTC
一组值(, 运算符)A set of values (, operator) "20,30,40 * * * *" 每小时的第 20 分钟、30 分钟和 40 分钟,触发 3 次3 times per hour, at 20 minutes, 30 minutes, and 40 minutes past the hour
一个间隔值(/ 运算符)An interval value (/ operator) "*/10 * * * *" 每小时的第 10 分钟、20 分钟...,触发 6 次6 times per hour, at 10 minutes, 20 minutes, and so on, past the hour

若要指定月份或天,可以使用数字值、名称或名称的缩写:To specify months or days you can use numeric values, names, or abbreviations of names:

  • 对于天,数字值为 0 到 6,其中 0 表示星期日。For days, the numeric values are 0 to 6 where 0 starts with Sunday.
  • 名称采用英语。Names are in English. 例如:MondayJanuaryFor example: Monday, January.
  • 名称不区分大小写。Names are case-insensitive.
  • 名称可缩写。Names can be abbreviated. 三字母是建议的缩写长度。Three letters is the recommended abbreviation length. 例如:MonJanFor example: Mon, Jan.

Cron 示例Cron examples

示例Example 何时触发When triggered
"*/5 * * * *" 每五分钟一次once every five minutes
"0 * * * *" 每小时一次(在每小时的开头)once at the top of every hour
"0 */2 * * *" 每两小时一次once every two hours
"0 9-17 * * *" 从 9:00 到 17:00 UTC 每小时触发一次once every hour from 9:00 to 17:00 UTC
"30 9 * * *" 每天 9:30 UTCat 9:30 UTC every day
"30 9 * * 1-5" 每个工作日的 9:30 UTCat 9:30 UTC every weekday
"30 9 * Jan Mon" 一月份的每个星期一 9:30 UTCat 9:30 UTC every Monday in January

清理资源Clean up resources

若要删除本系列教程中创建的所有资源(包括容器注册表、容器实例、密钥保管库和服务主体),请发出以下命令:To remove all resources you've created in this tutorial series, including the container registry or registries, container instance, key vault, and service principal, issue the following commands:

az group delete --resource-group $RES_GROUP
az ad sp delete --id http://$ACR_NAME-pull

后续步骤Next steps

本教程介绍了如何创建由计时器自动触发的 Azure 容器注册表任务。In this tutorial, you learned how to create Azure Container Registry tasks that are automatically triggered by a timer.

有关使用计划任务清理注册表中的存储库的示例,请参阅从 Azure 容器注册表中自动清除映像For an example of using a scheduled task to clean up repositories in a registry, see Automatically purge images from an Azure container registry.

有关由源代码提交或基础映像更新触发的任务的示例,请参阅 ACR 任务系列教程中的其他文章。For examples of tasks triggered by source code commits or base image updates, see other articles in the ACR Tasks tutorial series.