Skip to content

Instantly share code, notes, and snippets.

@thefron
Created February 9, 2014 20:41
Show Gist options
  • Save thefron/8905634 to your computer and use it in GitHub Desktop.
Save thefron/8905634 to your computer and use it in GitHub Desktop.
Node.js deploy /w Capistrano 3
# config/deploy.rb
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'my_app'
set :repo_url, '[email protected]:USER/my_app.git'
# Default deploy_to directory is /var/www/my_app
set :deploy_to, '/var/node/my_app'
# Default value for :scm is :git
set :scm, :git
# Default value for :format is :pretty
set :format, :pretty
# Default value for :log_level is :debug
set :log_level, :debug
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
# set :linked_files, %w{config/config.json}
# Default value for linked_dirs is []
# set :linked_dirs, %w{log}
set :node_env, (fetch(:node_env) || fetch(:stage))
# Default value for default_env is {}
set :default_env, { node_env: fetch(:node_env) }
# Default value for keep_releases is 5
set :keep_releases, 5
set :pid_file_name, 'my_app.pid'
set :pid_file, "/home/#{fetch(:user)}/.forever/pids/#{fetch(:pid_file_name)}"
namespace :deploy do
desc 'Install node modules non-globally'
task :npm_install do
on roles(:app) do
execute "cd #{release_path} && npm install"
end
end
desc 'Start application'
task :start do
on roles(:app) do
within current_path do
execute :forever, 'start', '--pidFile', fetch(:pid_file_name), "#{current_path}/server.js"
end
end
end
desc 'Stop application'
task :stop do
on roles(:app) do
within current_path do
execute :forever, 'stop', "#{current_path}/server.js"
end
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
within current_path do
if test("[ -e #{fetch(:pid_file)} ]") && execute("kill -0 `cat #{fetch(:pid_file)}` > /dev/null 2>&1")
execute :forever, 'restart', '--pidFile', fetch(:pid_file_name), "#{current_path}/server.js"
else
execute :forever, 'start', '--pidFile', fetch(:pid_file_name), "#{current_path}/server.js"
end
end
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
before :restart, 'deploy:npm_install'
end
# config/deploy/staging.rb
server 'HOSTNAME',
user: fetch(:user), roles: %w{app},
ssh_options: {
forward_agent: true,
keys: ['path/to/keyfile']
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment