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



86
87
88
# File 'lib/shiplane/build.rb', line 86

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

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



90
91
92
# File 'lib/shiplane/build.rb', line 90

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

Instance Method Details

#appnameObject



24
25
26
# File 'lib/shiplane/build.rb', line 24

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

#build!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shiplane/build.rb', line 48

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

#buildable_artifactsObject



44
45
46
# File 'lib/shiplane/build.rb', line 44

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

#docker_configObject



40
41
42
# File 'lib/shiplane/build.rb', line 40

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

#project_folderObject



32
33
34
# File 'lib/shiplane/build.rb', line 32

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

#project_folder_nameObject



28
29
30
# File 'lib/shiplane/build.rb', line 28

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

#shiplane_configObject



36
37
38
# File 'lib/shiplane/build.rb', line 36

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

#steps(artifact_name, attributes) ⇒ Object



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

def steps(artifact_name, attributes)
  [
    { command: "docker-compose build --no-cache #{artifact_name}", notify_before: "Building Artifact: #{artifact_name}...", notify_after: "Docker Compose Built", stop_on_failure: true },
    { command: "docker tag #{attributes['repo']}:#{sha} #{attributes['repo']}:#{postfix}-#{sha}", notify_before: "Tagging Build...", stop_on_failure: true, condition: !!postfix },
    { command: "docker tag #{attributes['repo']}:#{sha} #{attributes['repo']}:#{postfix}-latest", notify_before: "Tagging Build...", stop_on_failure: true, condition: !!postfix && tag_latest },
    { command: "docker tag #{attributes['repo']}:#{sha} #{attributes['repo']}:latest", notify_before: "Tagging Latest Build...", stop_on_failure: true, condition: tag_latest },
    { command: "echo '#{ENV['DOCKERHUB_PASSWORD']}' | docker login --username #{ENV['DOCKERHUB_USERNAME']} --password-stdin", notify_before: "Logging into DockerHub...", stop_on_failure: true },
    { command: "docker push '#{attributes['repo']}:#{sha}'", notify_before: "Pushing Image", notify_after: "Completed Artifact: #{artifact_name}...", stop_on_failure: true },
    { command: "docker push '#{attributes['repo']}:#{postfix}-#{sha}'", notify_before: "Pushing #{postfix} Image", notify_after: "Completed Artifact: #{artifact_name}...", stop_on_failure: true, condition: !!postfix },
    { command: "docker push '#{attributes['repo']}:#{postfix}-latest'", notify_before: "Pushing Latest #{postfix} Image", notify_after: "Completed Latest Artifact: #{artifact_name}...", stop_on_failure: true, condition: !!postfix && tag_latest },
    { command: "docker push '#{attributes['repo']}:latest'", notify_before: "Pushing Latest Image", notify_after: "Completed Latest Artifact: #{artifact_name}...", stop_on_failure: true, condition: tag_latest },
  ]
end