Build Automation #6 - Ruby on Rails and Capistrano
Build automation with Rails is the sweetest and the easiest thing on earth. OK I shouldn’t call it build automation rather deployment automation. As far as Rails is concerned your source is your build. Makes life simple, you don’t need to bother about two different structures. More than that, the structure is enforced and you dont need to worry about each and every developer creating their own structure and packaging as in Java.
Rake is a build language developed on Ruby. Its purpose is to automate the build process similar to Ant. Read Martin Fowlder’s “Using the Rake build language” for more details. Capistrano adds additional tasks that rake can execute to deploy a rails application. Assuming that you have installed ruby and rails, create a HelloWorld rails application. Following are the quick steps to automating your HelloWorld Rails application.
- Step 1 - Install Capistrano
c:\>gem install capistrano
- Step 2 - Apply capistrano to your application. Assuming its a HelloWorld application in rails issue the following command.
c:\HelloWorld\>cap --aply-to c:\HelloWorld HelloWorldThe arguments after the –aply-to in the above command are the path to the application and the application name. Once you execute the above command, Capistrano adds a file called capistrano.rake under the lib folder. This file contains all the tasks necessary for deployment.
- Step 3 - Edit deploy.rb file under the config folder and add the following lines
# Set the application name
set :application, “HelloWorld”
# Specify the source control repository location, say Subversion
set :repository, “http://compassites-server1:9000/repository/HelloWorld/trunk”
# Set the roles and which server would play that role. In our case, lets say all of them are the same unix box
role :app, “compassites-server1.compassites.net”
role :web, “compassites-server1.compassites.net”
role :db, “compassites-server1.compassites.net”
# Set the absolute path of the server. Assuming you are running your application using Apache web server.
set :deploy_to, “/apache2/htdocs/HelloWorld”
- Step 4 - Setup the folders. Rake deploys the application with the release date and version number as folder under the folder called release. This helps in rolling back the application to the previous release easily if need be. It also creates a current folder which is a sym link to the current release. The apache configuration refers to this directory to serve the application. To set up these directories, execute the following command.
rake remote:exec ACTION=setupThe above command will request for user name/password in the server and create and copy necessary files.
- Step 5 - Add task to restar the web server. Assuming that you have configured the apache to run your application, you just have to tell rake to restart the web server after the deployment happens. So override the default restart task in the deploy.rb file like below.
task :restart, :roles => :app do
sudo “apachectl graceful”
end
- Step 6 - You are all set. Execute the below command
c:\HelloWorld\>rake deployThe above command downloads the latest code from the repository under the date-revision number folder under release folder, sets the current folder link to point to this folder, runs the unit tests and restarts the server.
You can find detailed explanation and advanced setup information in the capistrano manual.










