Last active
December 24, 2015 00:09
-
-
Save rogeruiz/6715103 to your computer and use it in GitHub Desktop.
Gitolite Post-Receive Hook for Node.js
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
#!/usr/bin/env node | |
var deployable = process.env.GL_OPTION_DEPLOYABLE; | |
var jekyll = process.env.GL_OPTION_JEKYLL; | |
if ( ! deployable ) { | |
return; | |
} | |
var fs = require( 'fs' ); | |
var spawn = require( 'child_process' ).spawn; | |
var exec = require( 'child_process' ).exec; | |
var repoName = process.env.GL_REPO.toLowerCase(); | |
var repoBase = process.env.GL_REPO_BASE + '/' + repoName + '.git'; | |
var workTree = '/var/www/' + repoName; | |
function createDirectory () { | |
console.log('=> Creating ' + workTree + ' directory...'); | |
exec( 'sudo mkdir -p ' + workTree + '', function ( error, stdout, stderr ) { | |
if ( ! error ) { | |
console.log( '=> ' + workTree + ' created successfully...' ); | |
updatePermissions(); | |
} | |
} ); | |
} | |
function updateDirectory () { | |
console.log( '=> Updating ' + workTree + ' with latest commit...' ); | |
var git = spawn( 'git', [ 'checkout', '-f' ], { | |
env: { | |
'GIT_WORK_TREE': workTree, | |
'GIT_DIR': repoBase | |
} | |
} ); | |
git.on( 'close', function ( code ) { | |
if ( 0 === code ) { | |
console.log( '=> ' + workTree + ' updated successfully...' ); | |
} | |
} ); | |
if ( jekyll ) { | |
loadJekyll(); | |
} | |
} | |
function loadJekyll() { | |
var previousDirectory = process.cwd(); | |
try { | |
process.chdir( workTree ); | |
console.log( '=> Installing gems for Jekyll site [ ' + repoName + ' ]...' ); | |
exec( 'rvm 2.2.2 exec bundle install' , function( error, stdout, stderr ) { | |
if ( ! error ) { | |
console.log( '=> Gems for Jekyll site [ ' + repoName + ' ] successfully installed... ' ); | |
} | |
} ); | |
console.log( '=> Building Jekyll site [ ' + repoName + ' ] with `bundle exec jekyll build`...' ); | |
exec( 'rvm 2.2.2 exec bundle exec jekyll build', function( error, stdout, stderr ) { | |
if ( ! error ) { | |
console.log( '=> ' + workTree + '/_sites deployed successfully...' ); | |
updatePermissions( true ); | |
process.chdir( previousDirectory ); | |
} | |
} ); | |
} catch ( error ) { | |
console.error( 'Project directory not found: ' + error ); | |
} | |
} | |
function updatePermissions ( disable ) { | |
exec( 'sudo chown -R git:www-data ' + workTree + '', function ( error, stdout, stderror ) { | |
if ( ! error ) { | |
console.log( '=> Permissions recursively resolved to git:www-data...' ); | |
if ( ! disable ) { | |
updateDirectory(); | |
} | |
} | |
} ); | |
} | |
console.log( '=> Deploying ' + repoName + '...' ); | |
fs.exists( workTree, function ( exists ) { | |
if ( exists ) { | |
console.log( '=> ' + workTree + ' exists...' ); | |
updatePermissions(); | |
} else { | |
console.log( '=> ' + workTree + ' doesn\'t exist.' ); | |
createDirectory(); | |
} | |
} ); | |
process.on( 'exit', function ( code ) { | |
if ( 0 === code ) { | |
console.log( '================================================================================' ); | |
if ( ! jekyll ) { | |
console.log( '=> Your site is available @ http://rogeruiz.com/' + repoName + '' ); | |
} else { | |
console.log( '=> Jekyll site [ ' + repoName + ' ] has been updated' ); | |
} | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment