Created
August 20, 2015 06:00
-
-
Save sharshenov/e09344555bcd5990d1c0 to your computer and use it in GitHub Desktop.
capistrano + foreman deploy for RubyOnRails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SECRET_KEY_BASE=ahsbdjhasbjhdabks # run rake:secret to generate | |
DATABASE_URL=postgres://user:pass@dbhost/database |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'capistrano/setup' | |
require 'capistrano/deploy' | |
require 'capistrano/rvm' | |
require 'capistrano/bundler' | |
require 'capistrano/rails/assets' | |
require 'capistrano/rails/migrations' | |
require 'capistrano/rails/console' | |
require 'capistrano/foreman' | |
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/deploy.rb | |
lock '3.4.0' | |
set :application, 'PROJECT_NAME' | |
set :repo_url, '[email protected]:team/PROJECT_NAME.git' | |
set :deploy_to, "/opt/#{ fetch :application }" | |
set :linked_dirs, fetch(:linked_dirs, []).push(%w{ log tmp/pids tmp/cache tmp/sockets public/uploads }) | |
set :linked_files, fetch(:linked_files, []).push('.env') | |
set :foreman_template, 'upstart' | |
set :foreman_export_path, "/etc/init" | |
set :foreman_options, { | |
app: fetch(:application), | |
root: current_path | |
} | |
namespace :deploy do | |
task :restart do | |
on roles(:app) do |host| | |
f = "#{ fetch :foreman_export_path }/#{ fetch(:foreman_options)[:app] }.conf" | |
if test("[ -f #{f} ]") | |
invoke 'foreman:restart' | |
else | |
invoke 'foreman:setup' | |
end | |
end | |
end | |
desc "Upload .env.STAGE" | |
task :upload_env do | |
on roles(:all) do | |
upload! ".env.#{ fetch :stage }", "#{ shared_path }/.env.#{ fetch :stage }" | |
end | |
end | |
desc "Fix file permissions" | |
task :fix_file_permissions do | |
on roles(:app) do | |
execute "chown -R #{ fetch :application } #{shared_path}/tmp" | |
execute "chown -R #{ fetch :application } #{shared_path}/log" | |
execute "chown -R #{ fetch :application } #{shared_path}/public/uploads" | |
end | |
end | |
task :check_env do | |
on roles(:all) do |host| | |
f = "#{ shared_path }/.env" | |
if test("[ -f #{f} ]") | |
info "#{f} already exists on #{host}!" | |
else | |
execute "echo 'RACK_ENV=#{ fetch :stage }' > #{f}" | |
execute "echo 'PATH=/usr/local/rvm/wrappers/ruby-2.2.1:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' >> #{f}" | |
end | |
end | |
on roles(:all) do |host| | |
f = "#{ shared_path }/.env.#{ fetch :stage }" | |
if test("[ -f #{f} ]") | |
info "#{f} already exists on #{host}!" | |
else | |
invoke "deploy:upload_env" | |
end | |
end | |
end | |
before 'check:linked_files', :check_env | |
after :publishing, :fix_file_permissions | |
after :publishing, :restart | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'foreman' | |
gem 'dotenv-rails' | |
group :development do | |
gem 'capistrano' | |
gem 'capistrano-rvm' | |
gem 'capistrano-bundler' | |
gem 'capistrano-foreman' # foreman support | |
gem 'capistrano-rails' # assets & migrations | |
gem 'capistrano-rails-console' # provides rails:console task | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
On server: | |
1. Create user with the name of project: | |
``` | |
useradd PROJECT_NAME | |
``` | |
2. Install rvm & ruby | |
``` | |
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 | |
\curl -sSL https://get.rvm.io | bash -s stable --ruby=2.2.1 | |
``` | |
3. Install rails dependencies | |
``` | |
apt-get install nodejs libpq-dev | |
``` | |
4. Disable rails logging or setup logrotate to cleanup /opt/PROJECT_NAME/log/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/deploy/production.rb | |
server 'SERVER_HOSTNAME', user: 'root', roles: %w{app db web} | |
set :linked_files, fetch(:linked_files, []).push('.env.production') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment