Skip to content

Instantly share code, notes, and snippets.

@madhums
Created August 3, 2011 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madhums/1123769 to your computer and use it in GitHub Desktop.
Save madhums/1123769 to your computer and use it in GitHub Desktop.
node.js multistage deployment using capistrano.
# /Capfile
# you need to install capistrano gem
# gem install capistrano
# gem install capistrano-ext
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
load 'config/deploy'
cap staging deploy:setup
cap staging deploy
# /config/deploy.rb
APP_CONFIG = YAML.load_file(File.dirname(__FILE__)+"/config.yml")
# /config/config.yml file will contain
# host: host_name_or_IP
# passphrase: ssh_passphrase
set :stages, %w(staging production)
set :default_stage, 'staging'
require 'capistrano/ext/multistage'
set :application, "app_name" #usually the one given in package.json of your project
set :node_file, "app.js"
set :host, APP_CONFIG['host']
set :repository, "git@github.com:username/app_name.git"
set :user, "root"
set :admin_runner, 'root'
set :scm, :git
set :deploy_via, :remote_cache
role :app, host
set(:deploy_to) {"/path/#{application}/#{node_env}"}
set :scm_passphrase, APP_CONFIG['passphrase']
set :use_sudo, false
default_run_options[:pty] = true
namespace :deploy do
task :start, :roles => :app, :except => { :no_release => true } do
run "sudo start #{application}_#{node_env}"
end
task :stop, :roles => :app, :except => { :no_release => true } do
run "sudo stop #{application}_#{node_env}"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "sudo restart #{application}_#{node_env} || sudo start #{application}_#{node_env}"
end
desc "Symlink config files"
task :symlink_configs, :roles => :app do
%w[app_config.yml].each do |f|
run "ln -sf #{shared_path}/config/#{f} #{release_path}/config/#{f}"
end
end
desc "Check required packages and install if packages are not installed"
task :check_packages, roles => :app do
run "cd #{release_path} && jake depends"
end
task :create_deploy_to_with_sudo, :roles => :app do
run "sudo mkdir -p #{deploy_to}"
run "sudo chown #{admin_runner}:#{admin_runner} #{deploy_to}"
end
task :write_upstart_script, :roles => :app do
upstart_script = <<-UPSTART
description "#{application}"
start on startup
stop on shutdown
script
# We found $HOME is needed. Without it, we ran into problems
export HOME="/home/#{admin_runner}"
export NODE_ENV="#{node_env}"
cd #{current_path}
exec sudo -u #{admin_runner} sh -c "NODE_ENV=#{node_env} /usr/local/bin/node #{current_path}/#{node_file} #{application_port} >> #{shared_path}/log/#{node_env}.log 2>&1"
end script
respawn
UPSTART
put upstart_script, "/tmp/#{application}_upstart.conf"
run "sudo mv /tmp/#{application}_upstart.conf /etc/init/#{application}_#{node_env}.conf"
end
desc "Update submodules"
task :update_submodules, :roles => :app do
run "cd #{release_path}; git submodule init && git submodule update"
end
task :create_deploy_to_with_sudo, :roles => :app do
run "sudo mkdir -p #{deploy_to}"
run "sudo chown #{admin_runner}:#{admin_runner} #{deploy_to}"
end
task :install_dependent_packages, :roles => :app do
run "cd #{release_path} && npm install"
end
end
before 'deploy:setup', 'deploy:create_deploy_to_with_sudo'
after 'deploy:setup', 'deploy:write_upstart_script'
after "deploy:finalize_update", "deploy:update_submodules", "deploy:symlink_configs", "deploy:install_dependent_packages", "deploy:cleanup"
# /config/deploy/production.rb
set :node_env, "production"
set :branch, "production"
# /config/deploy/staging.rb
set :node_env, "staging"
set :branch, "master"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment