Moving to Git SCM from SVN and Deploying with Capistrano

picture-20.png
Back in 2007 I started using git after watching linus speak about it.

Currently there are 11 domains that I’m working on and I needed a quick way to create directories to store the apps, make a blank README file, and then create the remote repository. You may find this post helpful for setting up a remote git repository with gitosis.

create_to_git.rb

#!/usr/bin/env ruby
def run_subdir(command, subdir)
  `cd #{subdir};#{command}`
end
subdir = ARGV[0]
`mkdir #{subdir}`
run_subdir("git init", subdir)
run_subdir("git remote add origin git@server.yourdomain.com:#{subdir}.git", subdir)
run_subdir("touch README.txt",subdir)
run_subdir("git add .", subdir)
run_subdir('git commit -m "added blank readme"', subdir)
run_subdir("git push origin master:refs/heads/master", subdir)

Here is my Capistrano deploy.rb thats used to deploy to a remote server from one of my git repositories. The app is a simple merb app without a DB.

deploy.rb

set :application, "yourappname"
set :scm, :git
set :repository,  "/home/git/repositories/yourappname.git"
set :deploy_to, "/www/apps/#{application}"

#put the user you use to do deployments here
set :user, "deploy"

role :app, "server.yourdomain.com"
role :web, "server.yourdomain.com"
role :db,  "server.yourdomain.com", :primary => true

#no mongrel cluster in this app
#set :mongrel_conf, "#{deploy_to}/current/config/mongrel_cluster.yml"
 set :runner, nil

task :after_update_code do
  # no need for a database in this app
  #  run "rm #{current_release}/config/database.yml"
  #  run "ln -s #{deploy_to}/#{shared_dir}/config/database.yml #{current_release}/config/database.yml"
end

# I added the task below because the cleanup task was trying to sudo which I didn't need
desc <<-DESC
  Clean up old releases without sudo. By default, the last 5 releases are kept on each \
  server (though you can change this with the keep_releases variable). All \
  other deployed revisions are removed from the servers. By default, this \
  will use sudo to clean up the old releases, but if sudo is not available \
  for your environment, set the :use_sudo variable to false instead.
DESC
task :cleanup, :except => { :no_release => true } do
  count = fetch(:keep_releases, 5).to_i
  if count >= releases.length
    logger.important "no old releases to clean up"
  else
    logger.info "keeping #{count} of #{releases.length} deployed releases"

    directories = (releases - releases.last(count)).map { |release|
      File.join(releases_path, release) }.join(" ")

    run "rm -rf #{directories}", :via => run_method
  end
end

namespace :deploy do
  desc "Merb it up"
  task :restart do
    #restart_mongrel_cluster
    run "cd #{current_path};merb -k 9090"
    # run "cd #{current_path};env EVENT=1 merb -e production -c 1" # for evented mongrel
    run "cd #{current_path};merb -e production -c 1 -p 9090" # plain old mongrel
    #run "mongrel_rails cluster::restart -C #{mongrel_conf}"
  end
end

Now a directory called websites is on my Mac Pro and MacBook, in that I have 11 subdirectories where I can do most site changes without logging on to the remote DB/Application/Web server. When you want to run 100 unique sites, everything possible must be automated.

Leave a Reply

You must be logged in to post a comment.