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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
kind: pipeline
type: docker
name: default

steps:
- name: webhook
  image: acme/webhook
  settings:
    url: http://hook.acme.com
    method: post
    body: |
            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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
	"net/http"
	"os"
	"strings"
)

func main() {
	body := strings.NewReader(
		os.Getenv("PLUGIN_BODY"),
	)

	_, err := http.NewRequest(
		os.Getenv("PLUGIN_METHOD"),
		os.Getenv("PLUGIN_URL"),
		body,
	)
	if err != nil {
		os.Exit(1)
	}
}

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
2
3
4
FROM alpine
ADD webhook /bin/
RUN apk -Uuv add ca-certificates
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

Starter Project

If you are interested in creating plugin we recommend using our starter project as a base to jumpstart development.