Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mina Example Script using Roles (in whenever) as used in Capistrano
# Custom file for whenever.rb with roles, https://gist.github.com/priyank-gupta/e15b5dab570e54cde2ee
# to deploy on staging_1
mina deploy on=staging_1
# to update whenever on staging_2
mina whenever:update on=staging_2
# to restart delayed_job on staging_2
mina delayed_job:restart on=staging_2
# config/deploy.rb
# custom file for whenever.rb with roles, https://gist.github.com/priyank-gupta/e15b5dab570e54cde2ee
require 'mina/multistage'
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'yaml'
# require custom file for whenever.rb with roles, https://gist.github.com/priyank-gupta/e15b5dab570e54cde2ee
require_relative '../lib/mina/whenever'
desc 'Default Setup.'
task :defaults do
set_default :term_mode, :pretty
set_default :rails_env, 'staging'
set_default :branch, 'master'
end
set :shared_paths, ['log', 'config/secrets.yml', 'config/aws.yml', 'config/database.yml', 'tmp', 'public/assets']
set :asset_paths, ['vendor/assets/', 'app/assets/', 'app/assets/fonts/']
set :default_server, :staging
set :server, ENV['on'] || default_server
invoke :"env:#{server}"
set :app_path, lambda { "#{deploy_to}/#{current_path}" }
# custom setup task
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! %[mkdir -p "#{deploy_to}/#{shared_path}/tmp"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/public/assets"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/public/assets"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/database.yml'."]
end
# custom deploy task
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
to :prepare do
end
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'deploy:cleanup'
to :launch do
queue "mkdir -p #{app_path}/tmp/"
invoke :'nginx:restart'
invoke :'whenever:update'
if role?(:admin) || role?(:staging)
invoke :'delayed_job:restart'
end
end
end
end
# Custom nginx tasks
# Module for the same is also available, mina-nginx
namespace :nginx do
%w(stop start restart reload status).each do |action|
desc "#{action.capitalize} Nginx"
task action.to_sym do
queue %{echo "-----> #{action.capitalize} Nginx"}
set :term_mode, :system
queue "sudo service nginx #{action}"
end
end
end
# Custom Delayed Job tasks
# Module is also available
namespace :delayed_job do
%w(stop start restart).each do |action|
desc "#{action.capitalize} Delayed Job"
task action.to_sym do
queue %{echo "-----> #{action.capitalize} Delayed Job"}
queue "cd #{app_path} ; RAILS_ENV=#{rails_env} ./bin/delayed_job #{action}"
end
end
end
def role?(role)
roles.include?(role)
end
# config/deploy/staging.rb
# custom file for whenever.rb with roles, https://gist.github.com/priyank-gupta/e15b5dab570e54cde2ee
namespace :env do
task :staging_1 => [:environment] do
set :domain, 'x.x.x.x' # staging IP
set :deploy_to, '/path/to/your_site'
set :user, 'deploy'
set :deploy_server, 'staging_1' # just a handy name of the server
set :rails_env, 'staging'
set :branch, 'master'
set :repository, 'git@github.com:your_repo/your_repo.git'
set :roles, ->{ [:app, :db, :staging] } # to be used in whenever script
invoke :defaults # load rest of the config
end
end
namespace :env do
task :staging_2 => [:environment] do
set :domain, 'x.x.x.x' # staging2
set :deploy_to, '/path/to/your_site'
set :user, 'deploy'
set :deploy_server, 'staging_2' # just a handy name of the server
set :rails_env, 'staging'
set :branch, 'master_2'
set :repository, 'git@github.com:your_repo/your_repo.git'
invoke :defaults # load rest of the config
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment