Webhook Relay
Search…
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:

Create a bucket and configure DockerHub notification

  1. 1.
    Create a bucket here https://my.webhookrelay.com/buckets
  2. 2.
    Once you have it, in the inputs section you will find your public input endpoint, copy it:
  3. 3.
    Add a new DockerHub webhook setting pointing at our public input endpoint (DockerHub docs):

Get a sample of DockerHub webhook

Push a new Docker image:
1
$ docker push karolisr/demo-webhook:latest
2
The push refers to repository [docker.io/karolisr/demo-webhook]
3
48bd38e03c42: Mounted from karolisr/webhook-demo
4
fd9f9fbd5947: Mounted from karolisr/webhook-demo
5
5216338b40a7: Mounted from karolisr/webhook-demo
6
latest: digest: sha256:703f2bab2ce8df0c5ec4e45e26718954b09bf4a625ab831c6556fd27d60f1325 size: 949
Copied!
We should be able to see a new incoming webhook. It looks like this:
1
{
2
"push_data": {
3
"pushed_at": 1582839308,
4
"images": [],
5
"tag": "latest",
6
"pusher": "karolisr"
7
},
8
"callback_url": "https://registry.hub.docker.com/u/karolisr/demo-webhook/hook/242ii4ddc2jji4a0cc44fbcdbcdecj1ab/",
9
"repository": {
10
"status": "Active",
11
"description": "",
12
"is_trusted": false,
13
"full_description": "",
14
"repo_url": "https://hub.docker.com/r/karolisr/demo-webhook",
15
"owner": "karolisr",
16
"is_official": false,
17
"is_private": true,
18
"name": "demo-webhook",
19
"namespace": "karolisr",
20
"star_count": 0,
21
"comment_count": 0,
22
"date_created": 1524557040,
23
"repo_name": "karolisr/demo-webhook"
24
}
25
}
Copied!

Create a Function to transform the 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):
1
local json = require("json")
2
3
local body, err = json.decode(r.RequestBody)
4
if err then error(err) end
5
6
local message = "New image pushed at: " .. body["repository"]["repo_name"] .. ":" .. body["push_data"]["tag"]
7
8
-- Preparing Slack payload
9
local slack = {
10
response_type= "in_channel",
11
text= message}
12
13
local result, err = json.encode(slack)
14
15
-- Set request header to application/json
16
r:SetRequestHeader("Content-Type", "application/json")
17
-- Set request method to POST
18
r:SetRequestMethod("POST")
19
-- Set modified request body
20
r:SetRequestBody(result)
Copied!
Click “Save” and then try testing it with the “Send” button:
Function invoke example

Connect everything together

  1. 1.
    Navigate to https://api.slack.com/messaging/webhooks and click “Create your Slack app”. Select your workspace, enter a name that you will remember.
  2. 2.
    Create a new incoming webhook configuration, copy “Webhook URL” (it starts with https://hooks.slack.com/services/T3...), we will need to supply it to Webhook Relay.
  3. 3.
    Open your bucket details (via https://my.webhookrelay.com/buckets)
  4. 4.
    Open “OUTPUT DESTINATIONS” tab and create a new output called “Slack” with the Slack URL from step 2:
  5. 5.
    Once created, click on the “code” symbol and from the dropdown select dockerhub_to_slack function:
Push new image to DockerHub, you should see a new notification in your Slack channel:
Slack channel msg
That’s it, feel free to continue modifying Lua function to include pusher’s name and message format. Following this process you can transform any webhook into any other webhook.
Have fun!