Using Github Webhooks for Deployment

Today i want to present a little tool, which allows you to track the status of github repositories. It allows you to call shell scripts on events on your github repos.

What does it exactly do?

Imagine you have a repository containing a library distributed over several remote machines. If you update your library you have to update all copies of your work as well. To automate this task you can use Github webhooks. For this purpose we created gohub. Gohub listens on a flexible port for Github webhooks calls and reacts by executing shell scripts. These shell scripts could be used to update your distributed library.

How to use it?

First you need to create a little config.json file. A simple example looks like this:

1
2
3
4
5
6
7
{
    "Hooks":[
        "Repo":"repo",
        "Branch":"master",
        "Shell":"/path/to/your/niftyscript.sh"
    ]
}

After this you need to secure your gohub. Github just uses 4 different ips for their calls. So you can secure the access by using iptables:

1
2
3
4
iptables -A INPUT -s 207.97.227.253/32 -p tcp -m tcp --dport <PORT> -j ACCEPT
iptables -A INPUT -s 50.57.128.197/32 -p tcp -m tcp --dport <PORT> -j ACCEPT
iptables -A INPUT -s 108.171.174.178/32 -p tcp -m tcp --dport <PORT> -j ACCEPT
iptables -A INPUT -s 50.57.231.61/32 -p tcp -m tcp --dport <PORT> -j ACCEPT

Of course you can also use the capabilities of your favorite proxy like nginx or haproxy.

Last thing you have to do before starting your gohub is to set up the Github webhook. You have to do this via the Github webinterface.

  • just navigate to your repository
  • go to settings
  • go to service hooks
  • go to webhook urls
  • add the address

Important: By convention your uri must look like that:

reponame_branch

For example:

http://gohub.example.com:8392/gohub_master

This is important, because gohub adds a request handler for every repository - branch combination you add to your config.json.

Do not forget to click update!

Now you are ready to start your gohub server

go run main.go --config /path/to/config.json --port 8392 --log ./gohub.log

You can test your webhook by clicking on “test webhook”. You should see gohub executing your commands.

Of course you can deploy this app like described here and here.

How to contribute?

Feel free to fork the project and add new features. Also any bug notifications and bug fixes would be appreciated.

Comments