Class: Shiplane::Build

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shiplane/build.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sha, postfix:, tag_latest: false) ⇒ Build

Returns a new instance of Build.



16
17
18
19
20
21
22
# File 'lib/shiplane/build.rb', line 16

def initialize(sha, postfix:, tag_latest: false)
  @sha = sha
  @tag_latest = tag_latest
  @postfix = postfix

  Dotenv.overload File.join(Dir.pwd, build_config.fetch('environment_file', '.env'))
end

Instance Attribute Details

#postfixObject

Returns the value of attribute postfix.



12
13
14
# File 'lib/shiplane/build.rb', line 12

def postfix
  @postfix
end

#shaObject

Returns the value of attribute sha.



12
13
14
# File 'lib/shiplane/build.rb', line 12

def sha
  @sha
end

#tag_latestObject

Returns the value of attribute tag_latest.



12
13
14
# File 'lib/shiplane/build.rb', line 12

def tag_latest
  @tag_latest
end

Class Method Details

.build!(sha, postfix = nil) ⇒ Object

API Helper Methods



180
181
182
# File 'lib/shiplane/build.rb', line 180

def self.build!(sha, postfix = nil)
  new(sha, postfix: postfix).build!
end

.build_latest!(sha, postfix = nil) ⇒ Object



184
185
186
# File 'lib/shiplane/build.rb', line 184

def self.build_latest!(sha, postfix = nil)
  new(sha, postfix: postfix, tag_latest: true).build!
end

Instance Method Details

#appnameObject

Properties



105
106
107
# File 'lib/shiplane/build.rb', line 105

def appname
  @appname ||= project_config['appname']
end

#build!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shiplane/build.rb', line 24

def build!
  unless File.exist?(File.join(project_folder, Shiplane::SHIPLANE_CONFIG_FILENAME))
    Shiplane::CheckoutArtifact.checkout!(sha)
    Shiplane::ConvertComposeFile.convert_output!(project_folder, sha)
  end

  buildable_artifacts.each do |(artifact_name, attributes)|
    compose_context = docker_config.fetch('services', {}).fetch(artifact_name.to_s, {})
    Shiplane::ConvertDockerfile.convert_output!(project_folder, attributes, compose_context)

    FileUtils.cd project_folder do
      steps(artifact_name, attributes).select{|step| step.fetch(:condition, true) }.each do |step|
        puts step[:notify_before] if step.has_key? :notify_before
        success = system(step[:command])
        raise StepFailureException.new(step[:command], artifact_name) unless success
        puts step[:notify_after] if step.has_key? :notify_after
      end
    end
  end
rescue StepFailureException => e
  puts e.message
  raise if ENV['RAISE_EXCEPTIONS_ON_FAILED_BUILD']
end

#build_cache_optionObject



175
176
177
# File 'lib/shiplane/build.rb', line 175

def build_cache_option
  ENV['USE_BUILD_CACHE'] == 'true' ? nil : "--no-cache"
end

#build_command(artifact_name) ⇒ Object

Commands



64
65
66
67
68
69
70
71
# File 'lib/shiplane/build.rb', line 64

def build_command(artifact_name)
  [
    'docker-compose',
    'build',
    build_cache_option,
    artifact_name,
  ].compact.join(' ')
end

#build_output_image_name(artifact_name) ⇒ Object



171
172
173
# File 'lib/shiplane/build.rb', line 171

def build_output_image_name(artifact_name)
  @build_output_image_name ||= "#{appname}-#{sha}_#{artifact_name}:latest"
end

#buildable_artifactsObject



125
126
127
# File 'lib/shiplane/build.rb', line 125

def buildable_artifacts
  build_config.fetch('artifacts', {})
end

#default_registry_configurationObject



129
130
131
132
133
134
# File 'lib/shiplane/build.rb', line 129

def default_registry_configuration
  {
    'url' => :dockerhub,
    'auth_method' => 'token',
  }
end

#docker_configObject



121
122
123
# File 'lib/shiplane/build.rb', line 121

def docker_config
  @docker_config ||= YAML.load(File.new(File.join(project_folder, 'docker-compose.yml')))
end

#dockerhub?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/shiplane/build.rb', line 136

def dockerhub?
  registry_configuration['url'] == :dockerhub
end

#login_tokenObject



159
160
161
162
163
# File 'lib/shiplane/build.rb', line 159

def 
  return ENV['DOCKERHUB_PASSWORD'] if dockerhub? && token_auth?

  ENV['SHIPLANE_CONTAINER_REGISTRY_TOKEN']
end

#login_usernameObject



165
166
167
168
169
# File 'lib/shiplane/build.rb', line 165

def 
  return ENV['DOCKERHUB_USERNAME'] if dockerhub? && token_auth?

  ENV['SHIPLANE_CONTAINER_REGISTRY_USERNAME']
end

#project_folderObject



113
114
115
# File 'lib/shiplane/build.rb', line 113

def project_folder
  @project_folder ||= File.join(Dir.pwd, 'docker_builds', appname, project_folder_name)
end

#project_folder_nameObject



109
110
111
# File 'lib/shiplane/build.rb', line 109

def project_folder_name
  @project_folder_name ||= "#{appname}-#{sha}"
end

#push_command(attributes, tag = 'latest') ⇒ Object



96
97
98
99
100
101
102
# File 'lib/shiplane/build.rb', line 96

def push_command(attributes, tag='latest')
  [
    'docker',
    'push',
    "#{repo_name(attributes)}:#{tag}",
  ].compact.join(' ')
end

#registry_configurationObject



144
145
146
# File 'lib/shiplane/build.rb', line 144

def registry_configuration
  @registry_configuration ||= default_registry_configuration.merge(build_config.fetch('registry', {}))
end

#registry_urlObject



148
149
150
# File 'lib/shiplane/build.rb', line 148

def registry_url
  @registry_url ||= dockerhub? ? nil : registry_configuration['url']
end

#repo_name(attributes) ⇒ Object



152
153
154
155
156
157
# File 'lib/shiplane/build.rb', line 152

def repo_name(attributes)
  [
    registry_url,
    attributes['repo'],
  ].compact.join('/')
end

#shiplane_configObject



117
118
119
# File 'lib/shiplane/build.rb', line 117

def shiplane_config
  @shiplane_config ||= Shiplane::Configuration.new
end

#steps(artifact_name, attributes) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shiplane/build.rb', line 48

def steps(artifact_name, attributes)
  [
    { command: build_command(artifact_name), notify_before: "Building Artifact: #{artifact_name}...", notify_after: "Docker Compose Built", stop_on_failure: true },
    { command: tag_command(artifact_name, attributes, sha), notify_before: "Tagging Build [#{sha}]...", stop_on_failure: true },
    { command: tag_command(artifact_name, attributes, "#{postfix}-#{sha}"), notify_before: "Tagging Build [#{postfix}-#{sha}]...", stop_on_failure: true, condition: !!postfix },
    { command: tag_command(artifact_name, attributes, "#{postfix}-latest"), notify_before: "Tagging Build [#{postfix}-latest]...", stop_on_failure: true, condition: !!postfix && tag_latest },
    { command: tag_command(artifact_name, attributes), notify_before: "Tagging Build [latest]...", stop_on_failure: true, condition: tag_latest },
    { command:  , notify_before: "Logging into Container Registry...", stop_on_failure: true },
    { command: push_command(attributes, "#{sha}"), notify_before: "Pushing Image", notify_after: "Completed Artifact: #{artifact_name}...", stop_on_failure: true },
    { command: push_command(attributes, "#{postfix}-#{sha}"), notify_before: "Pushing #{postfix} Image", notify_after: "Completed Artifact: #{artifact_name}...", stop_on_failure: true, condition: !!postfix },
    { command: push_command(attributes, "#{postfix}-latest"), notify_before: "Pushing Latest #{postfix} Image", notify_after: "Completed Latest Artifact: #{artifact_name}...", stop_on_failure: true, condition: !!postfix && tag_latest },
    { command: push_command(attributes, "latest"), notify_before: "Pushing Latest Image", notify_after: "Completed Latest Artifact: #{artifact_name}...", stop_on_failure: true, condition: tag_latest },
  ]
end

#tag_command(artifact_name, attributes, tag = 'latest') ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/shiplane/build.rb', line 87

def tag_command(artifact_name, attributes, tag='latest')
  [
    'docker',
    'tag',
    build_output_image_name(artifact_name),
    "#{repo_name(attributes)}:#{tag}",
  ].compact.join(' ')
end

#token_auth?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/shiplane/build.rb', line 140

def token_auth?
  registry_configuration['auth_method'] == 'token'
end

#token_login_commandObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/shiplane/build.rb', line 73

def 
  @token_login_command ||= [
    'echo',
    ,
    '|',
    'docker',
    'login',
    registry_url,
    '--username',
    ,
    '--password-stdin',
  ].compact.join(' ')
end