Created
May 20, 2014 12:47
-
-
Save ksafranski/589e4193bf8fb55df503 to your computer and use it in GitHub Desktop.
Deploy to multiple environments on Nodejitsu with the same application
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
// Nodejitsu multiple environment deploy | |
// | |
// Prerequisits: | |
// Jitsu deploy CLI | |
// | |
// Usage: | |
// Include this file in root of project along with configured | |
// jitsu_conf.json file | |
// Run by calling: | |
// node deploy [ENV] | |
var fs = require('fs'); | |
var child_process = require('child_process'); | |
var tty = require('tty'); | |
var backup_pkg = './package.json.BAK'; | |
// Helper | |
var copyFileSync = function(srcFile, destFile, encoding) { | |
var content = fs.readFileSync(srcFile, encoding); | |
fs.writeFileSync(destFile, content, encoding); | |
}; | |
// Get the stuff | |
var pkg = require('./package.json'); | |
var config = require('./jitsu_conf.json'); | |
// Create temp copy of package.json | |
copyFileSync('./package.json', backup_pkg, 'utf-8'); | |
// Deploy env argument | |
var env = process.argv[2]; | |
// Set params | |
for (var key in config[env]) { | |
pkg[key] = config[env][key]; | |
} | |
// Write temp package.json for deploy | |
fs.writeFile('./package.json', JSON.stringify(pkg, null, 4), function (err) { | |
// Write error | |
if (err) { | |
throw err; | |
} | |
// Run the deploy process | |
var runner = child_process.spawn('jitsu', [ 'deploy' ]); | |
// Process STDIN | |
function inData(c) { | |
runner.stdin.write(c); | |
} | |
// Process STDOUT | |
function outData(c) { | |
process.stdout.write(c); | |
} | |
process.stdin.resume(); | |
process.stdin.on('data', inData); | |
runner.stdout.on('data', outData); | |
tty.setRawMode(true); | |
// On complete | |
runner.on('exit', function(code) { | |
tty.setRawMode(false); | |
process.stdin.pause(); | |
process.stdin.removeListener('data', inData); | |
runner.stdout.removeListener('data', outData); | |
// Get original package.json | |
var backup = fs.readFileSync(backup_pkg); | |
// Re-Write original package.json | |
fs.writeFile('./package.json', backup, function (err) { | |
if (err) { | |
console.log('ERROR: Could not reset package.json'); | |
} else { | |
// Remove backup | |
fs.unlinkSync(backup_pkg); | |
} | |
}); | |
}); | |
}); |
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
{ | |
"dev": { | |
"name": "myapp-dev", | |
"subdomain": "myapp-dev", | |
"domains": [ | |
"myapp-dev.com" | |
] | |
}, | |
"test": { | |
"name": "myapp-test", | |
"subdomain": "myapp-test", | |
"domains": [ | |
"myapp-test.com" | |
] | |
}, | |
"prod": { | |
"name": "myapp-prod", | |
"subdomain": "myapp-prod", | |
"domains": [ | |
"myapp-prod.com" | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment