使用 Azure Container Registry WebhookUsing Azure Container Registry webhooks

Azure Container Registry 會儲存和管理私人 Docker 容器映像,其方式類似於 Docker 中樞儲存公用 Docker 映像。An Azure container registry stores and manages private Docker container images, similar to the way Docker Hub stores public Docker images. 它也可以裝載Helm 圖表的存放庫 (預覽), 這是一種封裝格式, 可將應用程式部署至 Kubernetes。It can also host repositories for Helm charts (preview), a packaging format to deploy applications to Kubernetes. 在其中一個登錄存放庫發生特定動作時,您可以使用 Webhook 來觸發事件。You can use webhooks to trigger events when certain actions take place in one of your registry repositories. Webhook 可以回應登錄層級的事件,或可將範圍縮小至特定的存放庫標記。Webhooks can respond to events at the registry level, or they can be scoped down to a specific repository tag. 使用異地複寫的登錄時, 您可以設定每個 webhook 來回應特定地區複本中的事件。With a geo-replicated registry, you configure each webhook to respond to events in a specific regional replica.

如需 Webhook 要求的詳細資訊,請參閱 Azure Container Registry Webhook 結構描述參考For details on webhook requests, see Azure Container Registry webhook schema reference.

先決條件Prerequisites

建立 webhook-Azure 入口網站Create webhook - Azure portal

  1. 登入 Azure 入口網站Sign in to the Azure portal.
  2. 瀏覽至您要在其中建立 Webhook 的容器登錄。Navigate to the container registry in which you want to create a webhook.
  3. 在 [服務] 下, 選取 [ webhook]。Under Services, select Webhooks.
  4. 選取 [Webhook] 工具列中的 [新增] 。Select Add in the webhook toolbar.
  5. 填入下列資訊以完成 [建立 Webhook] 表單:Complete the Create webhook form with the following information:
Value 描述Description
Webhook 名稱Webhook name 您想要提供給 Webhook 的名稱。The name you want to give to the webhook. 它只能包含字母和數位, 而且長度必須是5-50 個字元。It may contain only letters and numbers, and must be 5-50 characters in length.
LocationLocation 針對異地複寫的登錄, 請指定登錄複本的 Azure 區域。For a geo-replicated registry, specify the Azure region of the registry replica.
服務 URIService URI Webhook 需在當中傳送 POST 通知的 URI。The URI where the webhook should send POST notifications.
自訂標頭Custom headers 您想要與 POST 要求一起傳遞的標頭。Headers you want to pass along with the POST request. 它們應該為「金鑰:值」的格式。They should be in "key: value" format.
觸發程序動作Trigger actions 觸發 Webhook 的動作。Actions that trigger the webhook. 動作包括影像推送、影像刪除、Helm 圖表推送、Helm 圖表刪除和影像隔離。Actions include image push, image delete, Helm chart push, Helm chart delete, and image quarantine. 您可以選擇一或多個動作來觸發 webhook。You can choose one or more actions to trigger the webhook.
狀態Status Webhook 建立之後的狀態。The status for the webhook after it's created. 此選項預設為啟用狀態。It's enabled by default.
ScopeScope Webhook 的運作範圍。The scope at which the webhook works. 如果未指定, 則範圍適用于登錄中的所有事件。If not specified, the scope is for all events in the registry. 您可以針對存放庫或標籤使用 "repository: tag" 格式, 或針對存放庫底下的所有標籤指定 "repository: *"。It can be specified for a repository or a tag by using the format "repository:tag", or "repository:*" for all tags under a repository.

範例 Webhook 表單:Example webhook form:

在 Azure 入口網站中建立 ACR Webhook

建立 webhook-Azure CLICreate webhook - Azure CLI

若要使用 Azure CLI 建立 Webhook,請使用 az acr webhook create 命令。To create a webhook using the Azure CLI, use the az acr webhook create command. 下列命令會針對登錄mycontainerregistry中的所有映射刪除事件建立 webhook:The following command creates a webhook for all image delete events in the registry mycontainerregistry:

az acr webhook create --registry mycontainerregistry --name myacrwebhook01 --actions delete --uri http://webhookuri.com

測試 WebhookTest webhook

Azure 入口網站Azure portal

使用 webhook 之前, 您可以使用 [ Ping ] 按鈕進行測試。Prior to using the webhook, you can test it with the Ping button. Ping 會將泛型 POST 要求傳送至指定的端點,並記錄回應。Ping sends a generic POST request to the specified endpoint and logs the response. 使用 Ping 功能可協助您確認您已正確設定 Webhook。Using the ping feature can help you verify you've correctly configured the webhook.

  1. 選取您想要測試的 Webhook。Select the webhook you want to test.
  2. 在頂端工具列中,選取 [Ping] 。In the top toolbar, select Ping.
  3. 檢查 HTTP STATUS 資料行中的端點回應。Check the endpoint's response in the HTTP STATUS column.

在 Azure 入口網站中建立 ACR Webhook

Azure CLIAzure CLI

若要使用 Azure CLI 測試 ACR Webhook,請使用 az acr webhook ping 命令。To test an ACR webhook with the Azure CLI, use the az acr webhook ping command.

az acr webhook ping --registry mycontainerregistry --name myacrwebhook01

若要查看結果,請使用 az acr webhook list-events 命令。To see the results, use the az acr webhook list-events command.

az acr webhook list-events --registry mycontainerregistry08 --name myacrwebhook01

刪除 WebhookDelete webhook

Azure 入口網站Azure portal

依序選取 Azure 入口網站上的 [Webhook] 和 [刪除] 按鈕,就可以將每個 Webhook 刪除。Each webhook can be deleted by selecting the webhook and then the Delete button in the Azure portal.

Azure CLIAzure CLI

az acr webhook delete --registry mycontainerregistry --name myacrwebhook01

後續步驟Next steps

Webhook 結構描述參考Webhook schema reference

若要深入了解 Azure Container Registry 所發出的 JSON 事件裝載格式與內容,請參閱 Webhook 結構描述參考:For details on the format and properties of the JSON event payloads emitted by Azure Container Registry, see the webhook schema reference:

Azure Container Registry Webhook 結構描述參考Azure Container Registry webhook schema reference

事件方格的事件Event Grid events

除了本文所討論的原生登錄 Webhook 事件,Azure Container Registry 可將事件發送至事件方格:In addition to the native registry webhook events discussed in this article, Azure Container Registry can emit events to Event Grid:

快速入門:將容器登錄事件傳送至事件方格Quickstart: Send container registry events to Event Grid