Example Go Plugin
This guide provides a brief tutorial for creating a webhook plugin, using the Go programming language, to trigger http requests during the build pipeline. The below example demonstrates how we might configure a webhook plugin in the Yaml file:
1 kind: pipeline
2 type: docker
3 name: default
4
5 steps:
6 - name: webhook
7 image: acme/webhook
8 settings:
9 url: http://hook.acme.com
10 method: post
11 body: |
12 hello world
Create a Go program that makes an http request using the Yaml configuration parameters, which are passed to the program as environment variables in uppercase, prefixed with PLUGIN_. Here is more information on plugin inputs.
1 package main
2
3 import (
4 "net/http"
5 "os"
6 )
7
8 func main() {
9 body := strings.NewReader(
10 os.GetEnv("PLUGIN_BODY"),
11 )
12
13 req, err := http.NewRequest(
14 os.GetEnv("PLUGIN_METHOD"),
15 os.GetEnv("PLUGIN_URL"),
16 body,
17 )
18 if err != nil {
19 os.Exit(1)
20 }
21 }
Compile your binary on the host machine for the target platform. Compiling on the host machine and adding the binary to the image is considered a best practice because it reduces the overall image size.
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o webhook
Create a Dockerfile that adds your compiled binary to the image, and configures the image to run your binary as the main entrypoint.
1 FROM alpine
2 ADD webhook /bin/
3 RUN apk -Uuv add ca-certificates
4 ENTRYPOINT /bin/webhook
Build and publish your plugin to the Docker registry. Once published your plugin can be shared with the broader Drone community.
$ docker build -t acme/webhook
$ docker push acme/webhook
Execute your plugin locally from the command line to verify it is working:
$ docker run --rm \
-e PLUGIN_METHOD=post \
-e PLUGIN_URL=http://hook.acme.com \
-e PLUGIN_BODY=hello \
acme/webhook
Plugin templates
To make it easier to create plugins we use templates to make them. We use the tool Boilr to make the plugin.
The default plugin template is here and there are more complex community versions available too.