Class: BuildLabels::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/version.rb,
lib/build-labels/builder.rb

Constant Summary collapse

VERSION =
'0.0.65'

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



17
18
19
20
21
22
23
# File 'lib/build-labels/builder.rb', line 17

def initialize
  # https://github.com/opencontainers/image-spec/blob/v1.0.1/annotations.md
  add_namespace :oc, 'org.opencontainers.image', [
    :vendor, :authors, :revision, :source, :documentation, :licenses, :url,
    :version, :'ref.name', :title, :description, :created
  ]
end

Instance Method Details

#add_namespace(name, path, labels = []) ⇒ Object



11
12
13
14
15
# File 'lib/build-labels/builder.rb', line 11

def add_namespace(name, path, labels=[])
  @namespaces ||= {}
  @namespaces[path] = OpenStruct.new
  self.class.send :define_method, name, -> { @namespaces[path] }
end

#apply_environmentObject



40
41
42
43
44
45
46
47
48
# File 'lib/build-labels/builder.rb', line 40

def apply_environment
  @namespaces.each do |ns, struct|
    struct.each_pair do |name, value|
      next unless value.to_s.include? '$'
      struct[name] = value.gsub(/\$\{(\w+)\}/) { ENV[$1] }
                          .gsub(/\$(\w+)/) { ENV[$1] }
    end
  end
end

#each_labelsObject



50
51
52
53
54
55
56
# File 'lib/build-labels/builder.rb', line 50

def each_labels
  generate_label_schema
  apply_environment
  @namespaces.each do |ns, values|
    yield ns, values
  end
end

#extend_compose(compose) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/build-labels/builder.rb', line 57

def extend_compose(compose)
  compose['services'].each do |name, service|
    service.delete_if {|k, v| !%w[image build].include? k }
    next unless service['build']
    if service['build'].class == String
      service['build'] = { 'context' => service['build'] }
    end

    if ENV['CI_BUILD_TARGET']
      service['build']['target'] = ENV['CI_BUILD_TARGET']
    end

    service['build']['labels'] ||= []
    add_namespace :dc, 'docker.service'
    self.dc.name = name

    each_labels do |ns, values|
      values.each_pair do |k,v|
        service['build']['labels'] << "#{ns}.#{k}=#{v}" unless v.to_s.empty?
      end
    end
  end
  compose['services'].delete_if do |name, svc| ! svc.key?('build') end

  puts compose.to_yaml
end

#generate_label_schemaObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/build-labels/builder.rb', line 25

def generate_label_schema
  # Label Schema compatibility
  add_namespace :ls, 'org.label-schema'
  self.ls['build-date'] = self.oc.created
  self.ls.url = self.oc.url
  self.ls['vcs-url'] = self.oc.source
  self.ls.version = self.oc.version
  self.ls['vcs-ref'] = self.oc.revision
  self.ls.vendor = self.oc.vendor
  self.ls.name = self.oc.title
  self.ls.description = self.oc.description
  self.ls.usage = self.oc.documentation
  self.ls['schema-version'] = '1.0'
end