執行 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.

重要

如果您先前已在預覽期間使用 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 Container Registry 定價中的容器組建一節。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 Container Registry 之類的私人登錄,也支援公用 Docker Hub。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.0.0
steps:
  - build: -t {{.Run.Registry}}/hello-world:{{.Run.ID}} .
  - push: ["{{.Run.Registry}}/hello-world:{{.Run.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.0.0
steps:
  - id: build-web
    build: -t {{.Run.Registry}}/hello-world:{{.Run.ID}} .
    when: ["-"]
  - id: build-tests
    build -t {{.Run.Registry}}/hello-world-tests ./funcTests
    when: ["-"]
  - id: push
    push: ["{{.Run.Registry}}/helloworld:{{.Run.ID}}"]
    when: ["build-web", "build-tests"]
  - id: hello-world-web
    cmd: {{.Run.Registry}}/helloworld:{{.Run.ID}}
  - id: funcTests
    cmd: {{.Run.Registry}}/helloworld:{{.Run.ID}}
    env: ["host=helloworld:80"]
  - cmd: {{.Run.Registry}}/functions/helm package --app-version {{.Run.ID}} -d ./helm ./helm/helloworld/
  - cmd: {{.Run.Registry}}/functions/helm upgrade helloworld ./helm/helloworld/ --reuse-values --set helloworld.image={{.Run.Registry}}/helloworld:{{.Run.ID}}

如需數個案例, 請參閱工作範例, 以取得完整的多步驟工作 YAML 檔案和 dockerfile。See task examples for complete 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.

以下是一個使用範例工作 YAML 檔案來執行工作的範例 Azure CLI 命令。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于數個案例的範例檔案, 簡單到複雜。Task examples - Example task.yaml files for several scenarios, simple to complex.
  • Cmd 存放庫 - 作為 ACR 工作命令的容器集合。Cmd repo - A collection of containers as commands for ACR tasks.