🖋🖋
Navigate back to the homepage
About

Deploying a Rails App With Capistrano (Part 2)

October 1st, 2013 · 2 min read

My last post outlined using Capistrano to deploy my 6-week long Flatiron School project. The post included a brief introduction of Capistrano, including what it is and the steps to get a Capfile (where Capistrano reads instructions from) into your app.

In this post, I am going to continue to walk through a basic deploy.rb file. If you don’t know what what a deploy.rb file is you should check out the earlier post first.

1require 'bundler/capistrano' # for bundler support
2
3set :application, "set your application name here"
4set :repository, "set your repository location here"
5
6set :user, 'USERNAME'
7set :deploy_to, "/home/#{ user }/#{ application }"
8set :use_sudo, false
9
10set :scm, :git
11
12default_run_options[:pty] = true
13
14role :web, "96.8.123.64" # Your HTTP server, Apache/etc
15role :app, "96.8.123.64" # This may be the same as your `Web` server
16
17# if you want to clean up old releases on each deploy uncomment this:
18# after "deploy:restart", "deploy:cleanup"
19
20# if you're still using the script/reaper helper you will need
21# these http://github.com/rails/irs_process_scripts
22
23# If you are using Passenger mod_rails uncomment this:
24namespace :deploy do
25 task :start do ; end
26 task :stop do ; end
27 task :restart, :roles => :app, :except => { :no_release => true } do
28 run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
29 end
30end

In the previous post I left off with the set ‘:scm, :git line’, so I will start with the following line:

1default_run_options[:pty] = true

This is included to make sure Capistrano interacts properly with the shell on our server. For example, when using github your server needs to be able to access your repo. This capistrano config settting is needed for the password prompt from git to work so you can access it.

1role :web, "96.8.123.64" # Your HTTP server, Apache/etc
2role :app, "96.8.123.64" # This may be the same as your Web server

The above two lines assign the web and app ‘roles’ to specific servers. Capistrano roles allow you to have different servers handle different parts of the app (server for app, server for handling requests, and server for the database). We used one server for all aspects so assigned all roles to the same server.

Since we used Passenger, we also uncommented the :deploy namespace as is instructed in the standard deploy.rb file. This part of the file is an example of Capistrano’s task-oriented nature. Similar to rake tasks, we can create ‘cap’ tasks with the task keyword. For example, take the following 3 lines of code:

1task :hello do
2 puts "Hello World"
3end

We can run this code with the following command in the terminal:

1cap hello

We also used a common capistrano task to symlink specific files that were purposely excluded from our git repo. For example, we had an application.yml file with our Crunchbase API key and gmail username/password. Since we open sourced the repo, we had to make sure that file was included in our .gitignore. Because capistrano is getting the code from github, we have to manually link these ignored files to the server in order for them to be included. The following task helps us do just that.

1task :symlink_config, :roles => :app do
2 run "ln -nfs #{shared_path}/config/application.yml #{release_path}/config/application.yml"
3end

In order for the symlink to work though, the file must already be in the shared folder on the server. You can securely copy a file from your local machine using a variation of the following command:

1scp config/application.yml example@123.456.789.12:/home/example/example_app/shared

And that concludes my Capistrano configuration process. To deploy and update the app I simply run the following command:

1cap deploy

If you’re interested in learning more about deploying a rails app with Capistrano, there is a good railscast and also a nice wiki on Github.

Stay in touch

I send a monthly update with my favorite articles, quotes, and new ideas

You may also like

Deploying a Rails App With Capistrano (Part 1)

September 9th, 2013 · 2 min read

Tools to Stay Focused

September 4th, 2013 · 1 min read
© 2013–2020 Adam Waxman
Link to $https://twitter.com/ajwaxmanLink to $https://unsplash.com/awaxman11Link to $https://github.com/awaxman11Link to $https://instagram.com/awaxman11Link to $https://www.linkedin.com/in/adamjwaxman/Link to $https://dribbble.com/ajwaxman