public
Created — forked from blindsey/github_post_commit.js

node.js auto deploy scripts

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8
Steps:
0. Checkout your git repo from the server (I use /var/www/carbonite)
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
github_post_commit.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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/' );
restart_node.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/bin/bash
 
cd /var/www/carbonite
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 &

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.