Class: Shipwright::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/shipwright/builder.rb

Constant Summary collapse

BASE_VERSION =
'1.0.0'
VERSION_FILE =
'VERSION'
DOCKERRUN =
'Dockerrun.aws.json'
COMMIT_MESSAGE =
"new version %s built by Shipwright #{Shipwright::VERSION}\n
[ci skip]"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Builder

Returns a new instance of Builder.



36
37
38
# File 'lib/shipwright/builder.rb', line 36

def initialize(path)
  self.path = path
end

Instance Attribute Details

#commit_messageObject

Returns the value of attribute commit_message.



10
11
12
# File 'lib/shipwright/builder.rb', line 10

def commit_message
  @commit_message
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/shipwright/builder.rb', line 10

def path
  @path
end

#previous_versionObject

Returns the value of attribute previous_version.



10
11
12
# File 'lib/shipwright/builder.rb', line 10

def previous_version
  @previous_version
end

#shipyardObject

Returns the value of attribute shipyard.



10
11
12
# File 'lib/shipwright/builder.rb', line 10

def shipyard
  @shipyard
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/shipwright/builder.rb', line 10

def version
  @version
end

Class Method Details

.applicationObject



32
33
34
# File 'lib/shipwright/builder.rb', line 32

def self.application
  File.basename Dir.pwd
end

.build(path, shipyard = nil) ⇒ Object



12
13
14
# File 'lib/shipwright/builder.rb', line 12

def self.build(path, shipyard = nil)
  new(path).tap { |b| b.shipyard = shipyard }.build
end

.bumpObject



16
17
18
19
# File 'lib/shipwright/builder.rb', line 16

def self.bump
  Bump::Bump.run('patch', bundle: false, tag: false)
  Shipwright.info "Installed #{version} into #{VERSION_FILE}"
end

.initObject



21
22
23
24
25
26
# File 'lib/shipwright/builder.rb', line 21

def self.init
  return if version

  Shipwright.info "Installing #{BASE_VERSION} into #{VERSION_FILE}"
  File.open(VERSION_FILE, 'wb') { |f| f.write BASE_VERSION }
end

.versionObject



28
29
30
# File 'lib/shipwright/builder.rb', line 28

def self.version
  Bump::Bump.version_from_version.tap { |v| return v.first if v }
end

Instance Method Details

#applicationObject



131
132
133
# File 'lib/shipwright/builder.rb', line 131

def application
  @application ||= self.class.application
end

#buildObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/shipwright/builder.rb', line 40

def build
  shipyard
  self.class.init
  Shipwright.info "Shipwright is building #{path} (at #{self.class.version})"

  bump_version

  build_image
  push_image

  update_dockerrun
  generate_artifact
  update_ebconfig

  git_commit
  git_push

  Shipwright.info "VERSION #{version} built"
end

#build_imageObject



64
65
66
67
68
# File 'lib/shipwright/builder.rb', line 64

def build_image
  Shipwright.info "Building image with tag #{docker_tag}"
  run_cmd "docker build -t #{docker_tag} #{path}"
  # self.image = Docker::Image.build_from_dir(path, 'tag' => docker_tag)
end

#bump_versionObject



60
61
62
# File 'lib/shipwright/builder.rb', line 60

def bump_version
  self.class.bump
end

#docker_tagObject



117
118
119
# File 'lib/shipwright/builder.rb', line 117

def docker_tag
  "#{shipyard}/#{application}:#{version}"
end

#generate_artifactObject



91
92
93
94
# File 'lib/shipwright/builder.rb', line 91

def generate_artifact
  Shipwright.info "Generating artifact"
  Shipwright::ElasticBeanstalk.generate_artifact
end

#git_commitObject



101
102
103
104
105
106
# File 'lib/shipwright/builder.rb', line 101

def git_commit
  Shipwright.info "Commiting to git"
  git.add all: true
  git.commit COMMIT_MESSAGE % version
  git.add_tag git_tag
end

#git_pushObject



108
109
110
111
# File 'lib/shipwright/builder.rb', line 108

def git_push
  Shipwright.info "Pushing to git"
  git.push 'origin', git.current_branch, tags: true
end

#git_tagObject



113
114
115
# File 'lib/shipwright/builder.rb', line 113

def git_tag
  "build-#{version}"
end

#push_imageObject



70
71
72
73
74
# File 'lib/shipwright/builder.rb', line 70

def push_image
  Shipwright.info "Pushing image to #{docker_tag}"
  run_cmd "docker push #{docker_tag}"
  # image.push
end

#run_cmd(cmd) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/shipwright/builder.rb', line 76

def run_cmd(cmd)
  io = IO.popen(cmd)
  io.each { |line| Shipwright.info line.chomp }
  io.close
  unless $?.exitstatus.zero?
    puts "Command #{cmd} exitted with non zero status #{$?.exitstatus}"
    exit $?.exitstatus
  end
end

#update_dockerrunObject



86
87
88
89
# File 'lib/shipwright/builder.rb', line 86

def update_dockerrun
  Shipwright.info "Updating Dockerrun.aws.json"
  Shipwright::ElasticBeanstalk.update_dockerrun
end

#update_ebconfigObject



96
97
98
99
# File 'lib/shipwright/builder.rb', line 96

def update_ebconfig
  Shipwright.info "Updating .elasticbeanstalk/config.yml"
  Shipwright::ElasticBeanstalk.update_ebconfig
end