A Github webhook implementation to address Drone IO's missing "auto-cancelation of previous builds" feature.
- David Almendarez (
@david
)
On push
and on pull_request
events are sent to the flask web-hook app and is authenticated using a SECRET env var named WEBHOOK_VERIFY_TOKEN
. This token is then compared with the SECRET setup on your Github web-hook settings page for your repository as shown below:
The flask app then compares the sha1 after
(hash) sent by either of those aforementioned events
with the latest (last) Drone IO builds hash
, if the hash do not match, that Drone build is then canceled (or stopped) via the Drone API. The Drone API is authenticated via a few env vars, listed below, so please assign these env vars on the machine running your web-hook.
WEBHOOK_VERIFY_TOKEN
: A string, should be kept a secret; used to authenticate your app with Github's web-hook mechanism. This needs to match thesecret
value entered on your repo's webhook settings page.DRONE_SERVER
: should be set ashttps://drone.britecorepro.com
DRONE_TOKEN
: your personal access token displayed on your profile page on our drone server (listed above).DRONE_REPO_OWNER_USERNAME
: your github username, i.e.:SirFroweey
is mine. Should be set toIntuitiveWebSolutions
if we decide to host this app on our DevOps infrastructure.DRONE_REPO_NAME
: your repository name, i.e.: 'BriteCore' or in this caseDroneAutoCancel
.ONLY_PROCESS_PR_EVENTS
: Boolean, should our hook only processpull_request
event types? Defaults toFalse
.
- Setup your local machines environmental variables as shown above.
- Clone this repository onto your machine.
cd
into your repository directory.- Run
pip install -r requirements.txt
or even better, setup a virtualenv and activate it then install the requirements. - Download
ngrok
and move it into yourApplications/
folder. - Create a symlink, as shown here:
# cd into your local bin directory
cd /usr/local/bin
# create symlink
ln -s /Applications/ngrok ngrok
- Run
python hook.py
on your terminal of choice - Execute
ngrok http 5000
- Setup your repo's Github webhook as shown below and point it to your
ngrok
https
URL that we generated earlier (via thengrok http 5000
command):
-
The Github
event
event should report back as successful with a green200
status code, as shown below: -
Pushing to your repository where you setup your web-hook (presumably your
BriteCore
fork), should report back with a successful200
response as shown below:
Experimental for now, ask @david for access to private repo.
- Install Docker
docker login
- Run
docker-compose up
- Navigate to
127.0.0.1:5000/webhook
on your web browser and ensure you get a 405 page, this signals a successful startup. - Download
ngrok
and move it into yourApplications/
folder. - Create a symlink, as shown here:
# cd into your local bin directory
cd /usr/local/bin
# create symlink
ln -s /Applications/ngrok ngrok
- Execute
ngrok http 5000
- Setup your repo's Github webhook as shown below and point it to your
ngrok
https
URL that we generated earlier (via thengrok http 5000
command):
- The Github
event
event should report back as successful with a green200
status code, as shown below:
- Pushing to your repository where you setup your web-hook (presumably your
BriteCore
fork), should report back with a successful200
response as shown below:
- It may be best to uncheck the
push
event from the Github web-hook settings page to prevent being spamming by400
requests due to thepush
event being filtered out when setting theONLY_PROCESS_PR_EVENTS
environment variable toTrue
.