/
Fastfile
307 lines (274 loc) · 9.58 KB
/
Fastfile
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
fastlane_require 'dotenv'
fastlane_require 'spaceship'
release_notes_command = "git log HEAD --pretty=format:\"%s\" -1"
# Check Git Status
lane :check_git_status do |options|
ensure_git_branch(
branch: "#{options[:name]}"
)
ensure_git_status_clean
git_pull
end
lane :set_build_numbers_to_current_timestamp do |options|
incremented_build_number = Time.now.to_i.to_s
`sed -i -e "s#.*IOS_VERSION_BUILD_NUMBER=.*#IOS_VERSION_BUILD_NUMBER='#{incremented_build_number}'#g" .env`
`sed -i -e "s#.*ANDROID_VERSION_CODE=.*#ANDROID_VERSION_CODE='#{incremented_build_number}'#g" .env`
ENV['IOS_VERSION_BUILD_NUMBER'] = incremented_build_number
ENV['ANDROID_VERSION_CODE'] = incremented_build_number
end
# JS Environments
lane :set_js_env do |options|
backup_file(path: 'src/environment/index.js')
sh "cp ../src/environment/index.#{ENV['ENV']}.js ../src/environment/index.js"
end
before_all do |lane, options|
Dotenv.load(".env.#{ENV['ENV']}.secret")
end
after_all do |lane, options|
if File.exist?('../src/environment/index.js.back')
restore_file(path: 'src/environment/index.js')
end
end
error do |lane, exception, options|
if File.exist?('../src/environment/index.js.back')
restore_file(path: 'src/environment/index.js')
end
end
# IOS
platform :ios do
lane :restore_files do |options|
plist_path = "#{ENV['IOS_PROJECT_PATH']}/#{ENV['IOS_PLIST_PATH']}"
if File.exist?("../#{plist_path}.back")
restore_file(path: plist_path)
end
pbxproj_path="#{ENV['IOS_PROJECT_PATH']}/#{ENV['IOS_PROJECT_NAME']}.xcodeproj/project.pbxproj"
if File.exist?("../#{pbxproj_path}.back")
restore_file(path: pbxproj_path)
end
appcenter_plist_path="#{ENV['IOS_PROJECT_PATH']}/#{ENV['IOS_PROJECT_NAME']}/AppCenter-Config.plist"
if File.exist?("../#{appcenter_plist_path}.back")
restore_file(path: appcenter_plist_path)
end
end
error do |lane, exception, options|
restore_files
end
lane :setup do |options|
produce(
app_name: ENV['IOS_APP_NAME'],
skip_itc: true
)
match(
type: 'development',
shallow_clone: true,
clone_branch_directly: true,
force_for_new_devices: true
)
match(
shallow_clone: true,
clone_branch_directly: true,
force_for_new_devices: true
)
end
lane :setup_push do
Spaceship.login
Spaceship.select_team
keyName = "Push JWT #{ENV['IOS_APP_NAME']} #{ENV['ENV']}".gsub!(/[^0-9A-Za-z ]/, '')
key = Spaceship::Portal::Key.create(name: keyName, apns: true)
puts "Key ID is"
puts key.id
File.write("#{keyName}.p8", key.download)
end
lane :build do |options|
plist_full_path = "#{ENV['IOS_PROJECT_PATH']}/#{ENV['IOS_PLIST_PATH']}"
backup_file(path: plist_full_path)
xcodeproj = "#{ENV['IOS_PROJECT_PATH']}/#{ENV['IOS_PROJECT_NAME']}.xcodeproj"
xcworkspace = "#{ENV['IOS_PROJECT_PATH']}/#{ENV['IOS_PROJECT_NAME']}.xcworkspace"
pbxproj_full_path="#{xcodeproj}/project.pbxproj"
backup_file(path: pbxproj_full_path)
update_info_plist(
xcodeproj: xcodeproj,
plist_path: ENV['IOS_PLIST_PATH'],
block: lambda { |plist|
plist['CFBundleName'] = ENV['IOS_APP_NAME']
plist['CFBundleDisplayName'] = ENV['IOS_APP_NAME']
plist['CFBundleShortVersionString'] = ENV['IOS_VERSION']
plist['CFBundleVersion'] = ENV['IOS_VERSION_BUILD_NUMBER']
},
)
if ENV['IOS_CODEPUSH_DEPLOYMENT_KEY'] then
set_info_plist_value(path: plist_full_path, key:'CodePushDeploymentKey', value: ENV['IOS_CODEPUSH_DEPLOYMENT_KEY'])
end
if ENV['IOS_APPCENTER_APP_SECRET'] then
appcenter_plist_full_path = "#{ENV['IOS_PROJECT_PATH']}/#{ENV['IOS_PROJECT_NAME']}/AppCenter-Config.plist"
backup_file(path: appcenter_plist_full_path)
set_info_plist_value(path: appcenter_plist_full_path, key:'AppSecret', value: ENV['IOS_APPCENTER_APP_SECRET'])
end
update_app_identifier(
xcodeproj: xcodeproj,
plist_path: ENV['IOS_PLIST_PATH'],
app_identifier: ENV['IOS_APP_ID']
)
profile_env_name = "sigh_#{ENV['IOS_APP_ID']}_#{ENV['MATCH_TYPE']}_profile-name"
gymOptions = ({
silent: true,
xcargs: "PROVISIONING_PROFILE_SPECIFIER='#{ENV[profile_env_name]}' DEVELOPMENT_TEAM='#{ENV['IOS_TEAM_ID']}'"
}).merge(
File.directory?("../#{xcworkspace}") ?
{workspace: xcworkspace} :
{project: xcodeproj}
)
gym(gymOptions)
restore_files
end
lane :deploy_hockey do |options|
hockey(
notify: '0',
ipa: ENV['IOS_IPA_PATH']
)
end
lane :deploy_to_appcenter do |options|
appcenter_upload(
api_token: ENV['FL_APPCENTER_API_TOKEN'],
owner_name: ENV['APPCENTER_USERNAME'],
app_name: ENV['IOS_APPCENTER_APP_ID'],
ipa: ENV['IOS_IPA_PATH'],
release_notes: %x[#{release_notes_command}]
)
end
lane :deploy_to_testflight do |options|
pilot(
username: ENV['IOS_APPSTORECONNECT_USER_ID'],
app_identifier: ENV['IOS_APP_ID'],
ipa: ENV['IOS_IPA_PATH'],
distribute_external: false,
skip_waiting_for_build_processing: true
)
end
lane :deploy do |options|
set_js_env
if options[:codepush] then
release_notes = %x[#{release_notes_command}]
sh "cd .. && appcenter codepush release-react -d #{ENV['IOS_CODEPUSH_DEPLOYMENT_NAME']} -a #{ENV['APPCENTER_USERNAME']}/#{ENV['IOS_APPCENTER_APP_ID']} --target-binary-version \"#{ENV['IOS_VERSION']}\" --description \"#{release_notes}\" --disable-duplicate-release-error"
else
setup_circle_ci
match(
shallow_clone: true,
clone_branch_directly: true,
readonly: true
)
build
if ENV['DEPLOYMENT_PLATFORM'] === 'hockeyapp' then
deploy_hockey
elsif ENV['DEPLOYMENT_PLATFORM'] === 'appcenter' then
deploy_to_appcenter
elsif ENV['DEPLOYMENT_PLATFORM'] === 'appstore'
deploy_to_testflight
end
end
end
lane :deploy_local do |options|
check_git_status(name: ENV['REPO_GIT_BRANCH'])
deploy
end
lane :add_devices do |options|
if ['development','adhoc'].include?(ENV['MATCH_TYPE']) then
register_devices(
team_id: ENV['IOS_TEAM_ID'],
devices_file: "./fastlane/ios_devices.txt"
)
match(
shallow_clone: true,
clone_branch_directly: true,
force_for_new_devices: true
)
else
puts 'Environment not associated to an ad-hoc or development app'
end
end
end
# ANDROID
platform :android do
lane :restore_files do |options|
appcenter_config_path = "#{ENV['ANDROID_PROJECT_DIR']}/app/src/main/assets/appcenter-config.json"
if File.exist?("../#{appcenter_config_path}.back")
restore_file(path: appcenter_config_path)
end
android_strings_path = "#{ENV['ANDROID_PROJECT_DIR']}/app/src/main/res/values/strings.xml"
if File.exist?("../#{android_strings_path}.back")
restore_file(path: android_strings_path)
end
end
error do |lane, exception, options|
restore_files
end
lane :set_keys do |options|
if ENV['ANDROID_APPCENTER_APP_SECRET'] then
appcenter_config_path = "#{ENV['ANDROID_PROJECT_DIR']}/app/src/main/assets/appcenter-config.json"
backup_file(path: appcenter_config_path)
appcenter_config = load_json(json_path: appcenter_config_path)
appcenter_config['app_secret'] = ENV['ANDROID_APPCENTER_APP_SECRET']
File.open("../#{appcenter_config_path}","w") do |f|
f.write(appcenter_config.to_json)
end
end
if ENV['ANDROID_CODEPUSH_DEPLOYMENT_KEY'] then
android_strings_path = "#{ENV['ANDROID_PROJECT_DIR']}/app/src/main/res/values/strings.xml"
# backup_file(path: android_strings_path) We can't backup this way because android can't build if there is a non-xml file in values folder. Solution to be found.
xml = Nokogiri::XML(File.open("../#{android_strings_path}"))
xml.at("//string[@name=\"reactNativeCodePush_androidDeploymentKey\"]").content = ENV['ANDROID_CODEPUSH_DEPLOYMENT_KEY']
File.write("../#{android_strings_path}", xml.to_xml)
end
end
lane :build do |options|
set_keys
gradle(
task: "assembleRelease",
project_dir: ENV['ANDROID_PROJECT_DIR']
)
restore_files
end
lane :deploy_hockey do |options|
hockey(
notify: '0',
apk: ENV['ANDROID_APK_PATH']
)
end
lane :deploy_to_appcenter do |options|
appcenter_upload(
api_token: ENV['FL_APPCENTER_API_TOKEN'],
owner_name: ENV['APPCENTER_USERNAME'],
app_name: ENV['ANDROID_APPCENTER_APP_ID'],
apk: ENV['ANDROID_APK_PATH'],
release_notes: %x[#{release_notes_command}]
)
end
lane :deploy_to_playstore do |options|
supply(
package_name: ENV['GRADLE_APP_IDENTIFIER'],
track: 'internal',
apk: ENV['ANDROID_APK_PATH'],
json_key: ENV['ANDROID_PLAYSTORE_JSON_KEY_PATH'],
)
end
lane :deploy do |options|
set_js_env
if options[:codepush] then
release_notes = %x[#{release_notes_command}]
sh "cd .. && appcenter codepush release-react -d #{ENV['ANDROID_CODEPUSH_DEPLOYMENT_NAME']} -a #{ENV['APPCENTER_USERNAME']}/#{ENV['ANDROID_APPCENTER_APP_ID']} --target-binary-version \"#{ENV['ANDROID_VERSION_NAME']}\" --description \"#{release_notes}\" --disable-duplicate-release-error"
else
build
if ENV['DEPLOYMENT_PLATFORM'] === 'hockeyapp' then
deploy_hockey
elsif ENV['DEPLOYMENT_PLATFORM'] === 'appcenter' then
deploy_to_appcenter
elsif ENV['DEPLOYMENT_PLATFORM'] === 'appstore' then
deploy_to_playstore
end
end
end
lane :deploy_local do |options|
check_git_status(name: ENV['REPO_GIT_BRANCH'])
deploy
end
end