Class: Brut::CLI::Apps::Deploy::Heroku

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/deploy.rb

Defined Under Namespace

Classes: DeployConfig

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #args_description, #argv, #bootstrap?, #commands, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #print, #puts, #stdin, #system!, #theme

Instance Method Details

#default_rack_envObject



96
# File 'lib/brut/cli/apps/deploy.rb', line 96

def default_rack_env = "development"

#descriptionObject



97
# File 'lib/brut/cli/apps/deploy.rb', line 97

def description = "Deploy to Heroku using container-based deployment"

#optsObject



98
99
100
# File 'lib/brut/cli/apps/deploy.rb', line 98

def opts = [
  [ "--build-only", "Only generate Dockerfiles and build images, do not deploy" ],
]

#runObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/brut/cli/apps/deploy.rb', line 101

def run
  deploy_config_path = Brut.container.project_root / "deploy" / "deploy_config.rb"
  begin
    require deploy_config_path
    if !defined?(AppDeployConfig)
      fatal "#{deploy_config_path} must define the class AppDeployConfig"
      return 1
    end
    if !AppDeployConfig.ancestors.include?(Brut::CLI::Apps::Deploy::Heroku::DeployConfig)
      fatal "#{deploy_config_path} must define a subclass of Brut::CLI::Apps::Deploy::Heroku::DeployConfig"
      return 1
    end
    dockerfile = Brut.container.project_root / "deploy" / "Dockerfile"
    if !dockerfile.exist?
      fatal "#{dockerfile} does not exist - it should've been created when you added the Heroku segment"
      return 1
    end
    git_checks = Brut::CLI::Apps::Deploy::GitChecks.new(executor: execution_context.executor)
    results = git_checks.check!
    if results.errors?
      results.errors.each do |_,message|
        fatal message
      end
      return 1
    end
    begin
      command = %{heroku container:login}
      system!(command)
    rescue Brut::CLI::SystemExecError => ex
      fatal(ex)
      fatal("Not logged into Heroku")
      return 1
    end
    config = AppDeployConfig.new
    version = ""
    git_guess = %{git rev-parse HEAD}
    version = capture!(git_guess).strip.chomp
    if version == ""
      fatal "Attempt to use git via command '#{git_guess}' to figure out the version failed"
      return 1
    end
    names = []
    config.each_dockerfile do |process_dockerfile, process_description|
      process_dockerfile_path = dockerfile.dirname / process_dockerfile
      FileUtils.cp dockerfile, process_dockerfile_path
      File.open(dockerfile.dirname / process_dockerfile, "a") do |file|
        file.puts(process_description.cmd_directive)
      end
      image_name = "#{config.registry_hostname}/#{Brut.container.app_id}/#{process_description.name}"
      push_or_load = if options.build_only?
                       "--load"
                     else
                       "--push"
                     end
      command = %{docker buildx build --provenance=false --build-arg app_git_sha1=#{version} --file #{process_dockerfile_path} --platform #{config.platform} #{push_or_load} --tag #{image_name} . 2>&1}
      system!(command, output: :stream)
      names << process_description.name
    end
    deploy_command = "heroku container:release #{names.sort.join(' ')} -a #{Brut.container.app_id}"
    if options.build_only?
      puts "Not deploying"
    else
      system!(deploy_command, output: :stream)
    end

    0
  rescue LoadError => ex
    fatal "Could not find #{deploy_config_path}: #{ex}"
    1
  end
end