Module: Capistrano::DeployTags

Defined in:
lib/capistrano/deploy_tags.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/capistrano/deploy_tags.rb', line 42

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'

    desc 'prepare git tree so we can tag on successful deployment'
    namespace :git do
      task :prepare_tree, :except => { :no_release => true } do
        next if fetch(:no_deploytags, false)

        cdt.validate_git_vars

        logger.log Capistrano::Logger::IMPORTANT, "Preparing to deploy HEAD from branch '#{branch}' to '#{stage}'"

        if cdt.pending_git_changes?
          logger.log Capistrano::Logger::IMPORTANT, "Whoa there, partner. Dirty trees can't deploy. Git yerself clean first."
          raise 'Dirty git tree'
        end

        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'
      task :tagdeploy, :except => { :no_release => true } do
        next if fetch(:no_deploytags, false)

        cdt.validate_git_vars

        current_sha = `git rev-parse #{branch} HEAD`.strip[0..8]
        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?
      end
    end

  end
end

Instance Method Details

#exec_success?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/capistrano/deploy_tags.rb', line 19

def exec_success?
  $?.success?
end

#git_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/capistrano/deploy_tags.rb', line 30

def git_tag?(tag)
  !`git tag -l #{tag}`.strip.empty?
end

#git_tag_for(stage) ⇒ Object



11
12
13
# File 'lib/capistrano/deploy_tags.rb', line 11

def git_tag_for(stage)
  "#{stage}-#{Time.new.utc.strftime('%Y.%m.%d-%H%M%S-utc')}"
end

#has_remote?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/capistrano/deploy_tags.rb', line 34

def has_remote?
  !`git remote`.strip.empty?
end

#pending_git_changes?Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
# File 'lib/capistrano/deploy_tags.rb', line 3

def pending_git_changes?
  # Do we have any changes vs HEAD on deployment branch?
  `git fetch #{remote}`.tap do |output|
    return !(`git diff #{branch} --shortstat`.strip.empty?) if exec_success?
    raise "'git fetch #{remote}' failed:\n #{output}"
  end
end

#remoteObject



38
39
40
# File 'lib/capistrano/deploy_tags.rb', line 38

def remote
  exists?(:git_remote) ? git_remote : `git remote`.strip.split(/\n/).first
end

#safe_run(*args) ⇒ Object



15
16
17
# File 'lib/capistrano/deploy_tags.rb', line 15

def safe_run(*args)
  raise "#{args.join(" ")} failed!" unless system(*args)
end

#validate_git_varsObject



23
24
25
26
27
28
# File 'lib/capistrano/deploy_tags.rb', line 23

def validate_git_vars
  unless exists?(:branch) && exists?(:stage)
    logger.log Capistrano::Logger::IMPORTANT, 'Capistrano Deploytags requires that :branch and :stage be defined.'
    raise 'define :branch and :stage'
  end
end