build-labels 0.0.1 → 0.0.3
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 +2 -0
- data/examples/simple-compose.yml +9 -0
- data/lib/build-labels/builder.rb +58 -3
- data/lib/build-labels/command_gitlab.rb +37 -0
- data/lib/build-labels/command_line.rb +26 -19
- data/lib/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06b8cbfa043cb60a1bb85aa96efea7fde084b43905f96b8e633bc5ef8b989749
|
4
|
+
data.tar.gz: d0bcb7c85b7d1ca878573cb1a0ebe46807447f134d45df0b185b48ac84d18b38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e67b1a55324168da25860c0dd1d0792670f839b0326f98c9861e39f9992e2cad47a31f1a429e211c8ac7660940b93a6d9a84be430b3fca4f86226e6ef28f8e81
|
7
|
+
data.tar.gz: 6637903d8537f8a02be3b2abad7a42685958cb6633048d245572cd9bef00069acb929475193b36c733322269b1d160527a119ae0cd1bed4b024c550c926863a8
|
data/bin/build-labels
CHANGED
data/lib/build-labels/builder.rb
CHANGED
@@ -1,12 +1,67 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'yaml'
|
3
|
-
require '
|
4
|
-
|
3
|
+
require 'ostruct'
|
4
|
+
require 'pp'
|
5
5
|
module BuildLabels
|
6
6
|
|
7
7
|
class Builder
|
8
8
|
|
9
|
-
def
|
9
|
+
def add_namespace(name, path, labels=[])
|
10
|
+
@namespaces ||= {}
|
11
|
+
@namespaces[path] = OpenStruct.new
|
12
|
+
self.class.send :define_method, name, -> { @namespaces[path] }
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
# https://github.com/opencontainers/image-spec/blob/v1.0.1/annotations.md
|
17
|
+
add_namespace :oc, 'org.opencontainers.image', [
|
18
|
+
:vendor, :authors, :revision, :source, :documentation, :licenses, :url,
|
19
|
+
:version, :'ref.name', :title, :description, :created
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
23
|
+
def generate_label_schema
|
24
|
+
# Label Schema compatibility
|
25
|
+
add_namespace :ls, 'org.label-schema'
|
26
|
+
self.ls['build-date'] = self.oc.created
|
27
|
+
self.ls.url = self.oc.url
|
28
|
+
self.ls['vcs-url'] = self.oc.source
|
29
|
+
self.ls.version = self.oc.version
|
30
|
+
self.ls['vcs-ref'] = self.oc.revision
|
31
|
+
self.ls.vendor = self.oc.vendor
|
32
|
+
self.ls.name = self.oc.title
|
33
|
+
self.ls.description = self.oc.description
|
34
|
+
self.ls.usage = self.oc.documentation
|
35
|
+
self.ls['schema-version'] = '1.0'
|
36
|
+
end
|
37
|
+
|
38
|
+
def apply_environment
|
39
|
+
@namespaces.each do |ns, struct|
|
40
|
+
struct.each_pair do |name, value|
|
41
|
+
value.sub! /^.*$/, `printf #{value}` if value.to_s =~ /\$/
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def extend_compose(compose_text)
|
47
|
+
generate_label_schema
|
48
|
+
apply_environment
|
49
|
+
compose = YAML.load compose_text
|
50
|
+
compose['services'].each do |name, service|
|
51
|
+
next unless service['build']
|
52
|
+
service['build']['labels'] ||= []
|
53
|
+
|
54
|
+
if service['build'].class == String
|
55
|
+
service['build'] = { 'context' => service['build'] }
|
56
|
+
end
|
57
|
+
|
58
|
+
@namespaces.each do |ns, values|
|
59
|
+
values.each_pair do |k,v|
|
60
|
+
service['build']['labels'] << "#{ns}.#{k}=#{v}" unless v.to_s.empty?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
puts compose.to_yaml
|
10
65
|
end
|
11
66
|
end
|
12
67
|
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'command_line'
|
2
|
+
|
3
|
+
BuildLabels::CommandLine::COMMANDS[:gitlab] = Class.new do
|
4
|
+
def run(builder, params)
|
5
|
+
builder.oc.vendor = '$CI_SERVER_URL/$GITLAB_USER_LOGIN'
|
6
|
+
builder.oc.authors = '$CI_SERVER_URL/$GITLAB_USER_LOGIN'
|
7
|
+
builder.oc.revision = '$CI_COMMIT_SHA'
|
8
|
+
builder.oc['ref.name'] = '$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"'
|
9
|
+
builder.oc.source = '$CI_PROJECT_URL'
|
10
|
+
builder.oc.documentation = '$CI_PROJECT_URL'
|
11
|
+
builder.oc.licenses = '$CI_PROJECT_URL'
|
12
|
+
builder.oc.url = '$CI_PROJECT_URL'
|
13
|
+
builder.oc.title = '$CI_PROJECT_TITLE'
|
14
|
+
builder.oc.created = '$CI_JOB_STARTED_AT'
|
15
|
+
builder.oc.version = '$CI_COMMIT_REF_NAME' # $CI_COMMIT_TAG
|
16
|
+
|
17
|
+
builder.add_namespace :gitlab, 'com.gitlab.ci'
|
18
|
+
builder.gitlab.user = '$CI_SERVER_URL/$GITLAB_USER_LOGIN'
|
19
|
+
builder.gitlab.email = '$GITLAB_USER_EMAIL'
|
20
|
+
builder.gitlab.tagorbranch = '$CI_COMMIT_REF_NAME'
|
21
|
+
builder.gitlab.pipelineurl = '$CI_PIPELINE_URL'
|
22
|
+
builder.gitlab.commiturl = '$CI_PROJECT_URL/commit/$CI_COMMIT_SHA'
|
23
|
+
builder.gitlab.cijoburl = '$CI_JOB_URL'
|
24
|
+
builder.gitlab.mrurl = '$CI_PROJECT_URL/-/merge_requests/$CI_MERGE_REQUEST_ID'
|
25
|
+
builder.gitlab.tag = '$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA'
|
26
|
+
|
27
|
+
# org.opencontainers.image.title BUILDTITLE=$(echo $CI_PROJECT_TITLE | tr " " "_")
|
28
|
+
# org.opencontainers.image.description
|
29
|
+
# date '+%FT%T%z' | sed -E -n 's/(\+[0-9]{2})([0-9]{2})$/\1:\2/p' #rfc 3339 date
|
30
|
+
# org.opencontainers.image.created
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def help = 'Use GitLab CI variables'
|
35
|
+
end.new
|
36
|
+
|
37
|
+
|
@@ -1,18 +1,25 @@
|
|
1
1
|
require_relative '../version'
|
2
|
+
require 'optparse'
|
2
3
|
|
3
4
|
module BuildLabels
|
4
5
|
module CommandLine
|
5
6
|
COMMANDS = {}
|
6
7
|
|
7
8
|
class << self
|
8
|
-
def load_env
|
9
|
-
File.read(
|
10
|
-
.map {
|
11
|
-
|
12
|
-
|
9
|
+
def load_env(filename)
|
10
|
+
File.read(filename).lines.map(&:strip).grep_v(/^\s*#/).reject(&:empty?)
|
11
|
+
.map {
|
12
|
+
if _1 =~ /([^=]+)=(.*)/
|
13
|
+
a, k,v = Regexp.last_match.to_a
|
14
|
+
ENV[k] = v
|
15
|
+
[k,v]
|
16
|
+
else
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
}.compact.to_h
|
13
20
|
rescue =>e
|
14
21
|
puts "Load env error: #{e.message}"
|
15
|
-
raise
|
22
|
+
raise "Invalid #{filename} file"
|
16
23
|
end
|
17
24
|
|
18
25
|
def run(args)
|
@@ -23,9 +30,9 @@ module BuildLabels
|
|
23
30
|
o.version = "#{BuildLabels::Builder::VERSION}"
|
24
31
|
|
25
32
|
usage = [
|
26
|
-
'
|
27
|
-
'cat
|
28
|
-
'
|
33
|
+
'build-labels -c docker-compose.yml gitlab',
|
34
|
+
'cat docker-compose.yml | build-labels gitlab',
|
35
|
+
'build-labels gitlab < docker-compose.yml'
|
29
36
|
]
|
30
37
|
o.banner = "Version: #{o.version}\nUsage:\n\t#{usage.join "\n\t"}"
|
31
38
|
o.separator ''
|
@@ -36,25 +43,25 @@ module BuildLabels
|
|
36
43
|
o.separator 'Options:'
|
37
44
|
|
38
45
|
# in all caps are required
|
39
|
-
o.on('-
|
40
|
-
o.on('-e', '--env', 'Load .
|
41
|
-
o.on('', '--name STACK_NAME', 'Define stack name')
|
42
|
-
o.on('', '--ingress', 'Generate ingress labels') { true }
|
43
|
-
o.on('', '--traefik', 'Generate traefik labels') { true }
|
44
|
-
o.on('', '--traefik_tls', 'Generate traefik tls labels') { true }
|
45
|
-
o.on('', '--host_sed /from/to/', 'Sed ingress host /\\*/dev.*/')
|
46
|
+
o.on('-c', '--compose COMPOSE_FILE', 'Compose file')
|
47
|
+
o.on('-e', '--env FILE', 'Load .build_info FILE') { load_env _1 }
|
46
48
|
o.on('-n', '--no-env', 'Do not process env variables') { true }
|
47
49
|
o.on('-h', '--help') { puts o; exit }
|
48
50
|
o.parse! args, into: params
|
49
51
|
|
50
|
-
raise '
|
52
|
+
raise 'Compose file not defined' if $stdin.tty? && !params[:compose]
|
51
53
|
|
52
54
|
command = args.shift || ''
|
53
55
|
raise "Unknown command: #{command}" unless COMMANDS.key?(command.to_sym)
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
+
compose_text = File.read(params[:compose]) if params[:compose]
|
58
|
+
compose_text ||= STDIN.read unless $stdin.tty?
|
57
59
|
|
60
|
+
builder = Builder.new
|
61
|
+
COMMANDS[command.to_sym].run builder, params
|
62
|
+
# eval $(grep -v -e '^#' .build_info | xargs -I {} echo export \'{}\') && echo $CI_COMMIT_AUTHOR
|
63
|
+
|
64
|
+
builder.extend_compose compose_text
|
58
65
|
rescue => e
|
59
66
|
puts e.message
|
60
67
|
exit 1
|
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.3
|
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-
|
11
|
+
date: 2022-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -102,8 +102,10 @@ extensions: []
|
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
104
|
- bin/build-labels
|
105
|
+
- examples/simple-compose.yml
|
105
106
|
- lib/build-labels.rb
|
106
107
|
- lib/build-labels/builder.rb
|
108
|
+
- lib/build-labels/command_gitlab.rb
|
107
109
|
- lib/build-labels/command_line.rb
|
108
110
|
- lib/version.rb
|
109
111
|
homepage: https://rubygems.org/gems/build-labels
|