-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgen_gitlab_yml.rb
executable file
·138 lines (117 loc) · 4.17 KB
/
gen_gitlab_yml.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env ruby
# gitlab-ci YAML generator for dynamic build matrices
require 'getoptlong'
###############################################################################
USAGE_STR = <<END_OF_USAGE
./gen_gitlab_yml.rb [options]
--prefix -p <prefix> specify the image prefix to which the tag will be appended (require)
--flavor -f <flavor> specify build <flavor>, can do multiple (default: 'all', all flavors)
--onload -o <version> specify onload <version> to build (default is 'latest')
--zf <truthy> build with TCPDirect (zf) (or not, if optional <truthy> is '0' or 'false')
--template -t <truthy> generate template section
--help -h show this help
END_OF_USAGE
$opts = {
:template => false,
:prefix => nil,
:ooversion => 'latest',
:flavors => [],
:zf => false
}
begin
GetoptLong.new(
[ '--prefix', '-p', GetoptLong::REQUIRED_ARGUMENT ],
[ '--flavor', '-f', GetoptLong::REQUIRED_ARGUMENT ],
[ '--onload', '-o', GetoptLong::REQUIRED_ARGUMENT ],
[ '--zf', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--template', '-t', GetoptLong::OPTIONAL_ARGUMENT ],
[ '--help', '-h', GetoptLong::NO_ARGUMENT ]
).each do |opt, arg|
case opt
when '--prefix'
if !$opts[:prefix].nil? then
STDERR << "ERROR: --prefix can only be specified once\n"
exit(-1)
end
$opts[:prefix] = arg
when '--onload'
if $opts[:ooversion] != 'latest' then
STDERR << "ERROR: --onload can only be specified once\n"
exit(-1)
end
$opts[:ooversion] = arg
when '--flavor'
$opts[:flavors] << arg
when '--zf'
$opts[:zf] = true
$opts[:zf] = false if arg == '0' || arg.downcase == 'false'
when '--template'
$opts[:template] = true
$opts[:template] = false if arg == '0' || arg.downcase == 'false'
when '--help'
$opts[:action] = :help
end
end
rescue StandardError => e
STDERR << "ERROR: #{e.to_s}\n"
exit(-1)
end
if $opts[:prefix].nil? && !$opts[:template] then
STDERR << "ERROR: --prefix must be specified\n"
exit(-1)
end
###############################################################################
def gen_template()
return <<~YML
# Builds all the flavors for the given image tag
# variables:
# IMAGE_PREFIX
# ONLOAD_VERSION
# FLAVOR
# USE_ZF
.docker_build_onload_template:
stage: build
image: docker:latest
services:
- docker:dind
retry: 1
before_script:
- apk add --update ruby
- echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin
script:
- |
IMAGE_NAME_DEST=$(./build_onload_image.rb -o $ONLOAD_VERSION -f $FLAVOR -a "$IMAGE_PREFIX" --zf $USE_ZF --gettag)
docker pull $IMAGE_NAME_DEST || true
./build_onload_image.rb -o $ONLOAD_VERSION -f $FLAVOR -a "$IMAGE_PREFIX" --zf $USE_ZF --execute --push
IMAGE_NAME_SHA=$(./build_onload_image.rb -o $ONLOAD_VERSION -f $FLAVOR -a "$IMAGE_PREFIX$CI_COMMIT_SHA-" --zf $USE_ZF --gettag)
docker tag $IMAGE_NAME_DEST $IMAGE_NAME_SHA
docker push $IMAGE_NAME_SHA
YML
end
###############################################################################
def gen_build_stanza(prefix, flavor, ooversion, use_zf)
zf = use_zf ? "zf" : ""
zfarg = use_zf ? "--zf " : ""
return <<~YML
onload#{zf}-#{flavor}-#{ooversion}:
stage: build
extends: .docker_build_onload_template
variables:
IMAGE_PREFIX: "#{prefix}"
ONLOAD_VERSION: "#{ooversion}"
FLAVOR: "#{flavor}"
USE_ZF: "#{use_zf.to_s}"
YML
end
###############################################################################
if $opts[:template] then
STDOUT << gen_template()
end
if $opts[:flavors].empty? then
%x{ ls -d */ | grep -v _archive | sed 's/\\///g' | sort }.each_line do |flavor|
$opts[:flavors] << flavor.strip
end
end
$opts[:flavors].each do |flavor|
STDOUT << gen_build_stanza($opts[:prefix], flavor, $opts[:ooversion], $opts[:zf])
end