Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.