Skip to content

Instantly share code, notes, and snippets.

@kyrcha
Last active January 11, 2017 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrcha/e1f1986634cb67e7c9637d523e2a5c97 to your computer and use it in GitHub Desktop.
Save kyrcha/e1f1986634cb67e7c9637d523e2a5c97 to your computer and use it in GitHub Desktop.
Deploying a node app with flightplan

Complete guide on how to deploy a node app in an Ubuntu server using flightplan can be found here:

var plan = require('flightplan');
var appName = 'app';
var username = 'deploy';
var startFile = 'bin/www';
var tmpDir = appName+'_' + new Date().getTime();
// configuration
plan.target('staging', [
{
host: '<staging-server-url>',
username: username,
port: 6622,
passphrase: 'passphrase',
privateKey: process.env.DEPLOY_PRIVATEKEY
},
]);
plan.target('production', [
{
host: '<staging-server-url>',
username: username,
port: 6622,
passphrase: 'passphrase',
privateKey: process.env.DEPLOY_PRIVATEKEY
},
]);
// run commands on localhost
plan.local('deploy', function(local) {
// @TODO: uncomment these if you need to run a build on your machine first
// local.log('Run build');
// local.exec('grunt build');
var passphrase = local.prompt('Enter your passphrase:', { hidden: true });
plan.target().runtime.hosts.forEach(function(host) {
host.passphrase = passphrase;
});
local.log('=== Deploy correct branch ===');
if(plan.runtime.target == "production") {
local.exec('git checkout master');
} else if(plan.runtime.target == "staging") {
local.exec('git checkout develop');
} else {
throw new Error('Bad environment.');
}
local.log('=== Copy files to remote hosts ===');
var filesToCopy = local.exec('git ls-files', {silent: true});
// rsync files to all the destination's hosts
local.transfer(filesToCopy, '/tmp/' + tmpDir);
});
// run commands on remote hosts (destinations)
// Structure:
// ~/
// |- tmp
// |- releases
// |- equad -> ~/tmp/equad_1437423777498
plan.remote('deploy', function(remote) {
remote.log('=== Stop the app ===');
remote.exec('forever stop ~/'+appName+'/'+startFile, {failsafe: true});
remote.log('=== Creating home structure and cleanup ===');
remote.exec('mkdir -p ~/releases');
//@TODO: Now I am deleting them, in the future retain them, or at least the previous one
remote.exec('rm -rf ~/releases/*');
remote.exec('rm -rf $(readlink ~/' + appName + ')');
remote.exec('rm -rf ~/' + appName);
remote.log('=== Move app to home (and releases) ===');
remote.sudo('cp -R /tmp/' + tmpDir + ' ~/', {user: username});
remote.rm('-rf /tmp/' + tmpDir);
remote.cp('-R ~/' + tmpDir + ' ~/releases');
remote.log('=== Install dependencies ===');
remote.sudo('npm --only=prod --prefix ~/' + tmpDir + ' install ~/' + tmpDir, {user: username});
remote.log('=== Linking ===');
remote.sudo('ln -snf ~/' + tmpDir + ' ~/'+appName, {user: username});
remote.log('=== Start the app ===');
remote.exec('forever start ~/'+appName+'/'+startFile);
});
plan.remote('restart', function(remote) {
remote.log('=== Restarting application ===');
remote.exec('forever restart ~/'+appName+'/'+startFile);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment