Class: HerokuTool::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku_tool/commander.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, configuration:, **options) ⇒ Commander

Returns a new instance of Commander.



11
12
13
14
15
# File 'lib/heroku_tool/commander.rb', line 11

def initialize(target, configuration:, **options)
  @target = target
  @migrate_outside_of_release_phase = target&.migrate_in_release_phase ? false : options[:migrate]
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



9
10
11
# File 'lib/heroku_tool/commander.rb', line 9

def configuration
  @configuration
end

#targetHerokuTool::HerokuTargets::HerokuTarget (readonly)



8
9
10
# File 'lib/heroku_tool/commander.rb', line 8

def target
  @target
end

Instance Method Details

#deploy(deploy_ref, with_maintenance:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/heroku_tool/commander.rb', line 17

def deploy(deploy_ref, with_maintenance:)
  deploy_ref_description = deploy_ref_describe(deploy_ref)
  puts "Deploy #{deploy_ref_description} to #{target} with migrate=#{target.migrate_in_release_phase ? "(during release phase)" : migrate_outside_of_release_phase?} with_maintenance=#{with_maintenance} "

  output_to_be_deployed(deploy_ref)
  configuration.before_deploying(self, target, deploy_ref_description)
  successful_push = puts_and_system "git push -f #{target.git_remote} #{deploy_ref || target}^{}:#{target.heroku_target_ref}"

  return false unless successful_push

  maintenance_on if with_maintenance
  if migrate_outside_of_release_phase?
    puts_and_system "heroku run rake db:migrate -a #{target.heroku_app}"
  end

  app_revision_env_var = configuration.app_revision_env_var
  if app_revision_env_var && app_revision_env_var != "HEROKU_SLUG_COMMIT"
    # HEROKU_SLUG_COMMIT is automatically set by https://devcenter.heroku.com/articles/dyno-metadata
    puts_and_system %{heroku config:set #{app_revision_env_var}=$(git describe --always #{deploy_ref}) -a #{target.heroku_app}}
  end

  maintenance_off if with_maintenance
  configuration.after_deploying(self, target, deploy_ref_description)
  true
end

#deploy_ref_describe(deploy_ref = nil) ⇒ Object



57
58
59
# File 'lib/heroku_tool/commander.rb', line 57

def deploy_ref_describe(deploy_ref = nil)
  `git describe #{deploy_ref || target.deploy_ref}`.strip
end

#exec_with_clean_env(cmd, *args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/heroku_tool/commander.rb', line 87

def exec_with_clean_env(cmd, *args)
  output, _status = if defined?(Bundler) && Bundler.respond_to?(:with_unbundled_env)
    Bundler.with_unbundled_env { Open3.capture2(cmd, *args) }
  elsif defined?(Bundler)
    Bundler.with_clean_env { Open3.capture2(cmd, *args) }
  else
    Open3.capture2(cmd, *args)
  end
  output
end

#maintenance_offObject



48
49
50
51
# File 'lib/heroku_tool/commander.rb', line 48

def maintenance_off
  puts_and_system "heroku maintenance:off -a #{target.heroku_app}"
  puts_and_system "heroku config:unset #{configuration.maintenance_mode_env_var} -a #{target.heroku_app}"
end

#maintenance_onObject



43
44
45
46
# File 'lib/heroku_tool/commander.rb', line 43

def maintenance_on
  puts_and_system "heroku maintenance:on -a #{target.heroku_app}"
  puts_and_system "heroku config:set #{configuration.maintenance_mode_env_var}=true -a #{target.heroku_app}"
end

#migrate_outside_of_release_phase?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/heroku_tool/commander.rb', line 53

def migrate_outside_of_release_phase?
  @migrate_outside_of_release_phase
end

#output_to_be_deployed(since_deploy_ref = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/heroku_tool/commander.rb', line 61

def output_to_be_deployed(since_deploy_ref = nil)
  puts "------------------------------"
  puts " Deploy to #{target}:"
  puts "------------------------------"
  system_with_clean_env "git --no-pager log $(heroku config:get #{configuration.app_revision_env_var} -a #{target.heroku_app})..#{since_deploy_ref || target.deploy_ref}"
  puts "------------------------------"
end

#puts_and_exec(cmd, *args) ⇒ Object



82
83
84
85
# File 'lib/heroku_tool/commander.rb', line 82

def puts_and_exec(cmd, *args)
  puts([cmd, *args].join(" "))
  exec_with_clean_env(cmd, *args)
end

#puts_and_system(cmd, *args) ⇒ Boolean

Returns true if the command was successful.

Returns:

  • (Boolean)

    true if the command was successful



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/heroku_tool/commander.rb', line 70

def puts_and_system(cmd, *args)
  puts([cmd, *args].join(" "))
  puts "-------------"
  system_with_clean_env(cmd, *args).tap do |result|
    if result
      puts "-------------"
    else
      puts "❌❌❌❌❌❌❌❌❌❌"
    end
  end
end