Back to blog home page

How to Build a GitHub Webhook with node.js

Posted by on Nov 18, 2014

GitHub Webhooks

Every GitHub repository can easily communicate with a web server whenever the repository is pushed. This communication is called a webhook.  Webhooks are used for many reasons, including updating an external issue tracker, creating a backup mirror or triggering a continuous integration build.

Click here for information on how to set up webhooks on GitHub.  For this tutorial let’s set the webhook to port ‘8082’, with URL ‘pushchanges’ and secret ‘123456’.  You will obtain a GitHub personal access token as described in GitHub.

In this tutorial, you will learn how to listen to push events on GitHub through the webhook,and then fetch modified content with Node.js.

The Webhook

The first thing you will do is listen to push events by using node-github-hook:

Fetching Content

You will use octonode to fetch file contents and return the contents as a Node.js ‘Buffer’ in the callback.

(Note: In this example Underscore.string is used to extract the branch, after the rightmost slash.)

Build your Angular app and connect it to any database with Backand today. – Get started now.

  • “For this tutorial let’s set the webhook to port , with URL and secret .” Looks like your CMS has removed some of the content that wasn’t properly escaped.

  • I’ve actually written a GitHub Webhook Server in node.js named Gookie, and it makes it easy to use a simple config.json to set up your webhook, secret and a deploy command/script. For triggering auto-deploy build scripts, it’s much simpler to use (and fully tested) compared to writing your own webhook server: https://github.com/hoxxep/Gookie

  • Pingback: Node Weekly No.62 | ENUE Blog()