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



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
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/capistrano/deploy_tags.rb', line 55

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"

        cdt.safe_run 'git', 'tag', '-a', cdt.git_tag_for(stage), '-m', cdt.commit_message(current_sha)
        cdt.safe_run 'git', 'push', '--tags' if cdt.has_remote?
      end
    end

  end
end

Instance Method Details

#commit_message(current_sha) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/capistrano/deploy_tags.rb', line 46

def commit_message(current_sha)
  if exists?(:deploytag_commit_message)
    deploytag_commit_message
  else
    tag_user = (ENV['USER'] || ENV['USERNAME'] || 'deployer').strip
    "#{tag_user} deployed #{current_sha} to #{stage}"
  end
end

#exec_success?Boolean

Returns:

  • (Boolean)


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

def exec_success?
  $?.success?
end

#formatted_timeObject



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

def formatted_time
  Time.new.utc.strftime(fetch(:deploytag_time_format, "%Y.%m.%d-%H%M%S-utc"))
end

#git_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


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

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}-#{formatted_time}"
end

#has_remote?Boolean

Returns:

  • (Boolean)


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

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



42
43
44
# File 'lib/capistrano/deploy_tags.rb', line 42

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

#safe_run(*args) ⇒ Object



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

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

#validate_git_varsObject



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

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