Skip to content

Instantly share code, notes, and snippets.

@chuckreynolds
Created October 21, 2019 23:56
Show Gist options
  • Save chuckreynolds/f616148cb77f3d069b101a0fe5f90229 to your computer and use it in GitHub Desktop.
Save chuckreynolds/f616148cb77f3d069b101a0fe5f90229 to your computer and use it in GitHub Desktop.
How to Use Node.js and Github Webhooks to Keep Remote Projects in Sync - works for public repos only. Just storing this here for reference.

How to Use Node.js and Github Webhooks to Keep Remote Projects in Sync

Source Post: https://www.digitalocean.com/community/tutorials/how-to-use-node-js-and-github-webhooks-to-keep-remote-projects-in-sync

FYI: this has nothing to do with SSH keys - therefore private repos wont work with this.

const secret  = 'your_secret_here';
const repopwd = '/your_repo_path_here/';
const http    = require('http');
const crypto  = require('crypto');
const exec    = require('child_process').exec;

http.createServer(function (req, res) {
    req.on('data', function(chunk) {
        let sig = "sha1=" + crypto.createHmac('sha1', secret).update(chunk.toString()).digest('hex');

        if (req.headers['x-hub-signature'] == sig) {
            exec(`cd ${repopwd} && git pull`);
        }
    });
    res.end();
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment