Skip to content

Instantly share code, notes, and snippets.

@thiagoramos23
Forked from shmatov/deploy.rake
Last active August 29, 2015 14:22
Show Gist options
  • Save thiagoramos23/fda3188b9e638a482fe5 to your computer and use it in GitHub Desktop.
Save thiagoramos23/fda3188b9e638a482fe5 to your computer and use it in GitHub Desktop.
# lib/tasks/deploy.rake
namespace :deploy do
desc 'Deploy to staging environment'
task :staging do
exec 'mina deploy -f config/deploy/staging.rb'
end
end
# config/deploy/staging.rb
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'
# Config
# ==============================================================================
set :term_mode, :system
set :rails_env, 'staging'
set :domain, '50.56.204.208'
set :port, 37894
set :deploy_to, "/home/apps/mfp/#{rails_env}"
set :app_path, "#{deploy_to}/#{current_path}"
set :repository, '[email protected]:goup/mfp.git'
set :brach, 'master'
set :user, 'apps'
set :shared_paths, ['public/static', 'tmp']
set :keep_releases, 5
# RVM
# ==============================================================================
set :rvm_path, '/usr/local/rvm/scripts/rvm'
task :environment do
invoke 'rvm:use[1.9.3]'
end
# Setup task
# ==============================================================================
task :setup do
queue! %{
mkdir -p "#{deploy_to}/shared/tmp/pids"
}
end
# Deploy task
# ==============================================================================
desc "deploys the current version to the server."
task :deploy => :environment do
deploy do
invoke 'git:clone'
invoke 'bundle:install'
invoke 'rails:db_migrate'
invoke 'rails:assets_precompile'
invoke 'deploy:link_shared_paths'
to :launch do
invoke :'unicorn:restart'
end
end
end
# Unicorn
# ==============================================================================
namespace :unicorn do
set :unicorn_pid, "#{app_path}/tmp/pids/unicorn.pid"
set :start_unicorn, %{
cd #{app_path}
bundle exec unicorn -c #{app_path}/config/unicorn/#{rails_env}.rb -E #{rails_env} -D
}
# Start task
# ------------------------------------------------------------------------------
desc "Start unicorn"
task :start => :environment do
queue 'echo "-----> Start Unicorn"'
queue! start_unicorn
end
# Stop task
# ------------------------------------------------------------------------------
desc "Stop unicorn"
task :stop do
queue 'echo "-----> Stop Unicorn"'
queue! %{
test -s "#{unicorn_pid}" && kill -QUIT `cat "#{unicorn_pid}"` && echo "Stop Ok" && exit 0
echo >&2 "Not running"
}
end
# Restart task
# ------------------------------------------------------------------------------
desc "Restart unicorn using 'upgrade'"
task :restart => :environment do
invoke 'unicorn:stop'
invoke 'unicorn:start'
end
end
# config/unicorn/staging.rb
app_path = "/home/apps/mfp/staging/current"
worker_processes 1
preload_app true
timeout 180
listen '127.0.0.1:9021'
user 'apps', 'apps'
working_directory app_path
pid "#{app_path}/tmp/pids/unicorn.pid"
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
ActiveRecord::Base.establish_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment