Skip to content

Instantly share code, notes, and snippets.

@corny
Created November 14, 2013 01:31
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save corny/7459729 to your computer and use it in GitHub Desktop.
Save corny/7459729 to your computer and use it in GitHub Desktop.
Capistrano 3 with Git Submodules
# Save this file as lib/capistrano/tasks/git.cap
namespace :git do
desc 'Copy repo to releases'
task create_release: :'git:update' do
on roles(:all) do
with fetch(:git_environmental_variables) do
within repo_path do
execute :git, :clone, '-b', fetch(:branch), '--recursive', '.', release_path
end
end
end
end
end
@ncswenson
Copy link

Thank you for posting this.

I am also trying to support submodules in my capistrano 3 deployment. I have placed the script you created above in /capistrano-3.0.0/lib/capistrano/tasks/git.cap but on deploy it does not seem to pull the submodule. Is there anything else I need to setup for this to work? Thanks so much!

@ddarbyson
Copy link

put it inside your actual project

/home/username/mywebsite/lib/capistrano/tasks/git.cap

I'm using 3.0.1 and it's working

@dtia
Copy link

dtia commented Jan 30, 2014

I tried putting it in git.cap under /home/username/mywebsite/lib/capistrano/tasks/git.cap as you suggested but it was complaining about the release path already existing.

I then moved this code into my deploy.rb adding it as another task and it worked fine after that. Thanks!

@kovshenin
Copy link

Works for me, thanks a lot!

@fellipebrito
Copy link

Great job man, it works like a charm here. So bad that capistrano does not have an way to do it on the configuration.

I will look carefully in the next release pull requests and see if I can help with that.

@kjprince
Copy link

I cannot get this to work on Cap V3.1.0

It's failing when trying to symlink the release:

Tasks: TOP => git:create_release
(See full trace by running task with --trace)

Anyone have this problem?

@ryancheung
Copy link

I get this to work on Cap v3.2

@liviucmg
Copy link

liviucmg commented Sep 1, 2014

The config worked for me on Capistrano v.3.2.1 after a few tweaks:

# Overwrite the 'deploy:updating' task.
Rake::Task["deploy:updating"].clear_actions
namespace :deploy do

  desc 'Copy repo to releases, along with submodules'
  task updating: :'git:update' do
    on roles(:all) do
      with fetch(:git_environmental_variables) do
        within repo_path do
          # We'll be using 'git clone' instead of 'git archive' (what Capistrano uses), since the latter doesn't fetch submodules.
          # Use --recursive to fetch submodules as well.
          execute :git, :clone, '-b', fetch(:branch), '--recursive', '.', release_path
          # Delete .git* files. We don't need them, and they can be a security threat.
          execute "find #{release_path} \\( -name '.git' -o -name '.gitignore' -o -name '.gitmodules' \\) -exec rm -rf {} \\; > /dev/null 2>&1", raise_on_non_zero_exit: false
        end
      end
    end
  end

end

@efueyo
Copy link

efueyo commented Oct 10, 2014

I added this git.cap and it worked like a charm, however I then had to add
after "deploy", "deploy:restart"
As deploy:restart stopped being invoked by default after git.cap was introduced
Did I miss something?

@florentdestremau
Copy link

@liviucmg thanks i added your script directly in my deploy.rb and it works perfectly !

@dkarter
Copy link

dkarter commented May 4, 2015

@arthurbryant
Copy link

Thank you. After deployed I found I 'cd' to the directory of submodule and found that 'git branch' point to no branch. So I have to add another task to force it checkout to master branch. Any ideas?

 desc 'Checkout master'
  task :checkout_master do
      on roles(:app) do |host|
          within current_path do
              execute "cd #{app_path}/current/public/pages && git checkout master && cd ../../"
          end
      end
  end

@zx1986
Copy link

zx1986 commented Aug 17, 2016

I got NameError: uninitialized constant Capistrano::Git in Capistrano 3.6.0

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