-
Notifications
You must be signed in to change notification settings - Fork 14
/
Rakefile
115 lines (98 loc) · 3.2 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
require "tmpdir"
CONTENT_TYPE_FOR_EXT = {
".sig" => "application/pgp-signature",
".db" => "application/zstd",
".files" => "application/zstd",
".zst" => "application/zstd",
".txt" => "text/plain",
}
def repo_release
require "octokit"
tag = ENV['DEPLOY_TAG']
repo = ENV['DEPLOY_REPO_NAME']
token = ENV['DEPLOY_TOKEN']
client = Octokit::Client.new(access_token: token)
rel = client.releases(repo).find{|r| r.tag_name==tag }
[client, rel]
end
def upload_to_github(files:)
$stderr.puts "Add #{files.size} files to github release"
client, release = repo_release
old_assets = release.assets
files.each do |fname|
if old_asset=old_assets.find{|a| a.name == File.basename(fname) }
$stderr.puts "Delete old #{old_asset.name}"
client.delete_release_asset(old_asset.url)
end
$stderr.print "Uploading #{fname} (#{File.size(fname)} bytes) ... "
client.upload_asset(release.url, fname, content_type: CONTENT_TYPE_FOR_EXT[File.extname(fname)])
$stderr.puts "OK"
end
end
namespace "upload" do
desc "aquire lock for upload"
task "lock" do
Dir.mktmpdir do |tmpdir|
my_lockid = ENV['DEPLOY_LOCK']
fname = File.join(tmpdir, "lock.txt")
File.write(fname, my_lockid)
loop do
client, release = repo_release
if asset=release.assets.find{|a| a.name == File.basename(fname) }
# already locked
their_lockid = asset.label
break if their_lockid == my_lockid
$stderr.print "Wait for lock"
sleep rand(10)
else
# try to lock
begin
$stderr.print "Uploading #{fname} (#{File.size(fname)} bytes) ... "
client.upload_asset(release.url, fname,
content_type: CONTENT_TYPE_FOR_EXT[File.extname(fname)],
label: my_lockid)
rescue Faraday::ConnectionFailed, Octokit::UnprocessableEntity
rescue Octokit::ClientError
# Wait longer due to abuse detection
sleep 75
end
end
end
end
end
desc "release lock for upload"
task "unlock" do
client, release = repo_release
assets = release.assets
my_lockid = ENV['DEPLOY_LOCK']
fname = "lock.txt"
if asset=assets.find{|a| a.name == File.basename(fname) }
their_lockid = asset.label
if their_lockid != my_lockid
raise "unlocking a foreign lock"
end
$stderr.puts "Delete lock file #{asset.name}"
client.delete_release_asset(asset.url)
end
end
desc "remove outdated github releases"
task "remove_old" do
$stderr.puts "Remove outdated package files from github release"
client, release = repo_release
old_assets = release.assets
old_assets.map do |a|
a.name =~ /ruby-head-r(\d{8})-/ && [a, $1]
end.compact.sort_by(&:last)[0..-19].each do |a, _date|
$stderr.puts "Delete old #{a.name}"
client.delete_release_asset(a.url)
end
end
end
desc "upload files to github release"
task "upload" do
files = ARGV[ARGV.index("--")+1 .. -1].reject do |f|
# Remove unexpanded wildcards and add dummy task to satisfy rake
!File.exist?(f) && f =~ /[\*\?]/ && task(f)
end
upload_to_github(files: files)
end