Convert DockerHub webhook to Slack notification
A short example on integrating two services directly by utilizing Functions.
Many Docker registries provide a way to notify team in chat channels when new images are pushed (if you are waiting for a build complete). Let’s add this capability to the official DockerHub registry! :)
Prerequisites:
- Docker
- 1.
- 2.Once you have it, in the inputs section you will find your public input endpoint, copy it:
- 3.
Push a new Docker image:
$ docker push karolisr/demo-webhook:latest
The push refers to repository [docker.io/karolisr/demo-webhook]
48bd38e03c42: Mounted from karolisr/webhook-demo
fd9f9fbd5947: Mounted from karolisr/webhook-demo
5216338b40a7: Mounted from karolisr/webhook-demo
latest: digest: sha256:703f2bab2ce8df0c5ec4e45e26718954b09bf4a625ab831c6556fd27d60f1325 size: 949
We should be able to see a new incoming webhook. It looks like this:
{
"push_data": {
"pushed_at": 1582839308,
"images": [],
"tag": "latest",
"pusher": "karolisr"
},
"callback_url": "https://registry.hub.docker.com/u/karolisr/demo-webhook/hook/242ii4ddc2jji4a0cc44fbcdbcdecj1ab/",
"repository": {
"status": "Active",
"description": "",
"is_trusted": false,
"full_description": "",
"repo_url": "https://hub.docker.com/r/karolisr/demo-webhook",
"owner": "karolisr",
"is_official": false,
"is_private": true,
"name": "demo-webhook",
"namespace": "karolisr",
"star_count": 0,
"comment_count": 0,
"date_created": 1524557040,
"repo_name": "karolisr/demo-webhook"
}
}
Go to the Functions page and click on a “Create Function” button. Enter a name, for example “dockerhub-to-slack” and click “Submit”.
You can now copy/paste webhook payload into the “request body” area for later tests. In the code editor let’s add a function to get repository name and prepare a Slack webhook payload (currently functions have to be written in Lua but more examples for WebAssembly will be added soon):
local json = require("json")
local body, err = json.decode(r.RequestBody)
if err then error(err) end