

AWS Tutorial 09 - Deploying Rails Apps With Capistrano Take 1
source link: https://fuzzyblog.io/blog/aws/2016/09/23/aws-tutorial-09-deploying-rails-apps-to-aws-with-capistrano-take-1.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

AWS Tutorial 09 - Deploying Rails Apps With Capistrano Take 1
Sep 23, 2016
In this tutorial we're going to use the latest version of Capistrano to deploy your Rails app onto one or more EC2 instances. I'm dividing this tutorial into two parts:
- Take 1 where a static set of boxes is encoded into your deploy/production.rb file
- Take 2 where we call AWS apis to figure out what boxes to deploy to
The reason for separating them is that take 2 will bring us much deeper into AWS API programming and concepts like tagging / IAM roles I wanted that to be highly separate from the guts of Capistrano.
Tools
In this tutorial I'm going to be specific to:
- Capistrano
- Passenger
- Bundler
That's my standard Rails stack and while other configurations will work, NGINX is a favorite of people, that's the only stack I personally understand end to end.
Getting Started - Add Capistrano to Your Gemfile
You're going to want to use Capistrano 3.6 or later. Earlier versions of Capistrano should work but, in my experience, Capistrano 3.6 is a much better release - it took me away from Vlad finally and for that I was glad.
If your Gemfile you want something like this:
group :development do
# Use Capistrano for deployment
gem 'capistrano', "3.6.1"
gem 'capistrano3-puma'
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano-rvm'
gem 'capistrano-passenger'
end
After that you want to run bundle install:
bundle install
As long as this runs correctly you should be fine to move onto the next step.
Run the Cap install Command
Capistrano requires a one time setup step. Do this now:
Cap install
This generates a file in the root of your Rails app called Capfile. Open that file and uncomment these lines:
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
Modify config/deploy.rb
The file config/deploy.rb covers the application level configuration for deployment. In this file you need to add lines like the ones below but specific to your code base / toolset:
set :application, 'banks'
set :repo_url, '[email protected]:appdatallc/banks.git'
set :ssh_options, {
forward_agent: true,
auth_methods: ["publickey"],
keys: ["#{Dir.home}/.ssh/fi_nav_sitecrawl.pem"]
}
set :deploy_to, '/var/www/apps/banks'
Modifying config/deploy/production.rb
The file config/deploy/production.rb covers the servers you are deploying to in the production environment. If you're setting up a staging server then there is a similar file in config/deploy/staging.rb.
Before you start does this section I strongly, strong, strongly advice you to read my AWS Tutorial on the SSH Config file. You can use the approach in that file to identify your boxes. If you do then this line:
server 'ec2-52-41-237-52.us-west-2.compute.amazonaws.com', user: 'ubuntu', roles: %w{web app}
would be replaced by this line:
server 'ficrawler1', roles: %w{web app}
Not only is this simpler to understand but it centralizes all your EC2 machine addresses in one place. That's a huge win.
Regardless of the approach you decide to take, you need one line in this file for each EC2 instance along with the roles the instance provides.
Doing a Deploy
Once all of this configured then you should be able to do a:
cap production deploy
to get your current code base onto all your boxes. Please keep in mind that Capistrano executes everything via SSH and it generates a long stream of commands. If you see errors then there are two things to understand:
- Not every error may be significant and need to be addressed
- If you are trying to track down an error then I would strongly recommend reducing the boxes you deploy to to just one and then redeploying. This is much easier to troubleshoot exactly what is happening
References
Here are some of the research sources I used in writing this:
Recommend
-
20
Rails Asset Pipeline Failures and Capistrano Apr 14, 2020 One of the single worst parts of being a one man show on an engineering effort is that when you hit a serious snag, well, you're fscked. I'm at the dea...
-
4
Capistrano Failure - Asset Manifest Not Created Jan 4, 2017 I setup a new Rails application using Capistrano earlier today and hit a fair number of odder than normal Capistra...
-
9
A Template for Rails 5 Apps with MySQL, Capistrano, Devise Sep 9, 2016 Despite my recent focus on Elixir, I still make most of my money doing Rails. Here's a template that Dv a...
-
11
README.md
-
11
Rails 实战 - Capistrano Capistrano 是 Ruby 编写的服务器自动化和部署工具,不是只能部署 Ruby 编写的项目,任何语言编写的项目都可以。 Rails 项目中使用
-
10
An annoying Capistrano, Unicorn and Bundler issue — Martian Chronicles, Evil Martians’ team blogIn short: always use an absolute path to Unicorn in Capistrano scripts; otherwise your application server will fail to restart after a number of d...
-
5
Capistrano and the custom maintenance page Posted: 2011-09-19 - Last updated: 2019-06-05 Tagged
-
5
Lighting fast, zero-downtime deployments with git, capistrano, nginx and Unicorn Posted: 2011-09-14 - Last updated: 2019-06-05 ...
-
7
舊專案沒做的,就把他做起來吧!一個 Rails 專案,有三台機器,每次部屬分別要手動部屬,但其實這個可以做自動化三台機器分別是不同的權限使用但都吃同一個專案,並非 load balance 那種情況這邊我只用自己的電腦來做,所以這三台機器都分...
-
12
Deploy Your Ruby on Rails App Using CapistranoBrena Monteiro on Jul 13, 2022
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK