Skip to content

Instantly share code, notes, and snippets.

@timestep
Forked from blindsey/github_post_commit.js
Last active August 29, 2015 14:10
Show Gist options
  • Save timestep/9f70433ac25087532b7b to your computer and use it in GitHub Desktop.
Save timestep/9f70433ac25087532b7b to your computer and use it in GitHub Desktop.
Steps:
0. Checkout your git repo from the server
1. Upload both of these files to the same directory on your server
2. chmod +x restart_node.sh
3. nohup node github_post_commit.js 2>&1 >> github_post_commit.log &
4. In the github admin for your repo, set a post commit hook to the url http://<your host>:8080/
5. Make a commit to your repo
6. Point a browser at http://<your host>:8080/ and you should see the commit
var http = require( 'http' ),
querystring = require( 'querystring' ),
exec = require( 'child_process' ).exec;
process.on( "uncaughtException", function( error ) {
console.error( "Uncaught exception: " + error.message );
console.trace();
});
var last_payload = {};
http.createServer( function( request, response ) {
if( request.method == 'GET' ) {
response.writeHead( 200, {'Content-Type': 'text/html'} );
response.write( "<html><body><pre>" );
response.write( JSON.stringify(last_payload, null, '\t') );
response.write( "</pre></body></html>" );
response.end();
} else {
var body = '';
request.on( 'data', function( chunk ) {
body += chunk.toString();
});
request.on( 'end', function() {
last_payload = JSON.parse( querystring.parse( body ).payload );
console.log( new Date(), request.method, request.url, last_payload );
exec( "./restart_node.sh", function( error, stdout, stderr ) {
response.writeHead( 200, {'Content-Type': 'text/plain'} );
response.end( error ? stderr : stdout );
});
});
}
}).listen( 8080 );
console.log( 'Server running at http://*:8080/' );
#!/bin/bash
cd //relevant directory
git pull
if [ ! -d log ]; then
mkdir log
fi
npm install
pkill -f 'node server.js'
NODE_ENV=production PORT=80 nohup node server.js >> log/node.log &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment