Skip to content

Instantly share code, notes, and snippets.

@guiman
Created May 2, 2015 21:08
Show Gist options
  • Save guiman/b8958e38968a7d184a0d to your computer and use it in GitHub Desktop.
Save guiman/b8958e38968a7d184a0d to your computer and use it in GitHub Desktop.
Example Mina script for Rails app and Unicorn
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv'
set :domain, 'your_server'
set :user, 'ubuntu'
set :deploy_to, '/path/to/app'
set :app_path, "#{deploy_to}/#{current_path}"
set :repository, 'repo'
set :branch, 'master'
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log']
set :forward_agent, true # solving the problem to bundle private gems
task :environment do
invoke :'rbenv:load'
invoke :'important_variables'
end
task :important_variables => :environment do
queue! %[source /home/ubuntu/.secret_key_base]
queue! %[source /home/ubuntu/.mysql]
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! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue %[echo "-----> Be sure to edit '#{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/secrets.yml'."]
end
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'deploy:cleanup'
to :launch do
queue "mkdir -p #{deploy_to}/#{current_path}/tmp/"
queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"
end
end
end
# Unicorn
# ==============================================================================
namespace :unicorn do
set :unicorn_pid, "#{app_path}/tmp/unicorn.pid"
set :start_unicorn, %{
cd #{app_path}
bundle exec unicorn -c #{app_path}/config/unicorn.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 => :environment 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment