共用方式為


ACR 工作參考:YAML

ACR 工作中的多步驟工作定義提供以容器為中心的計算基本類型,著重於建置、測試和修補容器。 本文涵蓋定義多重步驟工作的 YAML 檔案命令、參數、屬性和語法。

本文包含針對 ACR 工作建立多重步驟工作 YAML 檔案的參考。 如果您想要 ACR 工作的簡介,請參閱 ACR 工作概觀

acr-task.yaml 檔格式

ACR 工作支持標準 YAML 語法中的多步驟工作宣告。 您可以在 YAML 檔案中定義工作的步驟。 接著,您可以將檔案傳遞至 az acr run 命令,以手動執行工作。 或者,使用 檔案來建立具有 az acr task create 的工作,該工作 會在 Git 認可、基底映射更新或排程上自動觸發。 雖然本文指的是 acr-task.yaml 包含步驟的檔案,但 ACR 工作支援任何具有 支持擴展名的有效檔名。

最上層acr-task.yaml基本類型是工作屬性步驟類型和步驟屬性

  • 工作屬性 會套用至整個工作執行的所有步驟。 有數個全域工作屬性,包括:
    • version
    • stepTimeout
    • workingDirectory
  • 工作步驟類型 代表可在工作中執行的動作類型。 有三種步驟類型:
    • build
    • push
    • cmd
  • 工作步驟屬性 是套用至個別步驟的參數。 有數個步驟屬性,包括:
    • startDelay
    • timeout
    • when
    • ...還有更多。

檔案的 acr-task.yaml 基底格式,包括一些常見的步驟屬性,如下所示。 雖然不是所有可用步驟屬性或步驟類型用法的完整表示法,但它提供基本檔格式的快速概觀。

version: # acr-task.yaml format version.
stepTimeout: # Seconds each step may take.
steps: # A collection of image or container actions.
  - build: # Equivalent to "docker build," but in a multi-tenant environment
  - push: # Push a newly built or retagged image to a registry.
    when: # Step property that defines either parallel or dependent step execution.
  - cmd: # Executes a container, supports specifying an [ENTRYPOINT] and parameters.
    startDelay: # Step property that specifies the number of seconds to wait before starting execution.

支援的工作擴展名

ACR 工作已保留數個擴展名,包括 .yaml,它會以工作檔案的形式處理。 ACR 工作不會在下列清單中的任何擴展名視為 Dockerfile:.yaml、.yml、.toml、.json、.sh、.bash、.zsh、.ps1、.ps、.cmd、.bat、.ts、.js、.php、.py、.rb、.lua

YAML 是 ACR 工作目前唯一支援的檔案格式。 其他擴展名會保留給未來的可能支援。

執行範例工作

本文下列各節會參考數個範例工作檔案。 範例工作位於公用 GitHub 存放庫 Azure-Samples/acr-tasks 中。 您可以使用 Azure CLI 命令 az acr run 來執行它們。 範例命令類似:

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

範例命令的格式假設您已在 Azure CLI 中設定預設登錄,因此會省略 --registry 參數。 若要設定預設登錄,請使用 az config 命令搭配 set 命令,以接受 defaults.acr=REGISTRY_NAME 機碼值組。

例如,若要使用名為 「myregistry」 的預設登錄來設定 Azure CLI:

az config set defaults.acr=myregistry

工作屬性

工作屬性通常會出現在檔案頂 acr-task.yaml 端,而且是整個工作步驟完整執行時套用的全域屬性。 某些全域屬性可以在個別步驟內覆寫。

屬性 型別 選擇性 描述 支援覆寫 預設值
version 字串 Yes ACR 工作服務所剖析的 acr-task.yaml 檔案版本。 雖然 ACR 工作會努力維持回溯相容性,但此值可讓 ACR 工作在定義的版本中維持相容性。 如果未指定,則預設為最新版本。 No
stepTimeout int (秒) Yes 步驟可以執行的秒數上限。 stepTimeout如果在工作上指定 屬性,它會設定所有步驟的默認timeout屬性。 如果在步驟上指定 屬性 timeout ,它會覆寫 stepTimeout 工作所提供的屬性。

工作之步驟逾時值的總和應等於工作之 run timeout 屬性的值(例如,藉由傳遞 --timeoutaz acr task create 命令來設定)。 如果工作的執行 timeout 值較小,則會優先使用。
Yes 600 (10 分鐘)
workingDirectory 字串 Yes 運行時間期間容器的工作目錄。 如果在工作上指定 屬性,它會設定所有步驟的默認 workingDirectory 屬性。 如果在步驟上指定,它會覆寫工作所提供的屬性。 Yes c:\workspace 在 Windows 或 /workspace Linux 中
env [string, string, ...] Yes 格式的 key=value 字串數位,定義工作的環境變數。 如果在工作上指定 屬性,它會設定所有步驟的默認 env 屬性。 如果在步驟上指定,它會覆寫繼承自工作的任何環境變數。 Yes
secrets [secret, secret, ...] Yes 秘密物件的陣列。 No
networks [network, network, ...] Yes 網路物件的陣列。 No
volumes [volume, volume, ...] Yes 磁碟區對象的陣列。 指定要掛接至步驟之來源內容的磁碟區。 No

secret

秘密物件具有下列屬性。

屬性 型別 選擇性 描述 預設值
id 字串 No 秘密的標識碼。
keyvault 字串 Yes Azure 金鑰保存庫 秘密 URL。
clientID 字串 Yes Azure 資源使用者指派受控識別的 用戶端標識碼

network

網路物件具有下列屬性。

屬性 型別 選擇性 描述 預設值
name 字串 No 網路的名稱。
driver 字串 Yes 管理網路的驅動程式。
ipv6 bool Yes 是否啟用 IPv6 網路功能。 false
skipCreation bool Yes 是否略過網路建立。 false
isDefault bool Yes 網路是否為 Azure Container Registry 所提供的預設網路。 false

磁碟區

磁碟區物件具有下列屬性。

屬性 型別 選擇性 描述 預設值
name 字串 No 要掛接的磁碟區名稱。 只能包含英數位元、『-』和 『_』。
secret map[string]string No 對應的每個索引鍵都是在磁碟區中建立並填入的檔案名稱。 每個值都是秘密的字串版本。 秘密值必須是Base64編碼。

工作步驟類型

ACR 工作支援三種步驟類型。 每個步驟類型都支援數個屬性,詳述於每個步驟類型的 區段中。

步驟類型 描述
build 使用熟悉 docker build 的語法建置容器映像。
push docker push執行新建置或重新標記的映像至容器登錄。 支援 Azure Container Registry、其他私人登錄和公用 Docker Hub。
cmd 以命令的形式執行容器,並將參數傳遞至容器的 [ENTRYPOINT]。 步驟cmd類型支援、 detach和其他熟悉docker run的命令選項等env參數,以並行容器執行來啟用單元和功能測試。

build

建置容器映像。 步驟 build 類型代表多租使用者的安全方式,以一流的基本類型在雲端中執行 docker build

語法:build

version: v1.1.0
steps:
  - [build]: -t [imageName]:[tag] -f [Dockerfile] [context]
    [property]: [value]

執行 az acr run命令以取得 docker 版本。

az acr run -r $ACR_NAME --cmd "docker version" /dev/null

在 yaml 檔案中新增環境變數 DOCKER_BUILDKIT=1 ,以啟用 buildkit 和使用 secretbuildkit

步驟 build 類型支援下表中的參數。 步驟build類型也支援 docker build 命令的所有建置選項,例如--build-arg設定建置時間變數。

參數 描述 選擇性
-t | --image 定義建置映像的完整 image:tag

由於映像可用於內部工作驗證,例如功能測試,並非所有映像都需要 push 登錄。 不過,若要在工作執行中實例映射,映像需要名稱才能參考。

不同於 az acr build,執行 ACR 工作不提供預設推送行為。 使用 ACR 工作時,預設案例會假設能夠建置、驗證,然後推送映射。 請參閱 推送 以瞭解如何選擇性地推送建置的映像。
Yes
-f | --file 指定傳遞至 docker build的 Dockerfile。 如果未指定,則會假設內容根目錄中的預設 Dockerfile。 若要指定 Dockerfile,請傳遞相對於內容根目錄的檔名。 Yes
context 傳遞至 docker build的根目錄。 每個工作的根目錄都會設定為共用 的 workingDirectory,並包含相關聯 Git 複製目錄的根目錄。 No

屬性:組建

步驟 build 類型支援下列屬性。 在本文的 [ 工作步驟屬性] 區段中尋找這些屬性 的詳細數據。

屬性 類型 必要
detach bool 選擇性
disableWorkingDirectoryOverride bool 選擇性
entryPoint 字串 選擇性
env [string, string, ...] 選擇性
expose [string, string, ...] 選擇性
id 字串 選擇性
ignoreErrors bool 選擇性
isolation 字串 選擇性
keep bool 選擇性
network object 選擇性
ports [string, string, ...] 選擇性
pull bool 選擇性
repeat int 選擇性
retries int 選擇性
retryDelay int (秒) 選擇性
secret object 選擇性
startDelay int (秒) 選擇性
timeout int (秒) 選擇性
volumeMount object 選擇性
when [string, string, ...] 選擇性
workingDirectory 字串 選擇性

範例:build

建置映射 - 根目錄中的內容

az acr run -f build-hello-world.yaml https://github.com/AzureCR/acr-tasks-sample.git
version: v1.1.0
steps:
  - build: -t $Registry/hello-world -f hello-world.dockerfile .

建置映射 - 子目錄中的內容

version: v1.1.0
steps:
  - build: -t $Registry/hello-world -f hello-world.dockerfile ./subDirectory

傳入 ACR 工作的動態變數

使用 Azure Container Registry (ACR) 工作時,您可能會發現自己需要將不同的值傳遞至建置程式,而不需使用 --set 旗標搭配 az acr task run 命令來變更工作定義。

範例:在運行時間設定映像標記

假設您在檔案中 acr-task.yml 定義了具有影像標籤的 ACR 工作:

steps:
  - build: -t $Registry/hello-world:{{.Values.tag}}

您可以使用下列 Azure CLI 命令,在執行時間觸發工作並將變數設定 tagv2

az acr task run --registry myregistry --name mytask --set tag=v2

此命令會啟動名為 mytask 的 ACR 工作,並使用 v2 標記來建置映像,並覆寫檔案中的 acr-task.yml 佔位元。

這種方法可讓您在 CI/CD 管線中進行自定義,讓您根據目前的需求動態調整參數,而不需要改變工作定義。

push

將一或多個建置或重新標記的映像推送至容器登錄。 支援推送至 Azure Container Registry 之類的私人登錄,或推送至公用 Docker Hub。

語法:push

步驟 push 類型支援影像集合。 YAML 集合語法支援內嵌和巢狀格式。 推送單一映像通常會使用內嵌語法來表示:

version: v1.1.0
steps:
  # Inline YAML collection syntax
  - push: ["$Registry/hello-world:$ID"]

若要提高可讀性,請在推送多個影像時使用巢狀語法:

version: v1.1.0
steps:
  # Nested YAML collection syntax
  - push:
    - $Registry/hello-world:$ID
    - $Registry/hello-world:latest

屬性:推送

步驟 push 類型支援下列屬性。 在本文的 [ 工作步驟屬性] 區段中尋找這些屬性 的詳細數據。

屬性 型別 必要
env [string, string, ...] 選擇性
id 字串 選擇性
ignoreErrors bool 選擇性
startDelay int (秒) 選擇性
timeout int (秒) 選擇性
when [string, string, ...] 選擇性

範例:push

推送多個映像

az acr run -f build-push-hello-world.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
  - build: -t $Registry/hello-world:$ID -f hello-world.dockerfile .
  - push: 
    - $Registry/hello-world:$ID

建置、推送及執行

az acr run -f build-run-hello-world.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
  - build: -t $Registry/hello-world:$ID -f hello-world.dockerfile .
  - push:
    - $Registry/hello-world:$ID
  - cmd: $Registry/hello-world:$ID

cmd

步驟類型會 cmd 執行容器。

語法:cmd

version: v1.1.0
steps:
  - [cmd]: [containerImage]:[tag (optional)] [cmdParameters to the image]

屬性:cmd

步驟 cmd 類型支援下列屬性:

屬性 型別 必要
detach bool 選擇性
disableWorkingDirectoryOverride bool 選擇性
entryPoint 字串 選擇性
env [string, string, ...] 選擇性
expose [string, string, ...] 選擇性
id 字串 選擇性
ignoreErrors bool 選擇性
isolation 字串 選擇性
keep bool 選擇性
network object 選擇性
ports [string, string, ...] 選擇性
pull bool 選擇性
repeat int 選擇性
retries int 選擇性
retryDelay int (秒) 選擇性
secret object 選擇性
startDelay int (秒) 選擇性
timeout int (秒) 選擇性
volumeMount object 選擇性
when [string, string, ...] 選擇性
workingDirectory 字串 選擇性

您可以在本文的 [工作步驟屬性] 區段中找到這些屬性的詳細數據。

範例:cmd

執行 hello-world 映射

此命令會執行工作 hello-world.yaml 檔案,該檔案會參考 Docker Hub 上的 hello-world 映射。

az acr run -f hello-world.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
  - cmd: mcr.microsoft.com/hello-world

執行bash映像並回應 「hello world」

此命令會執行工作 bash-echo.yaml 檔案,該檔案會參考 Docker Hub 上的 bash 映像。

az acr run -f bash-echo.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
  - cmd: bash echo hello world

執行特定的Bash映像標籤

若要執行特定映像版本,請在 中 cmd指定標記。

此命令會執行工作 bash-echo-3.yaml 檔案,該檔案會參考 Docker Hub 上的 bash:3.0 映射。

az acr run -f bash-echo-3.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
  - cmd: bash:3.0 echo hello world

執行自定義映像

步驟 cmd 類型會使用標準 docker run 格式參考影像。 未以登錄開頭的映像會假設源自 docker.io。 上述範例同樣可以表示為:

version: v1.1.0
steps:
  - cmd: docker.io/bash:3.0 echo hello world

藉由使用標準 docker run 映像參考慣例, cmd 可以從任何私人登錄或公用 Docker Hub 執行映像。 如果您在 ACR 工作執行所在的相同登錄中參考映像,則不需要指定任何登錄認證。

  • 執行來自 Azure 容器登錄的映像。 下列範例假設您有名為 myregistry的登錄和自定義映像 myimage:mytag

    version: v1.1.0
    steps:
        - cmd: myregistry.azurecr.io/myimage:mytag
    
  • 使用 Run 變數或別名將登錄參考一般化

    您可以使用 Run 變數或別名,而不是在檔案中acr-task.yaml硬式編碼您的登錄名稱,使其更容易移植。 變數或$Registry別名會在Run.Registry運行時間擴充為工作執行所在的登錄名稱。

    例如,若要將上述工作一般化,讓它在任何 Azure 容器登錄中運作,請參考映像名稱中的 $Registry 變數:

    version: v1.1.0
    steps:
      - cmd: $Registry/myimage:mytag
    

存取秘密磁碟區

屬性 volumes 允許針對 build 工作中的 和 cmd 步驟指定磁碟區及其秘密內容。 在每個步驟內,選擇性 volumeMounts 屬性會列出磁碟區和對應的容器路徑,以在該步驟中掛接至容器。 秘密會以每個磁碟區掛接路徑的檔案的形式提供。

執行工作,並將兩個秘密掛接至步驟:一個儲存在密鑰保存庫中,另一個在命令行上指定:

az acr run -f mounts-secrets.yaml --set-secret mysecret=abcdefg123456 https://github.com/Azure-Samples/acr-tasks.git
# This template demonstrates mounting a custom volume into a container at a CMD step
secrets:
  - id: sampleSecret
    keyvault: https://myacbvault2.vault.azure.net/secrets/SampleSecret # Replace with valid keyvault with access

volumes:
  - name: mysecrets
    secret:
      mysecret1: {{.Secrets.sampleSecret | b64enc}}
      mysecret2: {{.Values.mysecret | b64enc}}

steps:
  - cmd: bash cat /run/test/mysecret1 /run/test/mysecret2
    volumeMounts:
      - name: mysecrets
        mountPath: /run/test

工作步驟屬性

每個步驟類型都支援數個適合其類型的屬性。 下表定義所有可用的步驟屬性。 並非所有步驟類型都支援所有屬性。 若要查看每個步驟類型可用的這些屬性,請參閱 Cmd置和 推送 步驟類型參考小節。

屬性 型別 選擇性 描述 預設值
detach bool Yes 執行時,是否應該卸離容器。 false
disableWorkingDirectoryOverride bool Yes 是否要停用 workingDirectory 覆寫功能。 搭配使用此選項 workingDirectory ,以完全控制容器的工作目錄。 false
entryPoint 字串 Yes [ENTRYPOINT] 寫步驟容器的 。
env [string, string, ...] Yes 格式的 key=value 字串數位,定義步驟的環境變數。
expose [string, string, ...] Yes 從容器公開的埠陣列。
id 字串 Yes 可唯一識別工作內的步驟。 工作中的其他步驟可以參考步驟的 id,例如使用 when進行相依性檢查。

id也是執行中容器的名稱。 例如,在工作中的其他容器中執行的進程可以參考 id 作為其 DNS 主機名,或使用 Docker 記錄 [id] 存取它。
acb_step_%d,其中 %d 是 YAML 檔案中下拉式步驟的 0 型索引
ignoreErrors bool Yes 不論容器執行期間是否發生錯誤,是否要將步驟標示為成功。 false
isolation 字串 Yes 容器的隔離等級。 default
keep bool Yes 是否應在執行後保留步驟的容器。 false
network object Yes 識別容器執行所在的網路。
ports [string, string, ...] Yes 從容器發行至主機的埠陣列。
pull bool Yes 是否在執行容器之前強制提取容器,以防止任何快取行為。 false
privileged bool Yes 是否要在特殊許可權模式中執行容器。 false
repeat int Yes 重複執行容器的重試次數。 0
retries int Yes 如果容器執行失敗,重試次數。 只有在容器的結束代碼不是零時,才會嘗試重試。 0
retryDelay int (秒) Yes 重試容器執行之間的延遲,以秒為單位。 0
secret object Yes 識別 Azure 資源的 Azure 金鑰保存庫 秘密或受控識別。
startDelay int (秒) Yes 延遲容器執行的秒數。 0
timeout int (秒) Yes 在終止之前,步驟可以執行的秒數上限。 600
when [string, string, ...] Yes 設定步驟對工作內一或多個其他步驟的相依性。
user 字串 Yes 容器的用戶名稱或 UID
workingDirectory 字串 Yes 設定步驟的工作目錄。 根據預設,ACR 工作會建立根目錄作為工作目錄。 不過,如果您的組建有數個步驟,則先前的步驟可以藉由指定相同的工作目錄,與後續步驟共用成品。 c:\workspace 在 Windows 或 /workspace Linux 中

volumeMount

volumeMount 物件具有下列屬性。

屬性 型別 選擇性 描述 預設值
name 字串 No 要掛接的磁碟區名稱。 必須完全符合屬性 volumes 的名稱。
mountPath 字串 在容器中掛接檔案的絕對路徑。

範例:工作步驟屬性

範例:id

建置兩個映像,實例化功能測試映像。 每個步驟都是由其 when 屬性中工作參考中其他步驟的唯id一識別。

az acr run -f when-parallel-dependent.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
  # build website and func-test images, concurrently
  - id: build-hello-world
    build: -t $Registry/hello-world:$ID -f hello-world.dockerfile .
    when: ["-"]
  - id: build-hello-world-test
    build: -t hello-world-test -f hello-world.dockerfile .
    when: ["-"]
  # run built images to be tested
  - id: hello-world
    cmd: $Registry/hello-world:$ID
    when: ["build-hello-world"]
  - id: func-tests
    cmd: hello-world-test
    env:
      - TEST_TARGET_URL=hello-world
    when: ["hello-world"]
  # push hello-world if func-tests are successful  
  - push: ["$Registry/hello-world:$ID"]
    when: ["func-tests"]

範例:時機

屬性 when 會指定步驟對工作內其他步驟的相依性。 它支援兩個參數值:

  • when: ["-"] - 表示不相依於其他步驟。 指定 when: ["-"] 的步驟會立即開始執行,並啟用並行步驟執行。
  • when: ["id1", "id2"] - 表示步驟相依於具有 id 「id1」 和 id 「id2」 的步驟。 在完成 「id1」 和 「id2」 步驟之前,將不會執行此步驟。

如果未 when 在步驟中指定,該步驟會相依於檔案中 acr-task.yaml 上一個步驟的完成。

沒有的循序步驟執行 when

az acr run -f when-sequential-default.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
    - cmd: bash echo one
    - cmd: bash echo two
    - cmd: bash echo three

使用 when執行循序步驟:

az acr run -f when-sequential-id.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
    - id: step1
      cmd: bash echo one
    - id: step2
      cmd: bash echo two
      when: ["step1"]
    - id: step3
      cmd: bash echo three
      when: ["step2"]

平行映射建置:

az acr run -f when-parallel.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
  # build website and func-test images, concurrently
  - id: build-hello-world
    build: -t $Registry/hello-world:$ID -f hello-world.dockerfile .
    when: ["-"]
  - id: build-hello-world-test
    build: -t hello-world-test -f hello-world.dockerfile .
    when: ["-"]

平行映像建置和相依測試:

az acr run -f when-parallel-dependent.yaml https://github.com/Azure-Samples/acr-tasks.git
version: v1.1.0
steps:
  # build website and func-test images, concurrently
  - id: build-hello-world
    build: -t $Registry/hello-world:$ID -f hello-world.dockerfile .
    when: ["-"]
  - id: build-hello-world-test
    build: -t hello-world-test -f hello-world.dockerfile .
    when: ["-"]
  # run built images to be tested
  - id: hello-world
    cmd: $Registry/hello-world:$ID
    when: ["build-hello-world"]
  - id: func-tests
    cmd: hello-world-test
    env:
      - TEST_TARGET_URL=hello-world
    when: ["hello-world"]
  # push hello-world if func-tests are successful  
  - push: ["$Registry/hello-world:$ID"]
    when: ["func-tests"]

執行變數

ACR 工作包含一組預設變數,可在工作步驟執行時使用。 您可以使用 格式 {{.Run.VariableName}}來存取這些變數,其中 VariableName 是下列其中一項:

  • Run.ID
  • Run.SharedVolume
  • Run.Registry
  • Run.RegistryName
  • Run.Date
  • Run.OS
  • Run.Architecture
  • Run.Commit
  • Run.Branch
  • Run.TaskName

變數名稱通常是自我解釋的。 常用的變數會遵循詳細數據。 從 YAML 版本 v1.1.0起,您可以使用縮寫、預先定義的工作 別名 來取代大部分的執行變數。 例如,取代 {{.Run.Registry}},請使用 $Registry 別名。

Run.ID

每個執行、透過 az acr run或觸發程式執行透過 az acr task create建立的工作,都有唯一標識符。 標識子代表目前正在執行的執行。

通常用於唯一標記影像:

version: v1.1.0
steps:
    - build: -t $Registry/hello-world:$ID .

Run.SharedVolume

所有工作步驟可存取之共用磁碟區的唯一標識符。 磁碟區會掛接至 c:\workspace Windows 或 /workspace Linux 中。

Run.Registry

登錄的完整伺服器名稱。 通常用來一般參考執行工作所在的登錄。

version: v1.1.0
steps:
  - build: -t $Registry/hello-world:$ID .

Run.RegistryName

容器登錄的名稱。 通常用於不需要完整伺服器名稱的工作步驟中,例如, cmd 在登錄上執行 Azure CLI 命令的步驟。

version 1.1.0
steps:
# List repositories in registry
- cmd: az login --identity
- cmd: az acr repository list --name $RegistryName

Run.Date

執行開始的目前 UTC 時間。

Run.Commit

對於認可至 GitHub 存放庫所觸發的工作,則為認可標識碼。

Run.Branch

針對認可至 GitHub 存放庫所觸發的工作,分支名稱。

別名

從 開始 v1.1.0,ACR 工作支援工作步驟執行時可用的別名。 別名的概念類似於bash和其他一些命令殼層中支援的別名(命令快捷方式)。

使用別名,您可以輸入單一單字來啟動任何命令或命令群組(包括選項和檔名)。

ACR 工作支援數個預先定義的別名,也支援您建立的自定義別名。

預先定義的別名

下列工作別名可用來取代 執行變數

Alias 執行變數
ID Run.ID
SharedVolume Run.SharedVolume
Registry Run.Registry
RegistryName Run.RegistryName
Date Run.Date
OS Run.OS
Architecture Run.Architecture
Commit Run.Commit
Branch Run.Branch

在工作步驟中,在別名前面加上 $ 指示詞,如下列範例所示:

version: v1.1.0
steps:
  - build: -t $Registry/hello-world:$ID -f hello-world.dockerfile .

影像別名

下列每個別名都會指向 Microsoft Container Registry (MCR) 中的穩定映射。 您可以在 Task 檔案的 cmd 區段中參考它們,而不使用 指示詞。

Alias 映像
acr mcr.microsoft.com/acr/acr-cli:0.5
az mcr.microsoft.com/acr/azure-cli:7ee1d7f
bash mcr.microsoft.com/acr/bash:7ee1d7f
curl mcr.microsoft.com/acr/curl:7ee1d7f

下列範例工作會使用數個別名來 清除 執行登錄中存放庫中 samples/hello-world 超過 7 天的映像標記:

version: v1.1.0
steps:
  - cmd: acr tag list --registry $RegistryName --repository samples/hello-world
  - cmd: acr purge --registry $RegistryName --filter samples/hello-world:.* --ago 7d

自定義別名

在您的 YAML 檔案中定義自定義別名,並使用它,如下列範例所示。 別名只能包含英數位元。 展開別名的預設指示詞是 $ 字元。

version: v1.1.0
alias:
  values:
    repo: myrepo
steps:
  - build: -t $Registry/$repo/hello-world:$ID -f Dockerfile .

您可以連結至自定義別名定義的遠端或本機 YAML 檔案。 下列範例會連結至 Azure Blob 記憶體中的 YAML 檔案:

version: v1.1.0
alias:
  src:  # link to local or remote custom alias files
    - 'https://link/to/blob/remoteAliases.yml?readSasToken'
[...]

下一步

如需多步驟工作的概觀,請參閱 在 ACR 工作中執行多步驟建置、測試及修補工作

如需單一步驟組建,請參閱 ACR 工作概觀