-
Notifications
You must be signed in to change notification settings - Fork 39
/
step.rb
172 lines (155 loc) · 6.07 KB
/
step.rb
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
require 'optparse'
require 'tempfile'
require_relative 'zip_file_generator'
require_relative 'uploaders/file_uploader'
require_relative 'uploaders/ipa_uploader'
require_relative 'uploaders/apk_uploader'
# -----------------------
# --- functions
# -----------------------
def fail_with_message(message)
puts "\e[31m#{message}\e[0m"
exit(1)
end
# ----------------------------
# --- Options
options = {
build_url: nil,
api_token: nil,
is_compress: false,
deploy_path: nil,
notify_user_groups: nil,
notify_email_list: nil,
is_enable_public_page: true
}
parser = OptionParser.new do|opts|
opts.banner = 'Usage: step.rb [options]'
opts.on('-u', '--buildurl URL', 'Build URL') { |u| options[:build_url] = u unless u.to_s == '' }
opts.on('-t', '--apitoken TOKEN', 'API Token') { |t| options[:api_token] = t unless t.to_s == '' }
opts.on('-c', '--compress BOOL', 'Is Compress') { |c| options[:is_compress] = true if c.to_s == 'true' }
opts.on('-d', '--deploypath PATH', 'Deploy Path') { |d| options[:deploy_path] = d unless d.to_s == '' }
opts.on('-g', '--usergroups ARRAY', 'Notify User Groups') { |g| options[:notify_user_groups] = g unless g.to_s == '' }
opts.on('-e', '--emaillist ARRAY', 'Notify Email List') { |e| options[:notify_email_list] = e unless e.to_s == '' }
opts.on('-p', '--publicpage BOOL', 'Enable Public Page') { |p| options[:is_enable_public_page] = false if p.to_s == 'false' }
opts.on('-h', '--help', 'Displays Help') do
exit
end
end
parser.parse!
fail_with_message('No build_url provided') unless options[:build_url]
fail_with_message('No api_token provided') unless options[:api_token]
fail_with_message('No deploy_path provided') unless options[:deploy_path]
if !Dir.exist?(options[:deploy_path]) && !File.exist?(options[:deploy_path])
fail_with_message('Deploy source path does not exist at the provided path')
end
puts
puts '========== Configs =========='
puts " * build_url: #{options[:build_url]}"
puts " * api_token: #{options[:api_token]}"
puts " * is_compress: #{options[:is_compress]}"
puts " * deploy_path: #{options[:deploy_path]}"
puts " * notify_user_groups: #{options[:notify_user_groups]}"
puts " * notify_email_list: #{options[:notify_email_list]}"
puts " * is_enable_public_page: #{options[:is_enable_public_page]}"
# ----------------------------
# --- Main
begin
public_page_url = ''
if File.directory?(options[:deploy_path])
if options[:is_compress]
puts
puts '## Compressing the Deploy directory'
tempfile = Tempfile.new(::File.basename(options[:deploy_path]))
begin
zip_archive_path = tempfile.path + '.zip'
puts " (i) zip_archive_path: #{zip_archive_path}"
zip_gen = ZipFileGenerator.new(options[:deploy_path], zip_archive_path)
zip_gen.write
tempfile.close
fail 'Failed to create compressed ZIP file' unless File.exist?(zip_archive_path)
public_page_url = deploy_file_to_bitrise(zip_archive_path,
options[:build_url],
options[:api_token]
)
rescue => ex
raise ex
ensure
tempfile.close
tempfile.unlink
end
else
puts
puts '## Uploading the content of the Deploy directory separately'
entries = Dir.entries(options[:deploy_path])
entries.delete('.')
entries.delete('..')
entries.each do |filepth|
disk_file_path = File.join(options[:deploy_path], filepth)
next if File.directory?(disk_file_path)
a_public_page_url = ''
if disk_file_path.match('.*.ipa')
a_public_page_url = deploy_ipa_to_bitrise(
disk_file_path,
options[:build_url],
options[:api_token],
options[:notify_user_groups],
options[:notify_email_list],
options[:is_enable_public_page]
)
elsif disk_file_path.match('.*.apk')
a_public_page_url = deploy_apk_to_bitrise(disk_file_path,
options[:build_url],
options[:api_token],
options[:notify_user_groups],
options[:notify_email_list],
options[:is_enable_public_page]
)
else
a_public_page_url = deploy_file_to_bitrise(disk_file_path,
options[:build_url],
options[:api_token]
)
end
public_page_url = a_public_page_url if public_page_url == '' && !a_public_page_url.nil? && a_public_page_url != ''
end
end
else
puts
puts '## Deploying single file'
a_public_page_url = ''
if options[:deploy_path].match('.*.ipa')
a_public_page_url = deploy_ipa_to_bitrise(
options[:deploy_path],
options[:build_url],
options[:api_token],
options[:notify_user_groups],
options[:notify_email_list],
options[:is_enable_public_page]
)
elsif options[:deploy_path].match('.*.apk')
a_public_page_url = deploy_apk_to_bitrise(
options[:deploy_path],
options[:build_url],
options[:api_token],
options[:notify_user_groups],
options[:notify_email_list],
options[:is_enable_public_page]
)
else
a_public_page_url = deploy_file_to_bitrise(
options[:deploy_path],
options[:build_url],
options[:api_token]
)
end
public_page_url = a_public_page_url
end
# - Success
fail 'Failed to export BITRISE_PUBLIC_INSTALL_PAGE_URL' unless system("envman add --key BITRISE_PUBLIC_INSTALL_PAGE_URL --value '#{public_page_url}'")
puts
puts '## Success'
puts "(i) You can find the Artifact on Bitrise, on the [Build's page](#{options[:build_url]})"
rescue => ex
fail_with_message(ex)
end
exit 0