Class: ECSHelper::Command::BuildAndPush

Inherits:
Base
  • Object
show all
Defined in:
lib/ecs_helper/command/build_and_push.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #helper, #option_parser, #options, #type

Instance Method Summary collapse

Methods inherited from Base

#check_bin, #initialize, #printable?, #validate

Methods included from Logging

#console, #error, #log

Constructor Details

This class inherits a constructor from ECSHelper::Command::Base

Instance Method Details

#auth_privateObject



45
46
47
48
# File 'lib/ecs_helper/command/build_and_push.rb', line 45

def auth_private
  auth_cmd = helper.auth_private_cmd
  auth_cmd.run
end

#buildObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ecs_helper/command/build_and_push.rb', line 61

def build
  build_command = ["docker build #{directory}"]
  build_args = options[:build_args].map { |a| "--build-arg=#{a}" }
  cache_command = should_cache? ? ["--cache-from #{latest_tag}"] : []
  tags_command = ["--tag #{latest_tag} --tag #{version_tag}"]
  command = (build_command + build_args + cache_command + tags_command).join(' ')
  build_cmd = Terrapin::CommandLine.new(command)

  console "Building with two tags: #{latest_tag} & #{version_tag}"
  build_cmd.run
end

#cmd_option_parserObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ecs_helper/command/build_and_push.rb', line 6

def cmd_option_parser
  options = { build_args: [] }
  parser = ::OptionParser.new do |opts|
    opts.banner = 'Usage: ecs_helper build_and_push [options]'
    opts.on('-i VALUE', '--image VALUE',
            'Set image name, will be used to detect ecr repo where to push image, for example web/nginx/toolbox (required)') do |c|
      options[:image] = processEqual(c)
    end
    opts.on('-d VALUE', '--directory VALUE', "Set directory for dockerfile and context, default = './'") do |c|
      options[:directory] = processEqual(c)
    end
    opts.on('-p VALUE', '--project VALUE',
            "Set project name, if not specified will look at ENV['PROJECT'], will be used to detect cluster") do |p|
      options[:project] = processEqual(p)
    end
    opts.on('-a VALUE', '--application VALUE',
            "Set application name, if not specified will look at ENV['APPLICATION'], will be used to detect service and task definition") do |a|
      options[:application] = processEqual(a)
    end
    opts.on('-c', '--cache', 'Cache image before build, default false') { options[:cache] = true }
    opts.on('--build-arg=VALUE', 'Pass --build-arg to the build command') { |o| options[:build_args] << o }
  end
  [parser, options]
end

#pullObject



54
55
56
57
58
59
# File 'lib/ecs_helper/command/build_and_push.rb', line 54

def pull
  pull_cmd = Terrapin::CommandLine.new("docker pull #{latest_tag}")
  pull_cmd.run
rescue Terrapin::ExitStatusError => e
  console e.message
end

#pushObject



73
74
75
76
# File 'lib/ecs_helper/command/build_and_push.rb', line 73

def push
  pull_cmd = Terrapin::CommandLine.new("docker push #{latest_tag} && docker push #{version_tag}")
  pull_cmd.run
end

#requiredObject



31
32
33
# File 'lib/ecs_helper/command/build_and_push.rb', line 31

def required
  [:image]
end

#runObject



35
36
37
38
39
40
41
42
43
# File 'lib/ecs_helper/command/build_and_push.rb', line 35

def run
  log('Command', type)
  log('Options', options)
  log('Repository', repository)
  log('Auth Private', auth_private)
  should_cache? && log('Pull', pull)
  log('Build', build)
  log('Push', push)
end

#should_cache?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ecs_helper/command/build_and_push.rb', line 50

def should_cache?
  options[:cache]
end