diff --git a/README.md b/README.md index e03f9f2..002d329 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,13 @@ system that will actually be deployed before checking the tree for changes. Know this ahead of time as this may affect how you deal with your deployment branches. +Setting the Remote +------------------ +By default, Capistrano Deploytags will use the first remote in the list returned +by `git remote`. If you prefer to use a different remote, then you may change the +`:git_remote` setting from your `deploy.rb`, the stage, or on the command line with +`-S git_remote=your-remote`. + Working on Your Deployment Scripts ---------------------------------- Because you must have a clean tree to deploy, working on your deployment diff --git a/lib/capistrano/deploy_tags.rb b/lib/capistrano/deploy_tags.rb index 89204eb..7a92763 100644 --- a/lib/capistrano/deploy_tags.rb +++ b/lib/capistrano/deploy_tags.rb @@ -15,7 +15,7 @@ module Capistrano def validate_git_vars unless exists?(:branch) && exists?(:stage) - logger.log Capistrano::Logger::IMPORTANT, "Capistrano Deploytags requires that :branch and :stage be defined." + logger.log Capistrano::Logger::IMPORTANT, 'Capistrano Deploytags requires that :branch and :stage be defined.' raise 'define :branch and :stage' end end @@ -28,12 +28,16 @@ module Capistrano !`git remote`.strip.empty? end + def remote + exists?(:git_remote) ? git_remote : `git remote`.strip.split(/\n/).first + end + def self.load_into(configuration) configuration.load do - before "deploy", 'git:prepare_tree' - before "deploy:migrations", 'git:prepare_tree' - after "deploy", 'git:tagdeploy' - after "deploy:migrations", 'git:tagdeploy' + before 'deploy', 'git:prepare_tree' + before 'deploy:migrations', 'git:prepare_tree' + after 'deploy', 'git:tagdeploy' + after 'deploy:migrations', 'git:tagdeploy' desc 'prepare git tree so we can tag on successful deployment' namespace :git do @@ -49,8 +53,9 @@ module Capistrano raise 'Dirty git tree' end - cdt.safe_run "git", "checkout", branch - cdt.safe_run "git", "pull", "origin", branch if cdt.has_remote? + cdt.safe_run 'git', 'checkout', branch + logger.log Capistrano::Logger::IMPORTANT, "Pulling from #{branch}" + cdt.safe_run 'git', 'pull', cdt.remote, branch if cdt.has_remote? end desc 'add git tags for each successful deployment' @@ -63,8 +68,8 @@ module Capistrano logger.log Capistrano::Logger::INFO, "Tagging #{current_sha} for deployment" tag_user = (ENV['USER'] || ENV['USERNAME']).strip - cdt.safe_run "git", "tag", "-a", cdt.git_tag_for(stage), "-m", "#{tag_user} deployed #{current_sha} to #{stage}" - cdt.safe_run "git", "push", "--tags" if cdt.has_remote? + cdt.safe_run 'git', 'tag', '-a', cdt.git_tag_for(stage), '-m', "#{tag_user} deployed #{current_sha} to #{stage}" + cdt.safe_run 'git', 'push', '--tags' if cdt.has_remote? end end diff --git a/spec/capistrano_deploy_tags_spec.rb b/spec/capistrano_deploy_tags_spec.rb index 3bac851..a94276f 100644 --- a/spec/capistrano_deploy_tags_spec.rb +++ b/spec/capistrano_deploy_tags_spec.rb @@ -32,28 +32,50 @@ describe Capistrano::DeployTags do end context "with a clean git tree" do + before :each do + configuration.set(:branch, 'master') + configuration.set(:stage, 'test') + end + it "raises an error if :stage or :branch are undefined" do with_clean_repo do + configuration.unset(:branch) + configuration.unset(:stage) expect { configuration.find_and_execute_task('git:prepare_tree') }.to raise_error('define :branch and :stage') end end it "does not raise an error when run from a clean tree" do with_clean_repo do - configuration.set(:branch, 'master') - configuration.set(:stage, 'test') expect { configuration.find_and_execute_task('git:prepare_tree') }.to_not raise_error end end - end - it "does not run when :no_deploytags is defined by (i.e. by the stage)" do - with_clean_repo do - configuration.set(:branch, 'master') - configuration.set(:stage, 'test') - configuration.set(:no_deploytags, true) - configuration.cdt.should_not_receive(:validate_git_vars) - expect { configuration.find_and_execute_task('git:prepare_tree') }.to_not raise_error + it "does not run when :no_deploytags is defined by (i.e. by the stage)" do + with_clean_repo do + configuration.set(:no_deploytags, true) + configuration.cdt.should_not_receive(:validate_git_vars) + configuration.find_and_execute_task('git:prepare_tree') + end + end + + it "uses a different remote when one is defined" do + with_clean_repo do + system('git remote add nowhere git@example.com:nowhere') + configuration.set(:git_remote, 'nowhere') + configuration.cdt.should_receive(:safe_run).with('git', 'checkout', 'master') + configuration.cdt.should_receive(:safe_run).with('git', 'pull', 'nowhere', 'master') + configuration.find_and_execute_task('git:prepare_tree') + end + end + + it "uses the first remote when one is not specified" do + with_clean_repo do + system('git remote add somewhere git@example.com:somewhere') + configuration.cdt.should_receive(:safe_run).with('git', 'checkout', 'master') + configuration.cdt.should_receive(:safe_run).with('git', 'pull', 'somewhere', 'master') + configuration.find_and_execute_task('git:prepare_tree') + end end end end