Created
September 15, 2012 16:44
-
-
Save ngryman/3728772 to your computer and use it in GitHub Desktop.
git deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var spawn = require('child_process').spawn; | |
var exec = require('child_process').exec; | |
var fs = require('fs'); | |
task('install-deps', ['setup'], function() { | |
console.log('\n\n > Attempting to install dependencies via npm\n'.blue); | |
console.log('\tExecuting command:\n\t$ npm install\n'.grey); | |
var npm = spawn('npm', ['install']); | |
npm.stdout.on('data', function(data) { | |
process.stdout.write(('\t' + data).grey); | |
}); | |
npm.stderr.on('data', function(data) { | |
process.stdout.write(('\t' + data).grey); | |
}); | |
npm.on('exit', function(code) { | |
if (code === 0) { | |
console.log('\n + npm installed dependencies successfully'.green); | |
complete(); | |
} | |
else { | |
throw new Error('npm exited with error code ' + code); | |
} | |
}); | |
}, true); | |
task('install-version', ['setup'], function() { | |
console.log('\n\n > Attempting to create versioned directory\n'.blue); | |
console.log(('\tExecuting command:\n\t$ rm -rf ' + versionPath + ' && mkdir -p ' + versionPath + '\n').grey); | |
exec('rm -rf ' + versionPath + ' && mkdir -p ' + versionPath, function(error) { | |
if (error !== null) { | |
console.log(error.message); | |
throw error; | |
} | |
else { | |
console.log((' + Versioned directory created successfully').green); | |
complete(); | |
} | |
}); | |
}, true); | |
task('install-files', ['setup'], function() { | |
console.log('\n\n > Attempting to moved files into desired location\n'.blue); | |
console.log((' Executing command:\n $ rsync -a . ' + versionPath).grey); | |
exec('rsync -a . ' + versionPath, function(error) { | |
if (error !== null) { | |
console.log(error.message); | |
throw error; | |
} | |
else { | |
console.log('\n + Files moved successfully'.green); | |
complete(); | |
} | |
}); | |
}, true); | |
task('install-games', ['setup'], function() { | |
console.log('\n\n > Attempting to install specific games\n'.blue); | |
console.log((' Executing command:\n $ chmod +x ' + versionPath + '/games/sudoku/server/qqwing').grey); | |
exec('chmod +x ' + versionPath + '/games/sudoku/server/qqwing', function(error) { | |
if (error !== null) { | |
console.log(error.message); | |
throw error; | |
} | |
else { | |
console.log('\n + Sudoku grid generation OK'.green); | |
complete(); | |
} | |
}); | |
}, true); | |
task('install', ['install-deps', 'install-version', 'install-files', 'install-games']); | |
task('stop', ['setup'], function() { | |
console.log('\n\n > Attempting to stop server\n' .blue); | |
console.log((' Executing command:\n $ forever stop ' + symlinkPath + '/server/index.js').grey); | |
exec('forever stop ' + symlinkPath + '/server/index.js', function(error) { | |
if (error !== null && error.message.indexOf('No such file or directory') === -1) { | |
console.log(error.message); | |
throw error; | |
} | |
else { | |
console.log('\n + Server stopped'.green); | |
complete(); | |
} | |
}); | |
}, true); | |
task('live', ['setup'], function() { | |
console.log('\n\n > Attempting to make a symbolic link\n' .blue); | |
console.log((' Executing command:\n $ rm -f ' + symlinkPath + ' && ln -s ' + versionPath + ' ' + symlinkPath).grey); | |
exec('rm -f ' + symlinkPath + ' && ln -s ' + versionPath + ' ' + symlinkPath, function(error) { | |
if (error !== null) { | |
console.log(error.message); | |
throw error; | |
} | |
else { | |
console.log('\n + Symlink created'.green); | |
complete(); | |
} | |
}); | |
}, true); | |
task('start', ['setup'], function() { | |
console.log('\n\n > Attempting to start server\n' .blue); | |
console.log((' Executing command:\n $ NODE_ENV=' + branch + ' forever start ' + symlinkPath + '/server/index.js').grey); | |
exec('forever start ' + symlinkPath + '/server/index.js', { | |
env: process.env | |
},function(error) { | |
if (error !== null) { | |
console.log(error.message); | |
throw error; | |
} | |
else { | |
console.log('\n + Server started'.green); | |
complete(); | |
} | |
}); | |
}, true); | |
task('deploy', ['install', 'stop', 'live', 'start'], function() {}); | |
task('default', ['deploy']); | |
var branch, repository, versionPath, symlinkPath; | |
task('setup', [], function() { | |
var package = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf-8')); | |
branch = process.env.branch || 'staging'; | |
repository = process.env.repository || ''; | |
versionPath = '~/' + repository + '/versions/' + package.name + '@' + package.version + '-' + branch; | |
symlinkPath = '~/' + repository + '/' + branch; | |
process.env.NODE_ENV = branch; | |
}); | |
function stylize(str, style) { | |
var styles = { | |
//styles | |
'bold': [1, 22], 'italic': [3, 23], | |
'underline': [4, 24], 'inverse': [7, 27], | |
//grayscale | |
'white': [37, 39], 'grey': [90, 39], | |
'black': [90, 39], | |
//colors | |
'blue': [34, 39], 'cyan': [36, 39], | |
'green': [32, 39], 'magenta': [35, 39], | |
'red': [31, 39], 'yellow': [33, 39] | |
}; | |
return '\033[' + styles[style][0] + 'm' + str + '\033[' + styles[style][1] + 'm'; | |
} | |
['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'] | |
.forEach(function(style) { | |
String.prototype.__defineGetter__(style, function() { | |
return stylize(this, style); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
read oldrev newrev branch | |
branch=`echo $branch | sed 's/.*\/\(.*\)$/\1/'` | |
repository=`basename "$PWD"` | |
mkdir /tmp/$newrev | |
git --work-tree=/tmp/$newrev checkout -f $branch | |
chdir /tmp/$newrev/ | |
jake branch=$branch repository=${repository%.git} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment