Starter Project
Create a Webhook Receiver
The goal of this tutorial is to build a custom webhook receiver using our starter project template as the base. A webhook receiver is a small http server that receives and processes webhooks.
This tutorial assumes you have a working knowledge of the Go programming language. You will also need to install boilr, a scaffolding system used to generate new code projects from templates.
Step 1: Create the Project
Drone provides scaffolding for creating a new webhook receiver from a project template. We will bootstrap our project using this template.
- Install the webhook template.
$ boilr template download drone/boilr-webhook drone-webhook
- Create a new webhook project.
$ boilr template use drone-webhook my-webhook-receiver
Step 2: Customize the Code
The generated project creates a simple server capable of receiving, authenticating and parsing requests. You only need to modify the plugin.Deliver
function to include your custom processing logic.
1 package plugin
2
3 import (
4 "context"
5
6 "github.com/drone/drone-go/plugin/webhook"
7 )
8
9 // New returns a new webhook extension.
10 func New() webhook.Plugin {
11 return &plugin{}
12 }
13
14 type plugin struct {
15 }
16
17 func (p *plugin) Deliver(ctx context.Context, req *webhook.Request) error {
18 // insert your custom logic here
return nil
19 }
Step 3: Build Your Docker Image
The generated project includes a Dockefile
that you can use to build and publish images. You will need to update the Dockerfile entrypoint.
- Build your binary file:
$ GOOS=linux CGO_ENABLED=0 go build
- Build and publish your Docker image:
$ docker build -t my-webhook-receiver .
$ docker push my-webhook-receiver
Step 4: Start the Webhook Receiver
- Create a shared secret used to sign the http request:
$ openssl rand -hex 16
bea26a2221fd8090ea38720fc445eca6 - Start the webhook receiver.
$ docker run \
-p 3000:3000 \
-e DRONE_SECRET=bea26a2221fd8090ea38720fc445eca6 \
my-webhook-receiver
Step 5: Enable Webhooks
Once the webhook receiver is running you will need to provide your Drone server with the webhook endpoint and secret:
DRONE_WEBHOOK_ENDPOINT=http://...
DRONE_WEBHOOK_SECRET=bea26a2221fd8090ea38720fc445eca6