# Capistrano - override default, get/copy source from local

**URL:** <https://www.ruby-forum.com/t/capistrano-override-default-get-copy-source-from-local/192257>\
**Category:** Rails\
**Created:** [July 27, 2010, 4:52am UTC](https://www.ruby-forum.com/t/capistrano-override-default-get-copy-source-from-local/192257 "2010-07-27T04:52:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 27, 2010, 4:52am UTC](https://www.ruby-forum.com/t/capistrano-override-default-get-copy-source-from-local/192257/1 "2010-07-27T04:52:10Z")

</div>

I am working on getting Capistrano running for an app but need to ask it  
to  
download source to a local directory and then send it to the deployment  
server via scp/etc (there are reasons for this I won’t get into). Below  
is  
where I am and what I am trying — if anyone has done this or can show  
me  
where I am going wrong I’d appreciate it.

I found the following info but it does not seem to work, cap deploy  
seems to  
run the same as without it:

[http://rdoc.info/rdoc/capistrano/capistrano/blob/6d540a5d4d04278312f96ae8276e9cacc2d826e5/Capistrano/Deploy/Strategy/Copy.html](http://rdoc.info/rdoc/capistrano/capistrano/blob/6d540a5d4d04278312f96ae8276e9cacc2d826e5/Capistrano/Deploy/Strategy/Copy.html)

set :copy\_strategy, :export

set :copy\_cache, true  
set :copy\_exclude, “.git/\*”

set :copy\_cache, “/tmp/caches/myapp”

Following is my deploy.rb:

set :application, “compare”  
set :repository, “removed\_email\_address@domain.invalid:structuralartistry/compare.git”

set :scm, “git”  
set :user, “myuser” # The server’s user for deploys  
set :scm\_passphrase, “mypassphrase”

set :copy\_strategy, :export  
set :copy\_cache, true  
set :copy\_exclude, “.git/\*”  
set :copy\_cache, “~/Documents/ror/deployment/compare”

set :use\_sudo, false

set :branch, “master”  
#set :deploy\_via, :remote\_cache  
set :deploy\_to, “/home/myuser/webapps/compare”

role :web, “xxx.xxx.xxx.xxx” # Your HTTP  
server,  
Apache/etc  
role :app, “xxx.xxx.xxx.xxx” # This may be the  
same  
as your `Web` server  
role :db, “xxx.xxx.xxx.xxx”, :primary =\> true # This is where Rails  
migrations will run

namespace :deploy do  
task :start, :roles =\> :app do  
run “touch #{current\_release}/tmp/restart.txt”  
end

task :stop, :roles =\> :app do  
# Do nothing.  
end

desc “Restart Application”  
task :restart, :roles =\> :app do  
run “touch #{current\_release}/tmp/restart.txt”  
end  
end

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 27, 2010, 6:36pm UTC](https://www.ruby-forum.com/t/capistrano-override-default-get-copy-source-from-local/192257/2 "2010-07-27T18:36:59Z")

</div>

I use this script, and it works for me (although, I’m sure I could  
improve it…) - hope this helps!  
================ START deploy.rb =============

set :application, “MY\_APP”  
set :user, “your\_username”  
set :admin\_runner, user  
set :rails\_env, ‘test’

set :deploy\_to, “/path/to/MY\_APP/#{application}” # this is the path on  
your server you’re deploying to  
set :deploy\_via, :copy

set :scm, :git  
set :repository, “/My/local/git/repository”  
set :branch, “master”  
set :port, YOUR\_PORT\_HERE

set :location, “your\_domain.com”

role :app, location  
role :web, location  
role :db, location, :primary =\> true

namespace :deploy do  
desc “Restarting mod\_rails with restart.txt”  
task :restart, :roles =\> :app, :except =\> { :no\_release =\> true } do  
run “touch #{current\_path}/tmp/restart.txt”  
end

[:start, :stop].each do |t|  
desc “#{t} task is a no-op with mod\_rails”  
task t, :roles =\> :app do ; end  
end

# RailsEnv is set in apache my\_app vhost config, but also set it in

environment.rb,

# so applicaitons can acccess the RAILS\_ENV constant.

task :set\_rails\_env do  
tmp = “#{current\_release}/MY\_APP/tmp/environment.rb”  
final = “#{current\_release}/MY\_APP/config/environment.rb”  
run \<\<-BASH  
echo “ENV[‘RAILS\_ENV’] ||= ‘#{rails\_env}’” \> #{tmp};  
cat #{final} \>\> #{tmp} && mv #{tmp} #{final};  
BASH  
end

end

# After the current release has been updated, re-create the symbolic

links on the server

# so that the database.yml and uploads (in shared directory) are

“understood” by the current release  
namespace(:customs) do  
task :config, :roles =\> :app do  
run \<\<-CMD  
ln -nfs #{shared\_path}/system/database.yml #{release\_path}/  
MY\_APP/config/database.yml  
CMD  
end  
task :symlink, :roles =\> :app do  
run \<\<-CMD  
ln -nfs #{shared\_path}/system/uploads #{release\_path}/MY\_APP/  
public/uploads  
CMD  
end  
end

after “deploy:update\_code”, “customs:config”  
after “deploy:symlink”,“customs:symlink”  
after “deploy”, “deploy:cleanup”  
after “deploy:finalize\_update”, “deploy:set\_rails\_env”

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 27, 2010, 6:42pm UTC](https://www.ruby-forum.com/t/capistrano-override-default-get-copy-source-from-local/192257/3 "2010-07-27T18:42:15Z")

</div>

oops…  
Replace this one line in the previous post I made to:

set :repository, “ssh://git@your\_git\_server:your\_git\_port/path/to/git/  
repository”

This will get your source from your remote git server. The previous  
one is assuming that you already have a copy of the git repository  
locally.

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 28, 2010, 5:06pm UTC](https://www.ruby-forum.com/t/capistrano-override-default-get-copy-source-from-local/192257/4 "2010-07-28T17:06:59Z")

</div>

Hi, thanks for the script, was very useful! I found that I was missing  
“set  
:deploy\_via, :copy”. Got it working on the first try.

The part at the bottom on replacing the database.yml was useful too as I  
need to both replace this and config.rb. Really minor but found a  
slightly  
simpler way to do this (which puts the callback in the first line rather  
than having to place somewhere else) when I was googling around trying  
to  
understand this:

task :after\_update\_code do

# link from our production/config directory to the release

run “ln -nfs #{deploy\_to}/production/config/database.yml  
#{release\_path}/config/database.yml”  
end

VS

task :config, :roles =\> :app do  
run \<\<-CMD  
ln -nfs #{shared\_path}/system/database.yml  
#{release\_path}/MY\_APP/config/database.yml  
CMD  
end

after “deploy:update\_code”, “customs:config”

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [July 29, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/capistrano-override-default-get-copy-source-from-local/192257/5 "2010-07-29T17:00:06Z")

</div>

Glad that helped, and thanks for the simpler script! I’ll definitely  
try it on my deploy, too.
