网络钩子

您可以使用 Webhook 在另一个服务中引发操作,以响应存储库中的推送事件。 Webhook 是发送到您在 Docker Hub 中定义的 URL 的 POST 请求。

创建网络钩子

创建网络钩子:

  1. 在您选择的存储库中,选择Webhooks选项卡。
  2. 提供 Webhook 的名称。
  3. 提供目标 Webhook URL。这是 webhook POST 请求传递的地方。
  4. 选择创建

查看 webhook 传送历史记录

查看 webhook 的历史记录:

  1. 将鼠标悬停在“当前 Webhooks”部分下的 Webhook 上。
  2. 选择菜单选项图标。
  3. 选择查看历史记录

然后,您可以查看传送历史记录,以及传送 POST 请求是否成功。

Webhook 负载示例

Webhook 有效负载具有以下 JSON 格式:

{
  "callback_url": "https://registry.hub.docker.com/u/svendowideit/testhook/hook/2141b5bi5i5b02bec211i4eeih0242eg11000a/",
  "push_data": {
    "pushed_at": 1417566161,
    "pusher": "trustedbuilder",
    "tag": "latest"
  },
  "repository": {
    "comment_count": 0,
    "date_created": 1417494799,
    "description": "",
    "dockerfile": "#\n# BUILD\u0009\u0009docker build -t svendowideit/apt-cacher .\n# RUN\u0009\u0009docker run -d -p 3142:3142 -name apt-cacher-run apt-cacher\n#\n# and then you can run containers with:\n# \u0009\u0009docker run -t -i -rm -e http_proxy http://192.168.1.2:3142/ debian bash\n#\nFROM\u0009\u0009ubuntu\n\n\nVOLUME\u0009\u0009[/var/cache/apt-cacher-ng]\nRUN\u0009\u0009apt-get update ; apt-get install -yq apt-cacher-ng\n\nEXPOSE \u0009\u00093142\nCMD\u0009\u0009chmod 777 /var/cache/apt-cacher-ng ; /etc/init.d/apt-cacher-ng start ; tail -f /var/log/apt-cacher-ng/*\n",
    "full_description": "Docker Hub based automated build from a GitHub repo",
    "is_official": false,
    "is_private": true,
    "is_trusted": true,
    "name": "testhook",
    "namespace": "svendowideit",
    "owner": "svendowideit",
    "repo_name": "svendowideit/testhook",
    "repo_url": "https://registry.hub.docker.com/u/svendowideit/testhook/",
    "star_count": 0,
    "status": "Active"
  }
}

验证 Webhook 回调

要验证 Webhook 链中的回调,您需要:

  1. 检索callback_url请求的 JSON 负载中的值。
  2. 向此 URL 发送包含有效 JSON 正文的 POST 请求。

笔记

仅当最后一个回调得到验证后,链请求才被视为完成。

回调JSON数据

回调数据中可识别以下参数:

  • state(必需):接受的值为successfailureerror。如果状态不是success,则 webhook 链将中断。
  • description:包含 Docker Hub 上提供的各种信息的字符串。最多 255 个字符。
  • context:包含操作上下文的字符串。可以从 Docker Hub 检索。最多 100 个字符。
  • target_url:可以找到操作结果的 URL。可以在 Docker Hub 上检索。

以下是回调负载的示例:

{
  "state": "success",
  "description": "387 tests PASSED",
  "context": "Continuous integration by Acme CI",
  "target_url": "https://ci.acme.com/results/afd339c1c3d27"
}