How to Configure Capistrano to Deploy from Local Git Repository?

Ruby on-RailsGitCapistrano

Ruby on-Rails Problem Overview


What changes do I need to make to the deploy.rb file below to make it deploy my app from a local git repo? If I can't deploy from a local repo, can I have capistrano use the working copying instead?

set :application, "my_app"
set :repository, "."
set :local_repository, "file:///path/to/application/.git"
set :deploy_to, "/data/www/apps/#{application}"
set :deploy_via, :copy
set :copy_cache, true
set :user, "dane"
set :use_sudo, false

set :scm, :git # Should I change this to :none
set :branch, "master"

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

That's easy:

set :scm, :none
set :repository, "."
set :deploy_via, :copy

Simply run Capistrano from the root of your project.

Solution 2 - Ruby on-Rails

The deploy_via, :copy has been dropped in version 3.

https://github.com/capistrano/capistrano/issues/695

In most cases you should have your code in a online repository like github or bitbucket, and then you just have to set this line in your deploy.rb file:

set :repo_url, '[email protected]:my_account/my_project.git'

Though if you happen to have a repository on the remote server that you are deploying too, then you would change that line in your deploy.rb file to be this:

set :repo_url, 'file:///home/deploy/bare_repo/my_project.git'

Keep in mind that the three forward slashes are important since the file:// tells capistrano that you are looking for a file, and the preceding slash is needed to point to a root path which would be something like /home/deploy/bare_repo/my_project.git.

Solution 3 - Ruby on-Rails

set :repository, 'file:///path/to/your/git_repository'
set :local_repository, "file://."
set :scm, :git
# set :deploy_via, :copy # you must comment it

Solution 4 - Ruby on-Rails

Capistrano 3 solution which is running for me:

  before :deploy, :deploy_from_local_repo
  
  task :deploy_from_local_repo do
    set :repo_url,  "file:///tmp/.git"
    run_locally do
      execute "tar -zcvf /tmp/repo.tgz .git"
    end
    on roles(:all) do
      upload! '/tmp/repo.tgz', '/tmp/repo.tgz'
      execute 'tar -zxvf /tmp/repo.tgz -C /tmp'
    end
  end

Before deploying you are uploading a tar.gz file to the server, unzip and finally reset the :repo_url to file mode.

Take care to remove the pervious repo:

task :remove_repo do
  on roles(:all) do
    execute "rm -r #{repo_path}"
  end
end

Solution 5 - Ruby on-Rails

I used a combination of @Ariejan and @HungYuHei answers which worked for me.

set :deploy_via, :copy
set :use_sudo, false    
set :scm, "git"
set :repository, "."
set :local_repository, "."
set :branch, "master"

If you use local copy (and don't have the project on Github), then it is also wise to disable :check_revision task in your deploy.rb which checks whether remote is in sync with local git.

Solution 6 - Ruby on-Rails

normally deploy via copy is super slow. but copy_cache only available if scm is NOT none (sync uses scm) this means that deploy from working copy can only be done with sluggish copy. I managed to find a fast setup for copy deploy from local repo that is fast. You still need to locally commit changes though but do not need to push them.

set :scm, "git"
set :local_repository, "file://."
set :deploy_via, :copy
# cache only seems to work if use scm
set :copy_cache, true
set :copy_via, :scp
set :copy_exclude, [".zeus*", ".bundle", ".git", "tmp/*", "doc", "log/*", "fixtures/*"]

Unfortunately it sometimes breaks mysteriously with:

fatal: Could not parse object 'c438b9d1242cb311be43d681e3f89bc486d748ed'.`

Ideally syncing local cache should be implemented even if no scm is used for deploy from working copy to work. great feature to add to capistrano

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaneSView Question on Stackoverflow
Solution 1 - Ruby on-RailsAriejanView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMaximusView Answer on Stackoverflow
Solution 3 - Ruby on-RailsHungYuHeiView Answer on Stackoverflow
Solution 4 - Ruby on-RailsjlebrijoView Answer on Stackoverflow
Solution 5 - Ruby on-RailsrohitmishraView Answer on Stackoverflow
Solution 6 - Ruby on-RailsViktor TrónView Answer on Stackoverflow