forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
154 lines (137 loc) · 4.68 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
require 'digest'
require 'aws-sdk'
require 'json'
BUCKET_NAME = 'particle-cli-ng-alpha'
TARGETS = [
{os: 'windows', arch: '386'},
{os: 'windows', arch: 'amd64'},
{os: 'darwin', arch: 'amd64'},
{os: 'linux', arch: 'arm', goarm: '6'},
{os: 'linux', arch: 'amd64'},
{os: 'linux', arch: '386', go386: '387'},
{os: 'openbsd', arch: 'amd64'},
{os: 'openbsd', arch: '386'},
{os: 'freebsd', arch: 'amd64'},
{os: 'freebsd', arch: '386'},
]
VERSION = `./version`.chomp
dirty = `git status 2> /dev/null | tail -n1`.chomp != 'nothing to commit, working directory clean'
CHANNEL = dirty ? 'dirty' : `git rev-parse --abbrev-ref HEAD`.chomp
ASSETS_HOST = 'dfu55fst9l042.cloudfront.net'
LABEL = "particle-cli-ng/#{VERSION} (#{CHANNEL})"
REVISION=`git log -n 1 --pretty=format:"%H"`
desc "build particle-cli-ng"
task :build do
puts "Building #{LABEL}..."
FileUtils.mkdir_p 'dist'
TARGETS.each do |target|
puts " * #{target[:os]}-#{target[:arch]}"
build(target)
end
end
desc "release particle-cli-ng"
task :release => :build do
abort 'branch is dirty' if CHANNEL == 'dirty'
abort "#{CHANNEL} not a channel branch (dev/beta/master)" unless %w(dev beta master).include?(CHANNEL)
puts "Releasing #{LABEL}..."
cache_control = "public,max-age=31536000"
TARGETS.each do |target|
puts " * #{target[:os]}-#{target[:arch]}"
from = "./dist/#{target[:os]}/#{target[:arch]}/particle-cli-ng"
to = remote_path(target[:os], target[:arch])
upload_file(from, to, content_type: 'binary/octet-stream', cache_control: cache_control)
upload_file(from + '.gz', to + '.gz', content_type: 'binary/octet-stream', cache_control: cache_control)
upload(from, to + ".sha1", content_type: 'text/plain', cache_control: cache_control)
end
upload_manifest()
notify_rollbar
puts "Released #{VERSION}"
end
def build(target)
path = "./dist/#{target[:os]}/#{target[:arch]}/particle-cli-ng"
path += ".exe" if target[:os] === 'windows'
ldflags = "-X=main.Version=#{VERSION} -X=main.Channel=#{CHANNEL}"
args = ["-o", "#{path}", "-ldflags", "\"#{ldflags}\""]
unless target[:os] === 'windows'
args += ["-a", "-tags", "netgo"]
end
vars = ["GOOS=#{target[:os]}", "GOARCH=#{target[:arch]}"]
vars << "GO386=#{target[:go386]}" if target[:go386]
vars << "GOARM=#{target[:goarm]}" if target[:goarm]
ok = system("#{vars.join(' ')} go build #{args.join(' ')}")
exit 1 unless ok
#if target[:os] === 'windows'
# # sign executable
# ok = system "osslsigncode -pkcs12 resources/exe/particle-codesign-cert.pfx \
# -pass '#{ENV['HEROKU_WINDOWS_SIGNING_PASS']}' \
# -n 'Particle CLI' \
# -i https://www.particle.io/ \
# -in #{path} \
# -out #{path} > /dev/null"
# unless ok
# $stderr.puts "Unable to sign Windows binaries, please follow the full release instructions"
# $stderr.puts "https://github.com/monkbroc/particle-cli-ng/blob/master/RELEASE-FULL.md#windows-release"
# exit 2
# end
#end
gzip(path)
end
def gzip(path)
system("gzip --keep -f #{path}")
end
def sha_digest(path)
Digest::SHA1.file(path).hexdigest
end
def remote_path(os, arch)
ext = ".exe" if os === 'windows'
"#{CHANNEL}/#{VERSION}/#{os}/#{arch}/particle-cli-ng#{ext}"
end
def remote_url(os, arch)
"https://#{ASSETS_HOST}/#{remote_path(os, arch)}"
end
def manifest
return @manifest if @manifest
@manifest = {
released_at: Time.now,
version: VERSION,
channel: CHANNEL,
builds: {}
}
TARGETS.each do |target|
@manifest[:builds][target[:os]] ||= {}
@manifest[:builds][target[:os]][target[:arch]] = {
url: remote_url(target[:os], target[:arch]),
sha1: sha_digest("dist/#{target[:os]}/#{target[:arch]}/particle-cli-ng")
}
end
@manifest
end
def s3_client
@s3_client ||= Aws::S3::Client.new(region: 'us-east-1', access_key_id: ENV['PARTICLE_CLI_RELEASE_ACCESS'], secret_access_key: ENV['PARTICLE_CLI_RELEASE_SECRET'])
end
def upload_file(local, remote, opts={})
upload(File.new(local), remote, opts)
end
def upload(body, remote, opts={})
s3_client.put_object({
key: remote,
body: body,
acl: 'public-read',
bucket: BUCKET_NAME
}.merge(opts))
end
def upload_manifest
puts 'uploading manifest...'
upload(JSON.dump(manifest), "#{CHANNEL}/manifest.json", content_type: 'application/json', cache_control: "public,max-age=60")
end
def notify_rollbar
unless ENV['ROLLBAR_TOKEN']
$stderr.puts 'ROLLBAR_TOKEN not set, skipping rollbar deploy notification'
return
end
Net::HTTP.post_form(URI.parse('https://api.rollbar.com/api/1/deploy/'),
environment: CHANNEL,
local_username: `whoami`.chomp,
revision: REVISION,
access_token: ENV['ROLLBAR_TOKEN'])
end