Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails Capistrano deploy using git as our deployment strategy. You'll need git version >=1.5.6.6 on your server for this to work.
# you'd obviously have more settings somewhere
set :scm, :git
set :repository, "git@github.com:defunkt/github.git"
set :branch, "origin/master"
set :migrate_target, :current # this tells capistrano where to run the migration. otherwise it would try to use the latest release directory (/path/to/app/releases/2012XXXXXXXXX)
set :use_sudo, false
set :ssh_options, {:forward_agent => true} # so you can checkout the git repo without giving the server access to the repo
set :rails_env, 'production'
# These are here to override the defaults by cap
# so that code that uses these variables never tries to access the wrong directory
set(:latest_release) { fetch(:current_path) }
set(:release_path) { fetch(:current_path) }
set(:current_release) { fetch(:current_path) }
# Same as above so that code that relys on getting the revision value gets the correct one.
set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip }
default_environment["RAILS_ENV"] = 'production'
default_run_options[:shell] = 'bash'
namespace :deploy do
desc "Setup a GitHub-style deployment."
task :setup, :except => { :no_release => true } do
# This is all pretty identical to the cap original for this
dirs = [deploy_to, shared_path]
dirs += shared_children.map { |d| File.join(shared_path, d) }
run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
run "git clone #{repository} #{current_path}" # only thing we really added
end
# We've overidden this because it normally does the symlinking which we don't want to do
task :update do
transaction do
update_code
end
end
desc "Update the deployed code."
task :update_code, :except => { :no_release => true } do
run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
finalize_update
end
desc "Deploy the code and update the database (overwritten to avoid symlink)"
task :migrations do
transaction do
update_code
end
migrate
restart
end
desc "Restart passenger with restart.txt"
task :restart, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end
# We override all of this so that rollback works properly with the this style of deployment
namespace :rollback do
desc "Moves the repo back to the previous version of HEAD"
task :repo, :except => { :no_release => true } do
set :branch, "HEAD@{1}"
deploy.default
end
desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
task :cleanup, :except => { :no_release => true } do
run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
end
desc "Rolls back to the previously deployed version."
task :default do
rollback.repo
rollback.cleanup
end
end
end
def run_rake(cmd)
run "cd #{current_path}; #{rake} #{cmd}"
end
@lokesh-webonise

This comment has been minimized.

Copy link

@lokesh-webonise lokesh-webonise commented Dec 31, 2013

HI,

We are using this script & its working fine on deploy command, but when we are trying to rollback , its deleting old references & not rolling back code base

can you suggest what could be wrong!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment