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



35
36
37
38
39
40
41
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
# File 'lib/capistrano/deploy_tags.rb', line 35

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

#git_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#git_tag_for(stage) ⇒ Object



8
9
10
# File 'lib/capistrano/deploy_tags.rb', line 8

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

#has_remote?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/capistrano/deploy_tags.rb', line 27

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

#pending_git_changes?Boolean

Returns:

  • (Boolean)


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

def pending_git_changes?
  # Do we have any changes vs HEAD on deployment branch?
  !(`git fetch && git diff #{branch} --shortstat`.strip.empty?)
end

#remoteObject



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

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

#safe_run(*args) ⇒ Object



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

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

#validate_git_varsObject



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

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