-
Notifications
You must be signed in to change notification settings - Fork 29
/
Rakefile
287 lines (245 loc) · 8.34 KB
/
Rakefile
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
LATEST_UBUNTU_VERSION = "jammy"
def download(url)
require "net/http"
url = URI.parse(url)
Net::HTTP.start(url.hostname, url.port, :use_ssl => (url.scheme == "https")) do |http|
path = url.path
path += "?#{url.query}" if url.query
request = Net::HTTP::Get.new(url.path)
http.request(request) do |response|
case response
when Net::HTTPSuccess
return response.read_body
else
response.error!
end
end
end
end
def default_ubuntu_version(ruby_version)
if ruby_version < "3.1"
"focal"
else
"jammy"
end
end
def default_ruby_version
ENV['ruby_version'] || '3.3.0'
end
def ubuntu_version(ruby_version)
ENV.fetch("ubuntu_version", default_ubuntu_version(ruby_version))
end
def ubuntu_latest_version?(ubuntu_version)
LATEST_UBUNTU_VERSION == ubuntu_version
end
@ruby_versions = nil
def ruby_versions
unless @ruby_versions
require "psych"
releases_yml = download("https://raw.githubusercontent.com/ruby/www.ruby-lang.org/master/_data/releases.yml")
releases = Psych.respond_to?(:unsafe_load) ? Psych.unsafe_load(releases_yml, filename: "releases.yml") : Psych.load(releases_yml, "releases.yml")
versions = {}
releases.each do |release|
version = release["version"]
ver2 = version.split('.')[0,2].join('.')
versions[ver2] ||= []
versions[ver2] << version
end
@ruby_versions = versions
end
@ruby_versions
end
def ruby_latest_stable_version
ruby_versions.keys.sort.reverse.each do |ver2|
ruby_versions[ver2].sort.reverse.each do |ver|
if ver.match?(/\A\d+\.\d+\.\d+\z/)
return ver
end
end
end
nil
end
def ruby_latest_version?(version)
if ENV.fetch("latest_tag", "false") == "false"
false
else
if not ubuntu_latest_version?(ubuntu_version(version))
false
else
version == ruby_latest_stable_version
end
end
end
def ruby_latest_full_version?(version)
ver2 = version.split('.')[0,2].join('.')
ruby_versions[ver2][0] == version
end
def ruby_version_exist?(version)
return true if version.start_with?("master")
require "net/http"
require "uri"
ver2 = version.split('.')[0,2].join('.')
Net::HTTP.get_response(URI.parse("https://cache.ruby-lang.org/pub/ruby/#{ver2}/ruby-#{version}.tar.gz")).code == "200"
end
namespace :debug do
task :versions do
pp ruby_versions
end
task :latest_stable_version do
p latest_stable_version: ruby_latest_stable_version
end
task :latest_version do
ENV["latest_tag"] = "true"
ruby_version = ENV["ruby_version"]
p latest_version: ruby_latest_version?(ruby_version)
end
task :latest_full_version do
ruby_version = ENV["ruby_version"]
p latest_full_version: ruby_latest_full_version?(ruby_version)
end
task :version_exist do
ruby_version = ENV["ruby_version"]
p version_exist: ruby_version_exist?(ruby_version)
end
end
namespace :docker do
def registry_name
ENV.fetch("registry_name", "rubylang")
end
def docker_image_name
"#{registry_name}/ruby"
end
def get_ruby_master_head_hash
if ENV.key?("GITHUB_ACTION") && File.exist?(cache_path = "/tmp/ruby-docker-images")
return File.read(cache_path)
end
head_hash = `curl -H 'accept: application/vnd.github.v3.sha' https://api.github.com/repos/ruby/ruby/commits/master`.chomp
if cache_path
File.write(cache_path, head_hash)
end
head_hash
end
def get_ruby_version_at_commit(commit_hash)
raise ArgumentError, "Invalid commit_hash: #{commit_hash.inspect}" unless commit_hash.match?(/\A[a-z0-9]+\z/)
version_h = download("https://raw.githubusercontent.com/ruby/ruby/#{commit_hash}/include/ruby/version.h")
version_info = {}
version_h.each_line do |line|
case line
when /\A#define RUBY_[A-Z_]*VERSION_([A-Z][A-Z][A-Z_0-9]*) (\d\d*)$/
version_info[$1.to_sym] = $2
end
end
return version_info
end
def make_tags(ruby_version, version_suffix=nil, tag_suffix=nil)
ruby_version_mm = ruby_version.split('.')[0,2].join('.')
if /\Amaster(?::([\da-f]+))?\z/ =~ ruby_version
commit_hash = Regexp.last_match[1] || get_ruby_master_head_hash
ruby_version = "master:#{commit_hash}"
tags = ["master#{version_suffix}", "master#{version_suffix}-#{commit_hash}"]
else
tags = ["#{ruby_version}#{version_suffix}"]
tags << "#{ruby_version_mm}#{version_suffix}" if ruby_latest_full_version?(ruby_version)
end
tags.collect! {|t| "#{docker_image_name}:#{t}-#{ubuntu_version(ruby_version)}#{tag_suffix}" }
tags.push "#{docker_image_name}:latest" if ruby_latest_version?(ruby_version)
return ruby_version, tags
end
def each_nightly_tag(ruby_version, tags)
return [] unless (ENV['nightly'] == 'true') && ruby_version.start_with?('master:')
commit_hash = ruby_version.split(":")[1]
commit_hash_re = /\b#{Regexp.escape(commit_hash)}\b/
image_name = tags.find {|x| x.match? commit_hash_re }
today = Time.now.utc.strftime('%Y%m%d')
yield image_name, image_name.sub(commit_hash_re, "nightly-#{today}")
yield image_name, image_name.sub(commit_hash_re, "nightly")
end
task :build do
ruby_version = default_ruby_version
unless ruby_version_exist?(ruby_version)
abort "unknown ruby version: #{ruby_version}"
end
version_suffix = ENV["image_version_suffix"]
tag_suffix = ENV["tag_suffix"]
tag = ENV["tag"] || ""
target = ENV.fetch("target", "ruby")
arch = ENV.fetch("arch", "linux/amd64")
ruby_version, tags = make_tags(ruby_version, version_suffix, tag_suffix)
tags << "#{docker_image_name}:#{tag}" if !tag.empty?
build_args = [
"RUBY_VERSION=#{ruby_version}",
"BASE_IMAGE_TAG=#{ubuntu_version(ruby_version)}"
]
if ruby_version.start_with?("master:")
commit_hash = ruby_version.split(":")[1]
version_info = get_ruby_version_at_commit(commit_hash)
ruby_so_suffix = version_info.values_at(:MAJOR, :MINOR, :TEENY).join(".")
build_args << "RUBY_SO_SUFFIX=#{ruby_so_suffix}"
end
%w(cppflags optflags).each do |name|
build_args << %Q(#{name}=#{ENV[name]}) if ENV.key?(name)
end
unless File.directory?("tmp/ruby")
FileUtils.mkdir_p("tmp/ruby")
IO.write('tmp/ruby/.keep', '')
end
build_cmd_args = arch =~ /arm/ ? ['buildx', 'build', '--platform', arch, '--load'] : ['build']
sh 'docker', *build_cmd_args, '-f', 'Dockerfile',
*tags.map {|tag| ["-t", tag] }.flatten,
*build_args.map {|arg| ["--build-arg", arg] }.flatten,
'--target', target,
'.'
sh 'docker', 'images'
each_nightly_tag(ruby_version, tags) do |image_name, tag|
sh 'docker', 'tag', image_name, tag
end
end
task :push do
ruby_version = ENV['ruby_version'] || '2.6.1'
unless ruby_version_exist?(ruby_version)
abort "unknown ruby version: #{ruby_version}"
end
version_suffix = ENV["image_version_suffix"]
tag_suffix = ENV["tag_suffix"]
ruby_version, tags = make_tags(ruby_version, version_suffix, tag_suffix)
tags.each do |tag|
if ENV['registry_name'].start_with?('ghcr.io')
docker_tag = "rubylang/#{tag.split("/").last}"
sh 'docker', 'tag', docker_tag, tag
end
sh 'docker', 'push', tag
end
each_nightly_tag(ruby_version, tags) do |_, tag|
if ENV['registry_name'].start_with?('ghcr.io')
docker_tag = "rubylang/#{tag.split("/").last}"
sh 'docker', 'tag', docker_tag, tag
end
sh 'docker', 'push', tag
end
end
namespace :manifest do
task :create do
ruby_version = ENV.fetch("ruby_version")
architectures = ENV.fetch("architectures").split(' ')
manifest_suffix = ENV.fetch("manifest_suffix", nil)
image_version_suffix = ENV["image_version_suffix"]
_, tags = make_tags(ruby_version, image_version_suffix)
amend_args = architectures.map {|arch|
manifest_name = "#{tags[0]}-#{arch}"
manifest_name = "#{manifest_name}-#{manifest_suffix}" if manifest_suffix
['--amend', manifest_name]
}.flatten
tags.each do |tag|
sh 'docker', 'manifest', 'create', "#{tag}", *amend_args
end
end
task :push do
ruby_version = ENV["ruby_version"]
image_version_suffix = ENV["image_version_suffix"]
_, tags = make_tags(ruby_version, image_version_suffix)
tags.each do |tag|
sh 'docker', 'manifest', 'push', "#{tag}"
end
end
end
end