class Gbuild::BuildCommand

Public Instance Methods

execute() click to toggle source
# File lib/gbuild/build_command.rb, line 20
def execute
  project_name = unless project
    _, project_from_image, _ = image.split("/")
    project_from_image
  else
    project
  end

  build_source = "#{Gbuild.source_bucket(project_name)}/#{SecureRandom.uuid}"
  build_log = "#{Gbuild.logs_bucket(project_name)}/#{SecureRandom.uuid}"

  t = Template.new "build.yaml"
  t.set :image, image
  t.set :dockerfile, dockerfile if dockerfile
  t.set :build_args, build_arg_list
  t.set :cache, (cache? ? "true" : "false")
  t.set :debug, debug?

  gcloud_config = Tempfile.new
  File.write gcloud_config.path, t.result

  puts t.result if debug?

  cmd = ["gcloud","builds","submit"]
  cmd << context unless context == {}

  cmd += ["--gcs-log-dir", build_source]
  cmd += ["--gcs-source-staging-dir", build_log]

  cmd += ["--config", gcloud_config.path]
  cmd += ["--timeout", timeout] if timeout
  cmd += ["--project", project_name]

  build_cmd = cmd.join " "
  puts build_cmd if debug?
  build_k = Kommando.new build_cmd, output: true
  build_k.run

  waits = []
  if clean_source?
    puts "removing source from #{build_source}"
    waits << (Kommando.run_async "gsutil rm -r #{build_source}")
  end
  if clean_log?
    puts "removing log from #{build_log}"
    waits << (Kommando.run_async "gsutil rm -r #{build_log}")
  end
  waits.each { |k| k.wait }
end