build-labels 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/build-labels +3 -0
- data/examples/Dockerfile +10 -0
- data/lib/build-labels/builder.rb +8 -2
- data/lib/build-labels/command_gitlab.rb +4 -1
- data/lib/build-labels/command_line.rb +9 -7
- data/lib/build-labels/command_print.rb +15 -0
- data/lib/build-labels/command_to_compose.rb +13 -0
- data/lib/build-labels/command_to_dockerfiles.rb +35 -0
- data/lib/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 598310c75d2bded8e415fdaf0ddda62dcb21c449781a072e8fc50087ac1e0afc
|
4
|
+
data.tar.gz: bb0a28a19f091161d3d5e9fd8d2537c8cd749abe5a3a14421ebb26572a8db537
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05f602bb1884a76bbab5de538103eecda267c7d369a39cfe297ef72437555b38cc3a10a375d9d3af55aea95b9c5d320eafb3dbf0d8a7b48322e6d6691b93411b
|
7
|
+
data.tar.gz: d73aa50550823aac30f3abf3d69721a1cec93516ed36891c27bb7143f8c1313edeaae2dcac89272cb910d793a9b7d1873e245f7e7942224401870a8c9bdfba4e
|
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
|
|
data/examples/Dockerfile
ADDED
@@ -0,0 +1,10 @@
|
|
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 '
|
3
|
+
ENV ORG_OPENCONTAINERS_IMAGE_VENDOR="/"
|
4
|
+
ENV ORG_OPENCONTAINERS_IMAGE_AUTHORS="/"
|
5
|
+
ENV COM_GITLAB_CI_USER="/"
|
6
|
+
ENV COM_GITLAB_CI_COMMITURL="/commit/"
|
7
|
+
ENV COM_GITLAB_CI_MRURL="/-/merge_requests/"
|
8
|
+
ENV COM_GITLAB_CI_TAG=":"
|
9
|
+
ENV ORG_LABEL-SCHEMA_VENDOR="/"
|
10
|
+
ENV ORG_LABEL-SCHEMA_SCHEMA-VERSION="1.0"
|
data/lib/build-labels/builder.rb
CHANGED
@@ -43,9 +43,15 @@ module BuildLabels
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
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
|
-
|
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
|
-
|
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
|
-
|
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,35 @@
|
|
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']
|
13
|
+
dockerfile = File.join dockerfile, (svc['build']['dockerfile'] || 'Dockerfile')
|
14
|
+
dockerfile = File.expand_path dockerfile, compose_dir
|
15
|
+
raise "file ot found: #{dockerfile}" unless File.exist? dockerfile
|
16
|
+
|
17
|
+
dockerfile_lines = File.readlines(dockerfile).map(&:strip)
|
18
|
+
|
19
|
+
builder.each_labels do |ns, values|
|
20
|
+
values.each_pair do |k, v|
|
21
|
+
next if v.to_s.empty?
|
22
|
+
|
23
|
+
name = "#{ns}.#{k}".gsub('.', '_').upcase
|
24
|
+
line = "ENV #{name}=\"#{v}\""
|
25
|
+
dockerfile_lines.push line unless dockerfile_lines.grep(/ENV\s+#{name}=/).any?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
File.write dockerfile, dockerfile_lines.join("\n")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def help = 'Add ENVs to Dockerfiles from docker-compose file'
|
33
|
+
end.new
|
34
|
+
|
35
|
+
|
data/lib/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.7
|
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
|
+
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:
|