build-labels 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64d76686ba12eb2b69d551840ee5d99f05353e75e6db83c06ae9352fe36f63f2
4
- data.tar.gz: 2b1118b96e92281fce14722e18b6b898ed650c8b14a7e036ca581fd6fa72a267
3
+ metadata.gz: 7c019e99a75702daa4a23d0b2910cc831042d71b2994c9b647f165e1b9a83593
4
+ data.tar.gz: d3ef13fbd884faa4ff971af506fe21e1dd9b68db7e4912c852f48747616efbe8
5
5
  SHA512:
6
- metadata.gz: c339263b0003bf89d8f428678bba701c7be56b24c63381095cece85cb9bea491c726497d328a0090c8e1b43a251fa98df413a2352d8c6df36093dba478f8b7dd
7
- data.tar.gz: 85dbc77af8be5fdb2dfa177a98d965e178d2c2ab3d19ca3c3043a38bc51c29a251b65857e97cfc6cd010a042942542cfb7fe6d5c6f073c1012c6a2549debca9b
6
+ metadata.gz: 7faa19f70fd4bd93dca085100b0eb26ce8d77c8ff1f12653d9bb8ef185767ebba4fa9f0347fe0c9ab4be3d8eaf21f07400ce380e54010b4c0be9417592af29d9
7
+ data.tar.gz: 7b7e772113a9e9c48cc467d9aa0538fd566f98083661247585d96be6beff03f65b7d834e1b82599d79e2638fb02bde448b4fd497d19112f36338831a21e0bfb7
data/bin/build-labels CHANGED
@@ -5,7 +5,10 @@ $LOAD_PATH << lib
5
5
 
6
6
  require 'build-labels'
7
7
  require 'build-labels/command_line'
8
+ require 'build-labels/command_to_compose'
9
+ require 'build-labels/command_to_dockerfiles'
8
10
  require 'build-labels/command_gitlab'
11
+ require 'build-labels/command_print'
9
12
 
10
13
  include BuildLabels
11
14
 
@@ -0,0 +1,2 @@
1
+ FROM ruby:3.1.2-alpine AS deploy
2
+ CMD sh -c ' echo Build $CI_COMMIT_BRANCH @ $CI_COMMIT_SHORT_SHA $CI_COMMIT_TIMESTAMP '
@@ -43,9 +43,15 @@ module BuildLabels
43
43
  end
44
44
  end
45
45
 
46
- def extend_compose(compose_text)
46
+ def each_labels
47
47
  generate_label_schema
48
48
  apply_environment
49
+ @namespaces.each do |ns, values|
50
+ yield ns, values
51
+ end
52
+ end
53
+
54
+ def extend_compose(compose_text)
49
55
  compose = YAML.load compose_text
50
56
  compose['services'].each do |name, service|
51
57
  next unless service['build']
@@ -54,7 +60,7 @@ module BuildLabels
54
60
  end
55
61
  service['build']['labels'] ||= []
56
62
 
57
- @namespaces.each do |ns, values|
63
+ each_labels do |ns, values|
58
64
  values.each_pair do |k,v|
59
65
  service['build']['labels'] << "#{ns}.#{k}=#{v}" unless v.to_s.empty?
60
66
  end
@@ -1,7 +1,7 @@
1
1
  require_relative 'command_line'
2
2
 
3
3
  BuildLabels::CommandLine::COMMANDS[:gitlab] = Class.new do
4
- def run(builder, params)
4
+ def run(builder, params, compose_text)
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'
@@ -23,6 +23,9 @@ BuildLabels::CommandLine::COMMANDS[:gitlab] = Class.new do
23
23
  builder.gitlab.cijoburl = '$CI_JOB_URL'
24
24
  builder.gitlab.mrurl = '$CI_PROJECT_URL/-/merge_requests/$CI_MERGE_REQUEST_ID'
25
25
  builder.gitlab.tag = '$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA'
26
+ builder.gitlab.commit_branch = '$CI_COMMIT_BRANCH'
27
+ builder.gitlab.commit_short_sha = '$CI_COMMIT_SHORT_SHA'
28
+ builder.gitlab.commit_timestamp = '$CI_COMMIT_TIMESTAMP'
26
29
 
27
30
  # org.opencontainers.image.title BUILDTITLE=$(echo $CI_PROJECT_TITLE | tr " " "_")
28
31
  # org.opencontainers.image.description
@@ -47,21 +47,23 @@ module BuildLabels
47
47
  o.on('-e', '--env FILE', 'Load .build_info FILE') { load_env _1 }
48
48
  o.on('-n', '--no-env', 'Do not process env variables') { true }
49
49
  o.on('-h', '--help') { puts o; exit }
50
- o.parse! args, into: params
50
+ rest = o.parse! args, into: params
51
51
 
52
- raise 'Compose file not defined' if $stdin.tty? && !params[:compose]
53
-
54
- command = args.shift || ''
55
- raise "Unknown command: #{command}" unless COMMANDS.key?(command.to_sym)
56
52
 
57
53
  compose_text = File.read(params[:compose]) if params[:compose]
58
54
  compose_text ||= STDIN.read unless $stdin.tty?
59
55
 
60
56
  builder = Builder.new
61
- COMMANDS[command.to_sym].run builder, params
57
+
62
58
  # eval $(grep -v -e '^#' .build_info | xargs -I {} echo export \'{}\') && echo $CI_COMMIT_AUTHOR
59
+ if rest === ['gitlab'] && compose_text
60
+ rest = ['gitlab', 'to_compose']
61
+ end
63
62
 
64
- builder.extend_compose compose_text
63
+ rest.each do |command|
64
+ raise "Unknown command: #{command}" unless COMMANDS.key?(command.to_sym)
65
+ COMMANDS[command.to_sym].run builder, params, compose_text
66
+ end
65
67
  rescue => e
66
68
  puts e.message
67
69
  exit 1
@@ -0,0 +1,15 @@
1
+ require_relative 'command_line'
2
+
3
+ BuildLabels::CommandLine::COMMANDS[:print] = Class.new do
4
+ def run(builder, params, compose_text)
5
+ builder.each_labels do |ns, values|
6
+ values.each_pair do |k,v|
7
+ puts "#{ns}.#{k}=#{v}" unless v.to_s.empty?
8
+ end
9
+ end
10
+ end
11
+
12
+ def help = 'Print labels to stdout'
13
+ end.new
14
+
15
+
@@ -0,0 +1,13 @@
1
+ require_relative 'command_line'
2
+
3
+ BuildLabels::CommandLine::COMMANDS[:to_compose] = Class.new do
4
+ def run(builder, params, compose_text)
5
+ raise 'Compose file not defined' unless compose_text
6
+
7
+ builder.extend_compose compose_text
8
+ end
9
+
10
+ def help = 'Add labels to all build sections of docker-compose file'
11
+ end.new
12
+
13
+
@@ -0,0 +1,34 @@
1
+ require_relative 'command_line'
2
+
3
+ BuildLabels::CommandLine::COMMANDS[:to_dockerfiles] = Class.new do
4
+ def run(builder, params, compose_text)
5
+ raise 'Compose file not defined' unless compose_text
6
+ compose_dir = params[:compose] ? File.dirname(params[:compose]) : '.'
7
+
8
+ compose = YAML.load compose_text
9
+ compose['services'].each do |name, svc|
10
+ next unless svc['build']
11
+
12
+ dockerfile = svc['build'].is_a?(String) ? svc['build'] : [svc['build']['context'], svc['build']['dockerfile']].compact.join('/')
13
+ dockerfile = File.expand_path dockerfile, compose_dir
14
+ raise "file ot found: #{dockerfile}" unless File.exist? dockerfile
15
+
16
+ dockerfile_lines = File.readlines(dockerfile).map(&:strip)
17
+
18
+ builder.each_labels do |ns, values|
19
+ values.each_pair do |k, v|
20
+ next if v.to_s.empty?
21
+
22
+ name = "#{ns}.#{k}".gsub('.', '_').upcase
23
+ line = "ENV #{name}=\"#{v}\""
24
+ dockerfile_lines.push line unless dockerfile_lines.grep(/ENV\s+#{name}=/).any?
25
+ end
26
+ end
27
+ File.write dockerfile, dockerfile_lines.join("\n")
28
+ end
29
+ end
30
+
31
+ def help = 'Add ENVs to Dockerfiles from docker-compose file'
32
+ end.new
33
+
34
+
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module BuildLabels
2
2
  class Builder
3
- VERSION = '0.0.5'
3
+ VERSION = '0.0.6'
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artyom B
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-23 00:00:00.000000000 Z
11
+ date: 2022-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -102,11 +102,15 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - bin/build-labels
105
+ - examples/Dockerfile
105
106
  - examples/simple-compose.yml
106
107
  - lib/build-labels.rb
107
108
  - lib/build-labels/builder.rb
108
109
  - lib/build-labels/command_gitlab.rb
109
110
  - lib/build-labels/command_line.rb
111
+ - lib/build-labels/command_print.rb
112
+ - lib/build-labels/command_to_compose.rb
113
+ - lib/build-labels/command_to_dockerfiles.rb
110
114
  - lib/version.rb
111
115
  homepage: https://rubygems.org/gems/build-labels
112
116
  licenses: