Skip to content

Instantly share code, notes, and snippets.

@jmarceli
Last active December 9, 2015 07:54
Show Gist options
  • Save jmarceli/9baddb0b3d6beb43cb1b to your computer and use it in GitHub Desktop.
Save jmarceli/9baddb0b3d6beb43cb1b to your computer and use it in GitHub Desktop.
mydevil.net Rails Capistrano deploy (based on CucumisSativus/Capfile), '>' used instead of '/' in file path
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
# Include tasks from other gems included in your Gemfile
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/faster_assets'
require 'capistrano/passenger'
require 'capistrano3/git-push'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
# config valid only for current version of Capistrano
lock '3.4.0'
set :deploy_user, 'USER'
set :application, 'APP_NAME'
set :repo_url, 'REPO_URL'
before :deploy, 'git:push'
# Defaults for capistrano/rails
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads', 'private')
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
set :stage, :staging
set :branch, 'GIT_BRANCH_NAME'
set :domain, 'YOUR.DOMAIN.COM' # required for automatic app restarts
# used in case we're deploying multiple versions of the same
# app side by side. Also provides quick sanity checks when looking
# at filepaths
set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}"
set :deploy_to, '/home/USERNAME/domains/DOMAIN'
# server-based syntax
server 'sX.mydevil.net', user: 'USERNAME', roles: %w{web app db}, primary: true
# dont try and infer something as important as environment from
# stage name.
set :rails_env, :production
# set RVM gemset
set :rvm_type, :user
set :rvm_ruby_version, '2.1.6@YOUR_GEMSET_NAME' # 2.1.6 is just example version of Ruby
Rake::Task["passenger:restart"].clear_actions
# MyDevil.net custom operations
namespace :deploy do
# link app to location required by mydevil.net
after :published, :symlink_to_public_ruby do
on roles(:web), in: :groups, limit: 3, wait: 10 do
execute "rm -r #{fetch(:deploy_to)}/public_ruby"
execute "ln -s #{fetch(:release_path)} #{fetch(:deploy_to)}/public_ruby"
end
end
# automatically restart app after deploy
after :published, :restart_app do
on roles(:app), in: :groups, limit: 3, wait: 10 do
execute "cd #{release_path} devil www restart #{fetch(:domain)}"
end
end
end
source 'https://rubygems.org'
group :development do
gem 'capistrano'
gem 'capistrano-passenger'
gem 'capistrano-rvm' # rvm support
gem 'capistrano-bundler' # bundle command
gem 'capistrano-rails' # assets and migrations
gem 'capistrano-faster-assets' # skips assets precompilation if not needed
gem 'capistrano3-git-push', github: 'goooseman/capistrano3-git-push' # automatic `git push` before deployment (handy feature)
end
# Some useful tasks which can be executed with
# cap STAGE_NAME setup:TASK_NAME
# eg. cap staging setup:restart_app
namespace :setup do
desc "Upload database.yml file."
task :upload_yml do
on roles(:app) do
execute "mkdir -p #{shared_path}/config"
upload! StringIO.new(File.read("config/database.yml")), "#{shared_path}/config/database.yml"
end
end
desc "Seed the database."
task :seed_db do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
execute :rake, "db:seed"
end
end
end
end
desc "Restart App."
task :restart_app do
on roles(:app) do
within "#{current_path}" do
with rails_env: :production do
#execute :rake, "db:seed"
execute "cd #{release_path} devil www restart #{fetch(:domain)}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment