Skip to content

Instantly share code, notes, and snippets.

@morrelinko
Last active August 30, 2016 06:52
Show Gist options
  • Save morrelinko/5707626a837b9e03bb1c to your computer and use it in GitHub Desktop.
Save morrelinko/5707626a837b9e03bb1c to your computer and use it in GitHub Desktop.
Git deploy NodeJS script
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
child = require('child_process'),
argf = '',
deployDir = '/home/morrelinko/socialinko';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
argf += data;
});
process.stdin.on('end', function () {
argf = argf.trim().split(' ');
var fromCommit = argf[0],
toCommit = argf[1],
branch = argf[2];
// Ensure we only deploy if master branch is pushed
if (/master$/.test(branch) == false) {
console.log(['Received branch', branch, ', not deploying.'].join(' '));
process.exit;
}
// Copy files to the deploy directory
child.exec('git --work-tree ' + deployDir + ' checkout -f master', function(error, stdout, stderr) {
if (error) {
console.log('DEPLOY: An error occured: ', error.message || error);
return process.exit();
}
console.log('DEPLOY: <master>(' + toCommit + ' copied to ' + deployDir);
});
});
@morrelinko
Copy link
Author

This is a NodeJs port of the Ruby code used in this blog post
http://krisjordan.com/essays/setting-up-push-to-deploy-with-git

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