Example Bash Plugin

Updated 2 years ago by Admin

This provides a brief tutorial for creating a Drone webhook plugin, using simple shell scripting, to make an 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 simple shell script that invokes curl using the plugin settings defined in the Yaml, which are passed to the script as environment variables in uppercase and prefixed with PLUGIN_.

1  #!/bin/sh
2
3 curl \
4 -X ${PLUGIN_METHOD} \
5 -d ${PLUGIN_BODY} \
6 ${PLUGIN_URL}

Create a Dockerfile that adds your shell script to the image, and configures the image to execute your shell script as the main entrypoint.

1  FROM alpine
2 ADD script.sh /bin/
3 RUN chmod +x /bin/script.sh
4 RUN apk -Uuv add curl ca-certificates
5 ENTRYPOINT /bin/script.sh

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


How did we do?