Docker Private Registry Notifications

Docker Private Registry Notifications

之前写过三篇文章介绍公司 CI/CD 流程,基本功能完成,但是还有一个问题是除了使阿里云官方的镜像仓库外,我们还有私有的镜像仓库,这部分想要触发自动部署,也需要有 Webhook 的功能。

当然,最主要的原因是:

每一个 Project 都需要写脚本通知,写 sh 脚本处理贼蛋疼!?
调整到镜像仓库级别后,完全可以抛弃 Jenkins 插件部署,自由度更高。

一,仓库配置

version: 0.1
log:
  fields:
    service: registry
storage:
  cache:
    blobdescriptor: inmemory
  filesystem:
    rootdirectory: /var/lib/registry
http:
  addr: :5000
  headers:
    X-Content-Type-Options: [nosniff]
health:
  storagedriver:
    enabled: true
    interval: 10s
    threshold: 3    
notifications:
  endpoints:
    - name: console-egg
      disabled: false
      url: 'http://192.168.5.48:7001/hook/docker/registry'      
      headers:
        X-Token: ['Suyi']
      timeout: 10s
      threshold: 10
      backoff: 1s

二,Webhook

Web 框架我是用 Egg.js 的,那么这里就有一个问题了,通知用了自定义的 Content-Type:

Content-Type: application/vnd.docker.distribution.events.v1+json 

查文档可知 Egg.js 使用 koa-bodypraser,所以配置里要加一项:

# config.default.js

exports.bodyParser = {
  extendTypes: {
    json: [ 'application/vnd.docker.distribution.events.v1+json' ],
  },
};

然后就可以看到通知:

{
  "events":[
    {
      "id": "a6089916-c2b3-4103-aa2d-40db93f10ee2",
      "timestamp": "2017-10-13T02:58:17.285607811Z",
      "action": "push",
      "target": {
        "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
        "size": 1569,
        "digest": "sha256:ddf13beebac2a486e5f9a7fe77fbcc3a20e3d632c006961430d4cd2271aefde2",
        "length": 1569,
        "repository": "arashivision/redis",
        "url": "http://localhost:5000/v2/arashivision/redis/manifests/sha256:ddf13beebac2a486e5f9a7fe77fbcc3a20e3d632c006961430d4cd2271aefde2",
        "tag": "3.2-alpine"
      },
      "request": {
        "id": "8cb085ed-fcaa-481a-bd10-3ed491761641",
        "addr": "172.17.0.1:52832",
        "host": "localhost:5000",
        "method": "PUT",
        "useragent": "docker/17.09.0-cego/go1.8.3 git-commit/afdb6d4 kernel/4.9.49-moby os/linux arch/amd64 UpstreamClient(Docker-Client/17.09.0-ce \(darwin\))"
      },
      "actor": {

      },
      "source": {
        "addr": "9e566ba1324c:5000",
        "instanceID": "1f0100e8-89de-42f8-b9bf-534b6c7fce23"
      }
    }
  ]
}

接下来就没什么好说的,按需要检测 action 类型即可。

三,参考

编辑于 2017-10-13

文章被以下专栏收录