forked from ruby/docker-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
75 lines (69 loc) · 2.52 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
namespace :docker do
def docker_hub_org
"rubylang"
end
def docker_image_name
"#{docker_hub_org}/ruby"
end
def default_ubuntu_version(ruby_version)
if ruby_version < "3.0"
"bionic"
else
"focal"
end
end
def ubuntu_version(ruby_version)
ENV.fetch("ubuntu_version", default_ubuntu_version(ruby_version))
end
def get_ruby_master_head_hash
`curl -H 'accept: application/vnd.github.v3.sha' https://api.github.com/repos/ruby/ruby/commits/master`.chomp
end
def make_tag_args(ruby_version, suffix, arch=nil)
ruby_ver2 = 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#{suffix}", "master#{suffix}-#{commit_hash}"]
else
tags = ["#{ruby_ver2}#{suffix}", "#{ruby_version}#{suffix}"]
end
arch = "-#{arch}" if arch
tag_args = tags.map {|t| ["-t", "#{docker_image_name}:#{t}-#{ubuntu_version(ruby_version)}#{arch}"] }.flatten
return ruby_version, tag_args
end
task :build do
ruby_version = ENV['ruby_version'] || '2.6.1'
suffix = ENV["image_name_suffix"]
arch = ENV["arch"]
# NOTE: the architecture name is based on Debian ports
# https://www.debian.org/ports/index.en.html
case arch
when 'arm64'
dockerfile = 'Dockerfile-arm64'
when 'amd64', nil
arch = nil
dockerfile = 'Dockerfile'
else
abort "unknown architecture name: '#{arch}'"
end
ruby_version, tag_args = make_tag_args(ruby_version, suffix, arch)
unless File.directory?("tmp/ruby")
FileUtils.mkdir_p("tmp/ruby")
IO.write('tmp/ruby/.keep', '')
end
env_args = %w(cppflags optflags).map {|name| ["--build-arg", "#{name}=#{ENV[name]}"] }.flatten
sh 'docker', 'run', '--rm', '--privileged', 'multiarch/qemu-user-static:register', '--reset' if arch
sh 'docker', 'build', '-f', dockerfile, *tag_args, *env_args,
'--build-arg', "RUBY_VERSION=#{ruby_version}",
'--build-arg', "BASE_IMAGE_TAG=#{ubuntu_version(ruby_version)}",
'.'
if ruby_version.start_with? 'master'
image_name = tag_args[3]
if ENV['nightly']
today = Time.now.getlocal("+09:00").strftime('%Y%m%d')
sh 'docker', 'tag', image_name, image_name.sub(/master#{suffix}-([\da-f]+)/, "master#{suffix}-nightly-#{today}")
sh 'docker', 'tag', image_name, image_name.sub(/master#{suffix}-([\da-f]+)/, "master#{suffix}-nightly")
end
end
end
end