Deploying Your Ruby on Rails Application Using Git and Capistrano

Hi all! It’s been a while since I have posted as I have been on holidays and have been rather lazy! This post is going to explain the arduous task of deploying your Ruby on Rails application in as much depth as possible to make your lives easier. It took me quite a few days to get this going, however now that I have done it once it is going to be a simple task to deploy other applications.

As the title mentions we will be using Git and Capistrano. If you prefer to use Subversion there are only a few changes that need to be made and you should be able to find them if you Google it.

Firstly, you require a Git repository. As you may expect, I am going to recommend using GitHub. I have only recently started using it and haven’t looked back. It’s reasonably priced and easy to use.

Secondly, you need to install Capistrano by running the following command.

gem install capistrano

Now in the console, set the directory to the root of your app and type the following

capify .

This will create two files for required for our deployment. Capfile located in the root directory and deploy.rb located in the Config folder.
The deploy.rb holds all the settings for deployment. Use the following as a template and insert your details where necessary

set :user, 'castitt'  # Your hosting account's username
set :domain, 'server.domain.com'  # Hosting servername where your account is located
set :project, 'ProjectName'  # Your application as its called in the repository
set :application, 'app.location.com'  # Your app's location (domain or subdomain)
set :applicationdir, "/home/#{user}/#{application}"  # The location of your application on your hosting (my differ for each hosting provider)
# version control config
set :scm, 'git'
set :repository,  "git@github.com:username/Project.git" # Your git repository location
set :deploy_via, :remote_cache
set :git_enable_submodules, 1 # if you have vendored rails
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true
# roles (servers)
role :web, domain
role :app, domain
role :db,  domain, :primary => true
# deploy config
set :deploy_to, applicationdir # deploy to directory set above
set :deploy_via, :export
# additional settings
default_run_options[:pty] = true  # Forgo errors when deploying from windows
set :chmod755, "app config db lib public vendor script script/* public/disp*"
set :use_sudo, false

Many of the settings above are self explanatory. Those that I haven’t explained aren’t worth worrying about. Now it’s time to deploy our app. Go to the console again at the root of our application and perform the following commands.

#setup the application directory on the server ready for deployment
cap deploy:setup

#run this the first time you are deploying the project
cap deploy:cold

#from then on, run one of the following
cap deploy
cap deploy:migrate

You should now have your Ruby on Rails application setup and ready to go. If you run into any issues let me know and hopefully I can help you. There are also many maintenance commands you can use through Capistrano.

#stops app for maintenance
cap deploy:web:disable
#starts app again
cap deploy:web:enable
#rollback a broken deployment
cap deploy:rollback

And that’s everything for this post. Hopefully your up is working perfectly. Thanks for reading and I will see you next time.

Thanks,

Cameron


One Response to “Deploying Your Ruby on Rails Application Using Git and Capistrano”

Leave a Reply

Spam protection by WP Captcha-Free