Skip to content

Instantly share code, notes, and snippets.

@tomlobato
Created January 24, 2016 10:58
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 tomlobato/7b619425f8ef980251f3 to your computer and use it in GitHub Desktop.
Save tomlobato/7b619425f8ef980251f3 to your computer and use it in GitHub Desktop.
Working deploy.rb for mina, on rails
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
task :deploy_prod => :environment do
set :rails_env, 'production'
set :restart_workers, true
set :branch, 'master'
set :deploy_to, '/path/to/PROD/rails/root'
set :test_url, "http://www.example.com"
@command_valid = true
invoke :deploy
end
task :deploy_beta => :environment do
set :rails_env, 'development'
set :restart_workers, false
set :branch, 'dev'
set :deploy_to, '/path/to/DEV/rails/root'
set :test_url, "http://DEV.example.com"
@command_valid = true
invoke :deploy
end
set :domain, 'example.com'
set :repository, 'git@github.com:user/repo'
set :user, 'server-user'
set :port, 'server-ssh-port'
shared_dirs = [
'tmp',
'log',
'public/uploads',
'public/site_download'
]
shared_files = ['public/sitemap.xml.gz']
set :shared_paths, (shared_dirs + shared_files)
task :environment do
end
task :log_prod do
queue "tail -f /var/www/site#{deploy_to}/#{current_path}/log/production.log"
end
task :log_beta do
queue "tail -f /var/www/site#{deploy_to}/#{current_path}/log/development.log"
end
task :setup => :environment do
shared_dirs.each do |shared_dir|
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/#{shared_dir}"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/#{shared_dir}"]
end
shared_files.each do |shared_file|
queue! %[touch "#{deploy_to}/#{shared_path}/#{shared_file}"]
end
queue %[echo "-----> Be sure to edit '#{shared_files.join(', ')}."]
queue %[
repo_host=`echo #{repository} | sed -e 's/.*@//g' -e 's/:.*//g'` &&
repo_port=`echo #{repository} | 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
unless @command_valid
puts "usage: mina [deploy_prod|deploy_beta] [-v]"
exit 1
end
# Put things to run locally before ssh
end
to :after_hook do
# Put things to run locally after ssh
require 'rbconfig'
is_win = RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
unless is_win
queue %[echo "-----> Testing site"]
queue %[
red=`tput setaf 1`; green=`tput setaf 2`; reset=`tput sgr0`
echo "${green}wget #{test_url}"
output=`wget -O /dev/null #{test_url} 2>&1`
if [ $? -ne 0 ]; then
echo "${red}Error accessing site:\n$output"
echo
echo "!!! ATTENTION: RIGHT NOW #{test_url} RUNNING IN #{domain}:#{deploy_to}/#{current_path} IS DOWN !!!"
else
echo "${green}ok"
fi
echo ${reset}
]
end
end
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
queue %[echo "-----> Bundle"]
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
to :launch do
queue %[
p=#{deploy_to}/#{current_path}/tmp &&
if [ ! -d $p ]; then mkdir -p $p; fi
]
queue %[echo 'Restarting app...']
queue! %[touch #{deploy_to}/#{current_path}/tmp/restart.txt]
if restart_workers
queue %[echo 'Restarting workers...']
queue! %[sudo restart_workers]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment