Deploying two rails apps under the same account using SVN and Capistrano on hostingrails.com

Posted on 28 March 2007

This is a step by step guide of how I setup my SVN repository and Capistrano deploy file on hostingrails.com

Please keep in mind this setup is just one way you can get an app deployed and is by no means how hostingrails recommend you use Capistrano. I also recommend you get your site up and running without Capistrano deployment first.

The method outlined below allows me to run a test and live app side by side using one set of code and one repository for easy management. It means that I can test my changes on the server on my test app before making them live.

If you are interested in setting up a test and live version of your app like I have, then you should also check out the hostingrails.com article about running multiple rails apps under the same account.

My setup is such that all of my apps are stored in a single SVN repository. Each app has its' own directory in the branches and tags directories.

Please note that I don't run my database locally, this means it is shared by anyone who checks out the code.

This setup requires me to commit file uploads to the repository however to ensure my file uploads are not deployed live I create a tag of the code I want to deploy, removing any upload directories in the process.

Setup an SVN repository

I'm going to assume you are familiar with what SVN is and the benefits of using it for managing your code.

Here's a quick step by step guide to get going:

  1. Create a directory where your SVN repository will sit in your shared space.

    mkdir ~/svn/
    svnadmin create ~/svn/
  2. Set up the branches, tags and trunk directories in your repository

    mkdir ~/svn/branches
    mkdir ~/svn/tags
    mkdir ~/svn/trunk
  3. Create your rails app in the trunk folder or move your rails app into the trunk folder.

    To move your app first upload it to a temporary directory and then do an import, for example:

    svn import ~/tmp/yourwebsite file:///home/yourusername/svn/trunk/yourwebsite -m "initial project import"
  4. Create an app folder inside tags

    mkdir ~/svn/tags/yourwebsite
  5. Now you can check out your app from your repository to create your working copy.

SVN GUI

Like a lot of Rails developers I'm developing on OS X. To access my SVN repository I found Smart SVN to be the best client so far. This is because it seems to have no problems with an svn+ssh style repository url.

Your repository url will be:

svn+ssh://yourusername@yourprimarywebsitedomain/home/yourusername/svn/

Capistrano

Again I'm going to assume you know all about Capistrano and why it's so good.

First i'll give you the steps to get it setup and then i'll explain how to use the deploy file and what it does.

  1. Setup Capistrano in your app locally by running the following.

    cap --apply-to ~/your/website/location
    
  2. Rename the generated /config/deploy.rb to back it up and place my amended deploy file in /config.

    You will need to fill in the specifics for your app which are:

    • Domain of the website to deploy
    • Domain of your primary website for your account
    • Your username
    • Your password
    • Any directories your app uploads to
    • Your mongrel port
  3. Duplicate your /config/environment.rb file and name it environment_test.rb

    Make the following change:

    ENV['RAILS_ENV'] ||= 'test_production'

    This environment file will be used to point your test app to a test database. The deploy file will automatically rename it and replace the default one depending on which site you are deploying.

  4. Duplicate /config/environments/production.rb and name it test_production.rb

  5. Edit /config/database.yml to add in the database config for your test app:

    test_production:
    adapter: mysql
    database: your-test-db-name
    username: username
    password: ********

    Note that if you are using migrations that the database they are run on on deploy is automatically set in the script.

  6. As per the multiple sites under one account article, you will need to edit /public/.htaccess and above the rewrite rules for rails enter:

    RewriteCond %{REQUEST_URI} ^/test.*
    RewriteRule .* - [L]
  7. Setup your 'deploy to' location on your server for your test app by commenting out the code around the 'set :deploy_to' line to just leave:

    set :deploy_to, "/home/yourusername/#{application}-test"

    Then run:

    rake remote:setup

    This will setup a location for your test app to be deployed to.

    Then comment the code to just leave:

    set :deploy_to, "/home/yourusername/#{application}"

    And again run:

    rake remote:setup

    This sets up the live app deploy location.

  8. Setup a subdomain for your test app to point to ~/public_html/test. Note that you wont need to setup a symlink for the test app as it is created in the deploy file when you deploy your app live.

    Now you can deploy your app for testing with:

    cap deploy

    And you can deploy your app live with:

    cap deploy -S newtag=1.0

    Where the number passed is your own arbitrary version number

So what does all this do?

When you deploy with 'cap deploy' the tag 'test' is updated with the code in the trunk. This tag is then deployed to the test location which is set in the deploy file.

When you deploy with 'cap deploy -S newtag=1.0' a new tag called 1.0 will be created from the code in the 'test' tag. This tag will then be deployed live.

Whether you deploy to go live or for testing, a tag is created and in that process the upload directories you have specified will be deleted from it. The reason for this is to prevent uploads being sent up to your server. They aren't required because they wont sync with your database and if they are large files it will become time consuming.

In the deploy file symlinks are setup for all the upload directories so that each version you deploy can access any uploads that have been made.

To get the deploy file to work with two apps Rails requires a custom environment. This is how Rails can work off of a separate database. The deploy file just checks for the presence of the newtag variable when you call Capistrano so that it knows which app to deploy.

I have my live site running with just one mongrel instance. However, my test app is using fast cgi so I have still made the necessary changes to my dispatch files.

About Paul

Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.

Follow Paul on Twitter

Email: paulsturgess [at] gmail.com

Read more articles in the archive →

Comments...

  • Great article, I will try something like that. I did get a little lost on set 3 and again on step 7.

    Bruno at 28 Mar 07 at 15:42

  • The easiest way to complete step 3 is to upload your app to ~/tmp/yourappname on your server and then run that 'svn import' command.

    For Capistrano Step 7 - just amend your deploy.rb file so there is only one 'set :deploy_to' line.

    This is so that when you run 'rake remote:setup' it knows which location to setup.

    You will need to do this twice to setup both locations - once for the live app and again for the test app.

    Paul at 20 Apr 07 at 06:35

  • can u tell me lkove exaple where they are suinga raisl aplication for sendiong the sm s

    or may be some graphical represntaton

    or even some other links for advanced undersantanign will alos be helpful

    Piyush GUpta at 11 Sep 07 at 08:52

Got something to say?