在 ACR 任务中运行多步骤生成、测试和修补任务Run multi-step build, test, and patch tasks in ACR Tasks

多步骤任务凭借基于多步骤、多容器的工作流扩展了 ACR 任务的单一映像生成和推送功能。Multi-step tasks extend the single image build-and-push capability of ACR Tasks with multi-step, multi-container-based workflows. 使用多步骤任务以串行或并行方式生成和推送多个映像。Use multi-step tasks to build and push several images, in series or in parallel. 然后,在单个任务运行中以命令的形式运行这些映像。Then run those images as commands within a single task run. 每个步骤定义一个容器映像生成或推送操作,此外还可以定义容器的执行。Each step defines a container image build or push operation, and can also define the execution of a container. 多步骤任务中的每个步骤使用一个容器作为其执行环境。Each step in a multi-step task uses a container as its execution environment.

Important

如果以前在预览期使用 az acr build-task 创建了任务,则需要使用 az acr task 命令重新创建这些任务。If you previously created tasks during the preview with the az acr build-task command, those tasks need to be re-created using the az acr task command.

例如,可以运行一个任务,其中的步骤可以自动完成以下逻辑:For example, you can run a task with steps that automate the following logic:

  1. 生成 Web 应用程序映像Build a web application image
  2. 运行 Web 应用程序容器Run the web application container
  3. 生成 Web 应用程序测试映像Build a web application test image
  4. 运行针对正在运行的应用程序容器执行测试的 Web 应用程序测试容器Run the web application test container which performs tests against the running application container
  5. 如果测试通过,则生成 Helm 图表存档包If the tests pass, build a Helm chart archive package
  6. 使用新的 Helm 图表存档包执行 helm upgradePerform a helm upgrade using the new Helm chart archive package

所有步骤在 Azure 中执行,可将工作卸载到 Azure 的计算资源,并消除基础结构的管理工作。All steps are performed within Azure, offloading the work to Azure's compute resources and freeing you from infrastructure management. 除了 Azure 容器注册表费用以外,只需为所用的资源付费。Besides your Azure container registry, you pay only for the resources you use. 有关定价的信息,请参阅 Azure 容器注册表定价中的“容器生成”部分 。For information on pricing, see the Container Build section in Azure Container Registry pricing.

常见任务方案Common task scenarios

多步骤任务可实现如以下逻辑的方案:Multi-step tasks enable scenarios like the following logic:

  • 以串行或并行方式生成、标记和推送一个或多个容器映像。Build, tag, and push one or more container images, in series or in parallel.
  • 运行单元测试和代码覆盖并捕获其结果。Run and capture unit test and code coverage results.
  • 运行功能测试并捕获其结果。Run and capture functional tests. ACR 任务支持运行多个容器,可在这些容器之间执行一系列请求。ACR Tasks supports running more than one container, executing a series of requests between them.
  • 执行基于任务的执行,包括容器映像生成的前期/后期步骤。Perform task-based execution, including pre/post steps of a container image build.
  • 将包含偏好部署引擎的一个或多个容器部署到目标环境。Deploy one or more containers with your favorite deployment engine to your target environment.

多步骤任务的定义Multi-step task definition

ACR 任务中的多步骤任务定义为 YAML 文件中的一系列步骤。A multi-step task in ACR Tasks is defined as a series of steps within a YAML file. 每个步骤可以指定对成功完成前面一个或多个步骤的依赖性。Each step can specify dependencies on the successful completion of one or more previous steps. 可使用以下任务步骤类型:The following task step types are available:

  • build:使用熟悉的 docker build 语法以串行或并行方式生成一个或多个容器映像。build: Build one or more container images using familiar docker build syntax, in series or in parallel.
  • push:将生成的映像推送到容器注册表。push: Push built images to a container registry. 支持 Azure 容器注册表等专用注册表,并支持公共 Docker 中心。Private registries like Azure Container Registry are supported, as is the public Docker Hub.
  • cmd:运行容器,使其可以在所运行任务的上下文中作为函数运行。cmd: Run a container, such that it can operate as a function within the context of the running task. 可将参数传递到容器的 [ENTRYPOINT],并指定 env、detach 等属性,以及其他熟悉的 docker run 参数。You can pass parameters to the container's [ENTRYPOINT], and specify properties like env, detach, and other familiar docker run parameters. cmd 步骤类型可以实现单元测试和功能测试,并支持并发容器执行。The cmd step type enables unit and functional testing, with concurrent container execution.

以下代码片段演示如何组合使用这些任务步骤类型。The following snippets show how to combine these task step types. 多步骤任务使用类似于以下内容的 YAML 文件可以像从 Dockerfile 构建单个映像并推送到注册表一样简单:Multi-step tasks can be as simple as building a single image from a Dockerfile and pushing to your registry, with a YAML file similar to:

version: v1.1.0
steps:
  - build: -t $Registry/hello-world:$ID .
  - push: ["$Registry/hello-world:$ID"]

或更复杂,例如以下虚构的多步骤定义,其中包括用于生成、测试 helm 包和 helm 部署的步骤(未显示容器注册表和 Helm 存储库配置):Or more complex, such as this fictitious multi-step definition which includes steps for build, test, helm package, and helm deploy (container registry and Helm repository configuration not shown):

version: v1.1.0
steps:
  - id: build-web
    build: -t $Registry/hello-world:$ID .
    when: ["-"]
  - id: build-tests
    build -t $Registry/hello-world-tests ./funcTests
    when: ["-"]
  - id: push
    push: ["$Registry/helloworld:$ID"]
    when: ["build-web", "build-tests"]
  - id: hello-world-web
    cmd: $Registry/helloworld:$ID
  - id: funcTests
    cmd: $Registry/helloworld:$ID
    env: ["host=helloworld:80"]
  - cmd: $Registry/functions/helm package --app-version $ID -d ./helm ./helm/helloworld/
  - cmd: $Registry/functions/helm upgrade helloworld ./helm/helloworld/ --reuse-values --set helloworld.image=$Registry/helloworld:$ID

有关多个方案的多步骤任务 YAML 文件和 Dockerfile,请参阅任务示例See task examples for multi-step task YAML files and Dockerfiles for several scenarios.

运行示例任务Run a sample task

任务既支持手动执行(称作“快速运行”),也支持在提交 Git 或更新基础映像时的自动执行。Tasks support both manual execution, called a "quick run," and automated execution on Git commit or base image update.

若要运行某个任务,请先在 YAML 文件中定义该任务的步骤,然后执行 Azure CLI 命令 az acr runTo run a task, you first define the task's steps in a YAML file, then execute the Azure CLI command az acr run.

以下示例 Azure CLI 命令使用示例任务 YAML 文件运行一个任务。Here's an example Azure CLI command that runs a task using a sample task YAML file. 其任务的步骤生成映像,然后推送该映像。Its steps build and then push an image. 在运行该命令之前,请使用自己的 Azure 容器注册表名称更新 \<acrName\>Update \<acrName\> with the name of your own Azure container registry before running the command.

az acr run --registry <acrName> -f build-push-hello-world.yaml https://github.com/Azure-Samples/acr-tasks.git

运行该任务时,输出应显示 YAML 文件中定义的每个步骤的进度。When you run the task, the output should show the progress of each step defined in the YAML file. 在以下输出中,步骤显示为 acb_step_0acb_step_1In the following output, the steps appear as acb_step_0 and acb_step_1.

az acr run --registry myregistry -f build-push-hello-world.yaml https://github.com/Azure-Samples/acr-tasks.git
Sending context to registry: myregistry...
Queued a run with ID: yd14
Waiting for an agent...
2018/09/12 20:08:44 Using acb_vol_0467fe58-f6ab-4dbd-a022-1bb487366941 as the home volume
2018/09/12 20:08:44 Creating Docker network: acb_default_network
2018/09/12 20:08:44 Successfully set up Docker network: acb_default_network
2018/09/12 20:08:44 Setting up Docker configuration...
2018/09/12 20:08:45 Successfully set up Docker configuration
2018/09/12 20:08:45 Logging in to registry: myregistry.azurecr-test.io
2018/09/12 20:08:46 Successfully logged in
2018/09/12 20:08:46 Executing step: acb_step_0
2018/09/12 20:08:46 Obtaining source code and scanning for dependencies...
2018/09/12 20:08:47 Successfully obtained source code and scanned for dependencies
Sending build context to Docker daemon  109.6kB
Step 1/1 : FROM hello-world
 ---> 4ab4c602aa5e
Successfully built 4ab4c602aa5e
Successfully tagged myregistry.azurecr-test.io/hello-world:yd14
2018/09/12 20:08:48 Executing step: acb_step_1
2018/09/12 20:08:48 Pushing image: myregistry.azurecr-test.io/hello-world:yd14, attempt 1
The push refers to repository [myregistry.azurecr-test.io/hello-world]
428c97da766c: Preparing
428c97da766c: Layer already exists
yd14: digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812 size: 524
2018/09/12 20:08:55 Successfully pushed image: myregistry.azurecr-test.io/hello-world:yd14
2018/09/12 20:08:55 Step id: acb_step_0 marked as successful (elapsed time in seconds: 2.035049)
2018/09/12 20:08:55 Populating digests for step id: acb_step_0...
2018/09/12 20:08:57 Successfully populated digests for step id: acb_step_0
2018/09/12 20:08:57 Step id: acb_step_1 marked as successful (elapsed time in seconds: 6.832391)
The following dependencies were found:
- image:
    registry: myregistry.azurecr-test.io
    repository: hello-world
    tag: yd14
    digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812
  runtime-dependency:
    registry: registry.hub.docker.com
    repository: library/hello-world
    tag: latest
    digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
  git: {}

Run ID: yd14 was successful after 19s

有关提交 Git 或更新基础映像时的自动生成的详细信息,请参阅自动映像生成基础映像更新生成教程文章。For more information about automated builds on Git commit or base image update, see the Automate image builds and Base image update builds tutorial articles.

后续步骤Next steps

可在以下资源中找到多步骤任务的参考信息和示例:You can find multi-step task reference and examples here:

  • 任务参考 - 任务步骤的类型、属性和用法。Task reference - Task step types, their properties, and usage.
  • 任务示例 - 从简单到复杂的多种方案的示例 task.yaml 和 Docker 文件。Task examples - Example task.yaml and Docker files for several scenarios, simple to complex.
  • 命令存储库 - 作为 ACR 任务命令的容器集合。Cmd repo - A collection of containers as commands for ACR tasks.