Use a configurable remote, or default to the first in git remote list.

Also, don't use double quotes where we don't need them.
This commit is contained in:
Karl Matthias
2013-08-27 15:22:53 +01:00
parent e934cead87
commit b0133c88c0
3 changed files with 53 additions and 19 deletions
+7
View File
@@ -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
+14 -9
View File
@@ -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
+32 -10
View File
@@ -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