GitHub/GitLab Webhooks

Yannik Schmidt
3 min readAug 15, 2020

I know it isn’t cool these days to just have a build-script, but you really don’t have to follow the trend of making CI more and more complicated. Jenkins and Docker both have their respective places, but these powerful tools should be used with care, and only whenever the complexity of a project actually justifies and necessitates complex CI-pipelines.

Cue Webhooks. GitLab and GitHub have an inbuilt feature called Webhooks. Whenever a certain event, for example a push on the master branch, occurs, a remote URL is automatically queried to inform a remote server of this event.

Setup on your server

Set up a deploy script

First, you need to think about the steps required to redeploy your application with a new version. The simplest form might just be a script which looks like this:

#!/bin/bash 
cd /path/to/project/
git pull
sudo systemctl restart project.service

If you want to do a restart via systemctl as a normal user, you can allow it in /etc/sudoers like this:

username ALL=(root) NOPASSWD: /bin/systemctl restart SERVICE.service

Obviously if the build script is contained within the respective project, something like this might be more appropriate:

username ALL=(otherUser) NOPASSWD: /path/to/build/script.sh

Set up a webhook listener

I wrote a very simple and easy to use webhook listener and handler. Clone the project and setup an auto-start, either via systemd or cron. Create a config file as described in the README.md and enter the script that should be executed. Select a secure token or password which you will later enter in the GitLab- or GitHub web interface. If you want your application to be available through HTTPS you have to set up a reverse proxy and certificate. A Nginx-proxy config snippet can be found within the README.md as well.

Enabling on GitHub

On GitHub, select the project or organization you want to create a webhook for, navigate to Settings -> Webhooks, enter your server’s address, select application/json, enter the secret token or password you chose before and select which events should trigger a webhook. The response to an automated check of your settings should be available shortly after.

Enabling on GitLab

On Gitlab, select the project you want to create a webhook for, also navigate to Settings -> Webhooks (or directly append -/hooks to your repository base URL), enter your server’s address, enter the secret token or password you chose before and select which events should trigger a webhook. For this example we choose Push-events on the master-branch. Save and scroll down to test your settings.

Feel free to share your thoughts or ask a question!

--

--

Yannik Schmidt

Python programmer at heart, Linux Expert at work, GameDev on my good days, web developer on the bad days.