Class: Gbuild::BuildCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/gbuild/build_command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# 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