build-labels 0.0.15 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16c94f8ee0325f5a908ff99778c8e590c93ccd270dadc7df019c86e586bc788c
4
- data.tar.gz: 1be4edf51d1bc84e8ea74168b1d5fb1f81f83582f2bfc5d8e9d4b9f9f8dee078
3
+ metadata.gz: 139e7e061e2bf7a7a9cb861de8477fc09e08340987ed89e53239e9867881e1c7
4
+ data.tar.gz: 9ab1d0aaf92e3b0f18e5ae4c42ead33c058513fd25123e6e859c231ae10538db
5
5
  SHA512:
6
- metadata.gz: c58fd66950b1d37f491989ae511efbaa6715f4072b630be97ca85cabd14aaf24c91b41d22d87d7c29e37445c8be472cfc59b6a378e8b45c685b9e8cf429a0a7a
7
- data.tar.gz: 4fa922c024bfe2b1ae6169eb57633093726015bc4b195030ab8f8ed74981d5956ede6f66b6f24cf75c393628142e20decf64fcf937f6d53bc6dc084d3426ed70
6
+ metadata.gz: ec5e462b03595e656d57d925fe58b993377a9e1508758c0115054fe8d41c6b518591d6dda5fb5c75441e4cc82a83aaa87304eb92c25165a20e4377ee7cfb368d
7
+ data.tar.gz: 17e2b02be847d4d8f008b3cc6f3564e06e3c0039cb51a3c1107680b82e7ade6c3090236a4f0ce45b7306364035ff03d52e80431b0fb83e9b835ba62d40b57095
data/examples/Dockerfile CHANGED
@@ -7,4 +7,5 @@ ENV COM_GITLAB_CI_COMMITURL="/commit/"
7
7
  ENV COM_GITLAB_CI_MRURL="/-/merge_requests/"
8
8
  ENV COM_GITLAB_CI_TAG=":"
9
9
  ENV ORG_LABEL-SCHEMA_VENDOR="/"
10
- ENV ORG_LABEL-SCHEMA_SCHEMA-VERSION="1.0"
10
+ ENV ORG_LABEL-SCHEMA_SCHEMA-VERSION="1.0"
11
+ ENV ORG_OPENCONTAINERS_IMAGE_REF_NAME=":"
@@ -0,0 +1,30 @@
1
+ x-common-build: &common-build
2
+ args:
3
+ - CI_PIPELINE_ID=${CI_PIPELINE_ID}
4
+ - CI_PIPELINE_IID=${CI_PIPELINE_IID}
5
+ - CI_NEXUS_CREDENTIALS=${CI_NEXUS_CREDENTIALS}
6
+ - CI_NEXUS_FOLDER=${CI_NEXUS_FOLDER}
7
+ - CI_COMMIT_TIMESTAMP=${CI_COMMIT_TIMESTAMP}
8
+ - CI_PROJECT_PATH_SLUG=${CI_PROJECT_PATH_SLUG}
9
+ - CI_COMMIT_BRANCH=${CI_COMMIT_BRANCH}
10
+ context: ../src_mobile_app
11
+ dockerfile: ../docker/flutter/Dockerfile
12
+
13
+ services:
14
+ app_dev:
15
+ image: ${REGISTRY_HOST}/app/${CI_COMMIT_BRANCH}-dev
16
+ build:
17
+ <<: *common-build
18
+ args:
19
+ - MAP_BACKEND=dev
20
+ volumes:
21
+ - ../docker_build_results_dev:/app/results
22
+
23
+ app_prod:
24
+ image: ${REGISTRY_HOST}/app/${CI_COMMIT_BRANCH}-prod
25
+ build:
26
+ <<: *common-build
27
+ args:
28
+ - MAP_BACKEND=prod
29
+ volumes:
30
+ - ../docker_build_results_prod:/app/results
@@ -6,6 +6,10 @@ services:
6
6
  build:
7
7
  context: .
8
8
  dockerfile: ./Dockerfile
9
+ ports:
10
+ - 8080
11
+ environment:
12
+ - hello
9
13
  service-b:
10
14
  image: service-a
11
15
  build: .
@@ -2,6 +2,38 @@
2
2
  require 'yaml'
3
3
  require 'ostruct'
4
4
  require 'pp'
5
+ require_relative 'yaml_merge'
6
+
7
+ class MyYAMLTree < Psych::Visitors::YAMLTree
8
+ class Registrar
9
+ # record object for future, using '@_yaml_anchor_name' rather
10
+ # than object_id if it exists
11
+ def register target, node
12
+ anchor_name = target.instance_variable_get('@_yaml_anchor_name') || target.object_id
13
+ @obj_to_node[anchor_name] = node
14
+ end
15
+ end
16
+
17
+ # check to see if this object has been seen before
18
+ def accept target
19
+ if anchor_name = target.instance_variable_get('@_yaml_anchor_name')
20
+ if @st.key? anchor_name
21
+ oid = anchor_name
22
+ node = @st[oid]
23
+ anchor = oid.to_s
24
+ node.anchor = anchor
25
+ return @emitter.alias anchor
26
+ end
27
+ end
28
+
29
+ # accept is a pretty big method, call super to avoid copying
30
+ # it all here. super will handle the cases when it's an object
31
+ # that's been seen but doesn't have '@_yaml_anchor_name' set
32
+ super
33
+ end
34
+
35
+ end
36
+
5
37
  module BuildLabels
6
38
 
7
39
  class Builder
@@ -38,7 +70,9 @@ module BuildLabels
38
70
  def apply_environment
39
71
  @namespaces.each do |ns, struct|
40
72
  struct.each_pair do |name, value|
41
- value.sub!( /^.*$/, `printf #{value}`) if value.to_s =~ /\$/
73
+ next unless value.to_s.include? '$'
74
+ struct[name] = value.gsub(/\$\{(\w+)\}/) { ENV[$1] }
75
+ .gsub(/\$(\w+)/) { ENV[$1] }
42
76
  end
43
77
  end
44
78
  end
@@ -52,11 +86,17 @@ module BuildLabels
52
86
  end
53
87
 
54
88
  def extend_compose(compose_text)
55
- compose = YAML.load compose_text
89
+
90
+ result = YamlMerge::parse_and_process_yaml compose_text
91
+ compose = YamlMerge::deep_copy_without_aliases result
92
+
93
+ # compose = YAML.load compose_text
94
+
56
95
  compose['services'].each do |name, service|
96
+ service.delete_if {|k, v| !%w[image build].include? k }
57
97
  next unless service['build']
58
98
  if service['build'].class == String
59
- service['build'] = { 'context' => service['build'] }
99
+ service['build'] = { 'context' => service['build'] }
60
100
  end
61
101
  service['build']['labels'] ||= []
62
102
  add_namespace :dc, 'docker.service'
@@ -68,9 +108,7 @@ module BuildLabels
68
108
  end
69
109
  end
70
110
  end
71
- compose['services'].delete_if do |name, svc|
72
- ! svc.key?('build')
73
- end
111
+ compose['services'].delete_if do |name, svc| ! svc.key?('build') end
74
112
  puts compose.to_yaml
75
113
  end
76
114
  end
@@ -5,7 +5,7 @@ BuildLabels::CommandLine::COMMANDS[:gitlab] = Class.new do
5
5
  builder.oc.vendor = '$CI_SERVER_URL/$GITLAB_USER_LOGIN'
6
6
  builder.oc.authors = '$CI_SERVER_URL/$GITLAB_USER_LOGIN'
7
7
  builder.oc.revision = '$CI_COMMIT_SHA'
8
- builder.oc['ref.name'] = '$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"'
8
+ builder.oc['ref.name'] = '$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME'
9
9
  builder.oc.source = '$CI_PROJECT_URL'
10
10
  builder.oc.documentation = '$CI_PROJECT_URL'
11
11
  builder.oc.licenses = '$CI_PROJECT_URL'
@@ -21,7 +21,6 @@ module BuildLabels
21
21
  puts "Load env error: #{e.message}"
22
22
  raise "Invalid #{filename} file"
23
23
  end
24
-
25
24
  def run(args)
26
25
  params = {}
27
26
 
@@ -0,0 +1,107 @@
1
+ # https://claude.ai/chat/4cd83f29-8ce4-42b7-8dcd-4f7c6222ab5e
2
+ require 'psych'
3
+
4
+ module YamlMerge
5
+ extend self
6
+
7
+ def deep_merge(base, override)
8
+ result = base.dup
9
+ override.each do |key, value|
10
+ if value.is_a?(Hash) && result[key].is_a?(Hash)
11
+ result[key] = deep_merge(result[key], value)
12
+ elsif value.is_a?(Array) && result[key].is_a?(Array)
13
+ result[key] = (result[key] + value).uniq
14
+ else
15
+ result[key] = value
16
+ end
17
+ end
18
+ result
19
+ end
20
+
21
+ def process_yaml(node, anchors = {})
22
+ case node
23
+ when Psych::Nodes::Mapping
24
+ result = {}
25
+ node.children.each_slice(2) do |key, value|
26
+ processed_key = process_yaml(key, anchors)
27
+ processed_value = process_yaml(value, anchors)
28
+ if key.is_a?(Psych::Nodes::Scalar) && key.anchor
29
+ anchors[key.anchor.to_sym] = processed_value
30
+ end
31
+ if processed_key == '<<'
32
+ if processed_value.is_a?(Array)
33
+ processed_value.each { |v| result = deep_merge(v, result) }
34
+ else
35
+ result = deep_merge(processed_value, result)
36
+ end
37
+ elsif result.key?(processed_key) && result[processed_key].is_a?(Array) && processed_value.is_a?(Array)
38
+ result[processed_key] = (result[processed_key] + processed_value).uniq
39
+ else
40
+ result[processed_key] = processed_value
41
+ end
42
+ end
43
+ node.anchor ? anchors[node.anchor.to_sym] = result : result
44
+ when Psych::Nodes::Sequence
45
+ result = node.children.map { |child| process_yaml(child, anchors) }
46
+ node.anchor ? anchors[node.anchor.to_sym] = result : result
47
+ when Psych::Nodes::Scalar
48
+ result = node.value
49
+ node.anchor ? anchors[node.anchor.to_sym] = result : result
50
+ when Psych::Nodes::Alias
51
+ anchors[node.anchor.to_sym] || node.anchor
52
+ else
53
+ node
54
+ end
55
+ end
56
+
57
+ def parse_and_process_yaml(yaml_string)
58
+ parsed = Psych.parse(yaml_string)
59
+ process_yaml(parsed.root)
60
+ end
61
+
62
+ def deep_copy_without_aliases(obj)
63
+ case obj
64
+ when Hash
65
+ obj.transform_values { |v| deep_copy_without_aliases(v) }
66
+ when Array
67
+ obj.map { |v| deep_copy_without_aliases(v) }
68
+ else
69
+ obj
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+
76
+ # SELF TEST ============================================================================================================
77
+ if File.expand_path($0) == File.expand_path(__FILE__)
78
+ yaml_string = <<~YAML
79
+ defaults: &defaults
80
+ common:
81
+ setting1: value1
82
+ setting2: value2
83
+ arr: [1, 2]
84
+
85
+ node1: &node1
86
+ <<: *defaults
87
+ specific:
88
+ key1: val1
89
+ arr: [3, 4]
90
+
91
+ node2:
92
+ <<: [*defaults, *node1]
93
+ arr: [5]
94
+ nested:
95
+ <<: *defaults
96
+ extra: [a, b]
97
+ YAML
98
+
99
+ result = YamlMerge::parse_and_process_yaml(yaml_string)
100
+ puts result.inspect
101
+
102
+ data_without_aliases = YamlMerge::deep_copy_without_aliases(result)
103
+ puts data_without_aliases.to_yaml
104
+
105
+ # puts Psych.dump(data_without_aliases, indentation: 2, line_width: -1)
106
+
107
+ end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module BuildLabels
2
2
  class Builder
3
- VERSION = '0.0.15'
3
+ VERSION = '0.0.17'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: build-labels
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artyom B
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-26 00:00:00.000000000 Z
11
+ date: 2024-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -103,6 +103,7 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - bin/build-labels
105
105
  - examples/Dockerfile
106
+ - examples/common-compose.yml
106
107
  - examples/simple-compose.yml
107
108
  - lib/build-labels.rb
108
109
  - lib/build-labels/builder.rb
@@ -112,6 +113,7 @@ files:
112
113
  - lib/build-labels/command_print.rb
113
114
  - lib/build-labels/command_to_compose.rb
114
115
  - lib/build-labels/command_to_dockerfiles.rb
116
+ - lib/build-labels/yaml_merge.rb
115
117
  - lib/version.rb
116
118
  homepage: https://rubygems.org/gems/build-labels
117
119
  licenses: