-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathRakefile
More file actions
358 lines (311 loc) · 11.6 KB
/
Rakefile
File metadata and controls
358 lines (311 loc) · 11.6 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
LATEST_UBUNTU_VERSION = "noble"
LATEST_RUBY_VERSION = "3.4"
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
desc "Display all available Ruby versions grouped by major.minor"
task :versions do
pp ruby_versions
end
desc "Show the latest stable Ruby version"
task :latest_stable_version do
p latest_stable_version: ruby_latest_stable_version
end
desc "Check if the specified Ruby version is the latest (env: ruby_version)"
task :latest_version do
ENV["latest_tag"] = "true"
ruby_version = ENV["ruby_version"]
p latest_version: ruby_latest_version?(ruby_version)
end
desc "Check if the specified Ruby version is the latest full version (env: ruby_version)"
task :latest_full_version do
ruby_version = ENV["ruby_version"]
p latest_full_version: ruby_latest_full_version?(ruby_version)
end
desc "Check if the specified Ruby version exists (env: ruby_version)"
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
# Define a helper method to try different approaches with consistent error handling
def try_fetch_hash(method_name, &block)
begin
result = yield
if result && !result.empty?
p "Retrieved master head hash using #{method_name}"
return result
else
p "get_ruby_master_head_hash with #{method_name} failed"
return nil
end
rescue => e
p "get_ruby_master_head_hash with #{method_name} failed: #{e.message}"
return nil
end
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 = nil
# First attempt: Using Net::HTTP
head_hash = try_fetch_hash("Net::HTTP") do
github_api_get(
"/repos/ruby/ruby/commits/master",
accept: "application/vnd.github.v3.sha",
token: ENV["GITHUB_TOKEN"],
)
end
# Second attempt: Using curl
if head_hash.nil?
sleep 30
head_hash = try_fetch_hash("curl") do
result = `curl -fs -H 'accept: application/vnd.github.v3.sha' https://api.github.com/repos/ruby/ruby/commits/master`.chomp
$?.success? && !result.empty? ? result : nil
end
end
# Third attempt: Using GitHub CLI if available
if head_hash.nil? && system("which gh > /dev/null 2>&1")
sleep 30
head_hash = try_fetch_hash("GitHub CLI") do
result = `gh api repos/ruby/ruby/commits/master --jq '.sha'`.chomp
$?.success? ? result : nil
end
end
# If all methods failed, provide a meaningful error
if head_hash.nil?
p "Failed to retrieve Ruby master head hash using Net::HTTP, curl, or GitHub CLI"
head_hash = "unknown"
end
if defined?(cache_path) && 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 get_date_at_commit(commit_hash)
require "json"
require "time"
raise ArgumentError, "Invalid commit_hash: #{commit_hash.inspect}" unless commit_hash.match?(/\A[a-z0-9]+\z/)
json = github_api_get("/repos/ruby/ruby/commits/#{commit_hash}")
date = JSON.parse(json).fetch("commit").fetch("committer").fetch("date")
Time.iso8601(date)
end
def github_api_get(path, accept: "application/vnd.github+json", token: ENV.fetch("GITHUB_TOKEN"))
require "net/http"
require "uri"
Net::HTTP.get_response(URI.join("https://api.github.com", path), {
"Accept" => accept,
"Authorization" => token&.then { "Bearer #{token}" },
}.compact).tap(&:value).then(&:body)
end
def make_tags(ruby_version, version_suffix=nil, tag_suffix=nil)
if ruby_version == "master"
commit_hash = ENV.fetch("ruby_sha", "").empty? ? get_ruby_master_head_hash : ENV.fetch("ruby_sha")
commit_date = get_date_at_commit(commit_hash).then do |date|
date_offset = Integer(ENV.fetch("ruby_commit_date_offset", "0"))
date += date_offset * 24 * 60 * 60
"%04d%02d%02d" % [date.year, date.month, date.day]
end
ruby_version = "master:#{commit_hash}"
tags = ["master#{version_suffix}", "master#{version_suffix}-#{commit_hash}", "master#{version_suffix}-#{commit_date}"]
else
ruby_version_mm = ruby_version.split('.')[0,2].join('.')
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
desc "Build Docker image for Ruby (env: ruby_version, ubuntu_version, arch, image_version_suffix, tag_suffix, tag, cppflags, optflags, target)"
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}"
elsif ruby_version.match?(/\A\d+\.\d+\.\d+-(preview|rc|dev)\d*\z/)
# For preview, rc, and dev versions, extract just the numeric version
# e.g., "4.0.0-preview2" -> "4.0.0"
ruby_so_suffix = ruby_version.match(/\A(\d+\.\d+\.\d+)/)[1]
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,
'--provenance=false',
'--sbom=false',
'.'
sh 'docker', 'images'
end
namespace :manifest do
desc "Create multi-architecture Docker manifests (env: ruby_version, architectures, manifest_suffix, image_version_suffix)"
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|
# "-#{arch}-#{manifest_suffix}" should match `tag_suffix` on `docker:build`
manifest_name = "#{tags[0]}-#{arch}"
manifest_name = "#{manifest_name}-#{manifest_suffix}" if manifest_suffix
['--amend', manifest_name]
}.flatten
latest_tag = nil
tags.each do |tag|
sh 'docker', 'manifest', 'create', "#{tag}", *amend_args
if tag =~ /#{LATEST_UBUNTU_VERSION}/
non_ubuntu_tag = tag.sub(/-#{LATEST_UBUNTU_VERSION}/, '')
sh 'docker', 'manifest', 'create', "#{non_ubuntu_tag}", *amend_args
if image_version_suffix.empty? && ruby_version =~ /^#{LATEST_RUBY_VERSION}/ && latest_tag.nil?
latest_tag = tag.sub(/#{ruby_version}-#{LATEST_UBUNTU_VERSION}/, "latest")
sh 'docker', 'manifest', 'create', "#{latest_tag}", *amend_args
end
end
end
end
desc "Push multi-architecture Docker manifests to registry (env: ruby_version, image_version_suffix)"
task :push do
ruby_version = ENV["ruby_version"]
image_version_suffix = ENV["image_version_suffix"]
_, tags = make_tags(ruby_version, image_version_suffix)
latest_tag = nil
tags.each do |tag|
sh 'docker', 'manifest', 'push', "#{tag}"
if tag =~ /#{LATEST_UBUNTU_VERSION}/
non_ubuntu_tag = tag.sub(/-#{LATEST_UBUNTU_VERSION}/, '')
sh 'docker', 'manifest', 'push', "#{non_ubuntu_tag}"
if image_version_suffix.empty? && ruby_version =~ /^#{LATEST_RUBY_VERSION}/ && latest_tag.nil?
latest_tag = tag.sub(/#{ruby_version}-#{LATEST_UBUNTU_VERSION}/, "latest")
sh 'docker', 'manifest', 'push', "#{latest_tag}"
end
end
end
end
end
end