Skip to content

Instantly share code, notes, and snippets.

@stevenscg
Last active July 12, 2019 23:50
Show Gist options
  • Save stevenscg/8176735 to your computer and use it in GitHub Desktop.
Save stevenscg/8176735 to your computer and use it in GitHub Desktop.
Capistrano deploy strategy that supports git submodules (requires Capistrano v3.1.0 or later)
# this include won't work for some reason:
# include Capistrano::Git::DefaultStrategy
module SubmoduleStrategy
# check for a .git directory
def test
test! " [ -d #{repo_path}/.git ] "
end
# same as in Capistrano::Git::DefaultStrategy
def check
test! :git, :'ls-remote', repo_url
end
def clone
git :clone, '-b', fetch(:branch), '--recursive', repo_url, repo_path
end
# same as in Capistrano::Git::DefaultStrategy
def update
git :remote, :update
end
# put the working tree in a release-branch,
# make sure the submodules are up-to-date
# and copy everything to the release path
def release
release_branch = fetch(:release_branch, File.basename(release_path))
git :checkout, '-b', release_branch, fetch(:remote_branch, "origin/#{fetch(:branch)}")
git :submodule, :update, '--init'
context.execute "rsync -ar --exclude=.git\* #{repo_path}/ #{release_path}"
end
end
@juanibiapina
Copy link

First, the include needs to be inside the module definition. Second, you might need to require 'capistrano/git'.

@snakeye
Copy link

snakeye commented Jan 17, 2014

How to use this?

@ngottlieb
Copy link

I had to use "-B" in git :checkout as I kept running into an error saying the branch already existed, but I haven't been able to track down where that problem originates.

Also, I'm curious whether or not the

git :submodule, :update, "--init"

is necessary in release given that we're doing git clone --recursive in clone.

I guess the branch must be coming from fetch(:branch) in clone. It's working now so I'm probably not going to dig deeper.

@bhaberer
Copy link

bhaberer commented Feb 6, 2014

I forked this, fixed a couple bugs and added some basic documentation feel free to merge it if you want.

@Simes
Copy link

Simes commented May 22, 2014

@ngottlieb: If you're requiring 'capistrano/git', you're causing the git rake tasks to be loaded twice. Rake appends new definitions on to the old ones rather than replacing them, so if you load the tasks twice, everything in them gets run twice.

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