Serverless webhook transformation (DockerHub to Slack)
Karolis
Posted on February 28, 2020
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! :)
We will use newly released Webhook Relay Functions to transform webhooks while they are passing through the service.
Prerequisites:
Create a bucket and configure DockerHub notification
- Create a bucket here https://my.webhookrelay.com/buckets
-
Once you have it, in the inputs section you will find your public input endpoint, copy it:
-
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:
$ 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"
}
}
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".
For this example we are using Lua language which provides an easy way to parse, transform payloads. Webhook Relay Functions also support WebAssembly modules, however they are currently under development and can only be added/updated using relay CLI.
You can now copy/paste webhook payload into the "request body" area for later tests. In the code editor let's add a Lua function to get repository name and prepare a Slack webhook payload:
local json = require("json")
local body, err = json.decode(r.RequestBody)
if err then error(err) end
local message = "New image pushed at: " .. body["repository"]["repo_name"] .. ":" .. body["push_data"]["tag"]
-- Preparing Slack payload
local slack = {
response_type= "in_channel",
text= message}
local result, err = json.encode(slack)
-- Set request header to application/json
r:SetRequestHeader("Content-Type", "application/json")
-- Set request method to PUT
r:SetRequestMethod("POST")
-- Set modified request body
r:SetRequestBody(result)
Click "Save" and then try testing it with the "Send" button:
Connect everything together
Navigate to https://api.slack.com/messaging/webhooks and click "Create your Slack app". Select your workspace, enter a name that you will remember.
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.Open your bucket details (via https://my.webhookrelay.com/buckets)
-
Open "OUTPUT DESTINATIONS" tab and create a new output called "Slack" with the Slack URL from step 2:
-
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:
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!
Originally released on https://webhookrelay.com/v1/examples/convert-dockerhub-webhook-to-slack.html
Posted on February 28, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.