Skip to content

Instantly share code, notes, and snippets.

@anoobbava
Forked from duykhoa/deploy.rb
Created May 31, 2018 11:07
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 anoobbava/ca5811eff7992cf1b0b6c8974b491a42 to your computer and use it in GitHub Desktop.
Save anoobbava/ca5811eff7992cf1b0b6c8974b491a42 to your computer and use it in GitHub Desktop.
Mina deploy with Puma task, support `start, stop, restart` feature, use `pumactl`. Thanks this tutorial for saving my life: http://ruby-journal.com/how-to-setup-rails-app-with-puma-and-nginx/
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm' # for rvm support. (http://rvm.io)
set :domain, 'deploy@host'
set :repository, 'git...
set :branch, 'master'
set :shared_paths, ['config/database.yml', 'config/application.yml', 'log', 'tmp/sockets', 'tmp/pids']
task :environment do
set :stage, ENV['to']
case stage
when 'production'
set :rails_env, 'production'
set :deploy_to, '
set :start_port, 30xx
when 'staging'
set :rails_env, 'staging'
set :deploy_to, ''
set :start_port, 30xx
else
print_error 'NO environment is specificed'
exit
end
set_default :puma_state, -> { "#{deploy_to}/#{shared_path}/tmp/sockets/puma.state" }
set_default :puma_pid, -> { "#{deploy_to}/#{shared_path}/tmp/pids/puma.pid" }
invoke :'rvm:use[ruby-v@gemset]'
end
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
queue %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/database.yml' and 'secrets.yml'."]
queue %[
repo_host=`echo $repo | sed -e 's/.*@//g' -e 's/:.*//g'` &&
repo_port=`echo $repo | grep -o ':[0-9]*' | sed -e 's/://g'` &&
if [ -z "${repo_port}" ]; then repo_port=22; fi &&
ssh-keyscan -p $repo_port -H $repo_host >> ~/.ssh/known_hosts
]
end
desc "Deploys the current version to the server."
task :deploy => :environment do
to :before_hook do
end
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
to :launch do
invoke :'puma_restart'
end
end
end
task :puma_start => :environment do
queue! %[
if [ -e '#{puma_pid}' ]; then
echo 'Puma is already running'
else
echo 'Start Puma'
cd #{deploy_to}/#{current_path} && puma -q -d -e #{rails_env} -C #{deploy_to}/#{current_path}/config/puma.rb -p #{start_port} -S #{puma_state} --pidfile #{puma_pid}
fi
]
end
task :puma_restart => :environment do
queue! %[
if [ -e '#{puma_pid}' ]; then
echo 'Restart Puma'
cd #{deploy_to}/#{current_path} && pumactl -S #{puma_state} restart
else
echo 'Start Puma'
cd #{deploy_to}/#{current_path} && puma -q -d -e #{rails_env} -C #{deploy_to}/#{current_path}/config/puma.rb -p #{start_port} -S #{puma_state} --pidfile #{puma_pid}
fi
]
end
task :puma_stop => :environment do
queue! %[
if [ -e '#{puma_pid}' ]; then
cd #{deploy_to}/#{current_path} && pumactl -S #{puma_state} stop
rm #{puma_pid}
else
echo 'Puma is not running. Phew!!!'
fi
]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment