Skip to content

Instantly share code, notes, and snippets.

@blindsey
Created August 27, 2011 06:17
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save blindsey/1175059 to your computer and use it in GitHub Desktop.
Save blindsey/1175059 to your computer and use it in GitHub Desktop.
node.js auto deploy scripts
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
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 /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 &
@tompi
Copy link

tompi commented Jan 9, 2013

Nice!

Perhaps check if there really was an update, something like:
GIT_CHANGES=git pull | wc -l
if [ $GIT_CHANGES -gt 1 ]; then
kill, and then start
fi

So you can run this in test, qa, prod and not have e.g. prod restart if you push something
in dev-branch... (I assume the deploy hooks are run for all branches...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment