-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
build.rb
400 lines (348 loc) · 14.5 KB
/
build.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# frozen_string_literal: true
SENTRY_ORG_SLUG = 'a8c'
SENTRY_PROJECT_SLUG_WORDPRESS = 'wordpress-ios'
SENTRY_PROJECT_SLUG_JETPACK = 'jetpack-ios'
APPCENTER_OWNER_NAME = 'automattic'
APPCENTER_OWNER_TYPE = 'organization'
# Lanes related to Building and Testing the code
#
platform :ios do
# Builds the WordPress app for Testing
#
# @option [String] device the name of the Simulator device to run the tests on
# @option [String] ios_version the Deployment Target version to use while testing
#
# @called_by CI
#
desc 'Build WordPress for Testing'
lane :build_for_testing do |options|
run_tests(
workspace: WORKSPACE_PATH,
scheme: 'WordPress',
derived_data_path: DERIVED_DATA_PATH,
build_for_testing: true,
device: options[:device],
deployment_target_version: options[:ios_version]
)
end
# Builds the Jetpack app for Testing
#
# @option [String] device the name of the Simulator device to run the tests on
# @option [String] ios_version the Deployment Target version to use while testing
#
# @called_by CI
#
desc 'Build Jetpack for Testing'
lane :build_jetpack_for_testing do |options|
run_tests(
workspace: WORKSPACE_PATH,
scheme: 'Jetpack',
derived_data_path: DERIVED_DATA_PATH,
build_for_testing: true,
device: options[:device],
deployment_target_version: options[:ios_version]
)
end
# Runs tests without building the app.
#
# Requires a prebuilt xctestrun file and simulator destination where the tests will be run.
#
# @option [String] name The (partial) name of the `*.xctestrun` file to run
# @option [String] device Name of the simulator device to run the test on
# @option [String] ios_version The deployment target version to test on
#
# @called_by CI
#
desc 'Run tests without building'
lane :test_without_building do |options|
# Find the referenced .xctestrun file based on its name
build_products_path = File.join(DERIVED_DATA_PATH, 'Build', 'Products')
xctestrun_path = Dir.glob(File.join(build_products_path, '*.xctestrun')).select do |path|
path.include?(options[:name])
end.first
UI.user_error!("Unable to find .xctestrun file at #{build_products_path}") if xctestrun_path.nil? || !File.exist?((xctestrun_path))
run_tests(
workspace: WORKSPACE_PATH,
scheme: 'WordPress',
device: options[:device],
deployment_target_version: options[:ios_version],
ensure_devices_found: true,
test_without_building: true,
xctestrun: xctestrun_path,
output_directory: File.join(PROJECT_ROOT_FOLDER, 'build', 'results'),
reset_simulator: true,
result_bundle: true
)
end
# Builds the WordPress app and uploads it to TestFlight, for beta-testing or final release
#
# @option [Boolean] skip_confirm (default: false) If true, avoids any interactive prompt
# @option [Boolean] skip_prechecks (default: false) If true, don't run the ios_build_prechecks and ios_build_preflight
# @option [Boolean] create_release If true, creates a GitHub Release draft after the upload, with zipped xcarchive as artefact
# @option [Boolean] beta_release If true, the GitHub release will be marked as being a pre-release
#
# @called_by CI
#
desc 'Builds and uploads for distribution to App Store Connect'
lane :build_and_upload_app_store_connect do |options|
ios_build_prechecks(skip_confirm: options[:skip_confirm], external: true) unless options[:skip_prechecks]
ios_build_preflight unless options[:skip_prechecks]
sentry_check_cli_installed
appstore_code_signing
gym(
scheme: 'WordPress',
workspace: WORKSPACE_PATH,
clean: true,
output_directory: BUILD_PRODUCTS_PATH,
derived_data_path: DERIVED_DATA_PATH,
export_team_id: get_required_env('EXT_EXPORT_TEAM_ID'),
export_options: { method: 'app-store' }
)
testflight(
skip_waiting_for_build_processing: true,
team_id: get_required_env('FASTLANE_ITC_TEAM_ID'),
api_key_path: APP_STORE_CONNECT_KEY_PATH
)
sentry_upload_dsym(
auth_token: get_required_env('SENTRY_AUTH_TOKEN'),
org_slug: SENTRY_ORG_SLUG,
project_slug: SENTRY_PROJECT_SLUG_WORDPRESS,
dsym_path: lane_context[SharedValues::DSYM_OUTPUT_PATH]
)
next unless options[:create_release]
archive_zip_path = File.join(PROJECT_ROOT_FOLDER, 'WordPress.xarchive.zip')
zip(path: lane_context[SharedValues::XCODEBUILD_ARCHIVE], output_path: archive_zip_path)
version = options[:beta_release] ? ios_get_build_version : ios_get_app_version
create_release(
repository: GHHELPER_REPO,
version: version,
release_notes_file_path: File.join(PROJECT_ROOT_FOLDER, 'WordPress', 'Resources', 'release_notes.txt'),
release_assets: archive_zip_path.to_s,
prerelease: options[:beta_release]
)
FileUtils.rm_rf(archive_zip_path)
end
# Builds the Jetpack app and uploads it to TestFlight, for beta-testing or final release
#
# @called_by CI
#
desc 'Builds and uploads Jetpack to TestFlight for distribution'
lane :build_and_upload_jetpack_for_app_store do
sentry_check_cli_installed
jetpack_appstore_code_signing
gym(
scheme: 'Jetpack',
workspace: WORKSPACE_PATH,
clean: true,
export_team_id: get_required_env('EXT_EXPORT_TEAM_ID'),
output_directory: BUILD_PRODUCTS_PATH,
derived_data_path: DERIVED_DATA_PATH,
export_options: { method: 'app-store' }
)
testflight(
skip_waiting_for_build_processing: true,
team_id: '299112',
api_key_path: APP_STORE_CONNECT_KEY_PATH
)
sentry_upload_dsym(
auth_token: get_required_env('SENTRY_AUTH_TOKEN'),
org_slug: SENTRY_ORG_SLUG,
project_slug: SENTRY_PROJECT_SLUG_JETPACK,
dsym_path: lane_context[SharedValues::DSYM_OUTPUT_PATH]
)
end
# Builds the "WordPress Internal" app and uploads it to App Center
#
# @option [Boolean] skip_confirm (default: false) If true, avoids any interactive prompt
# @option [Boolean] skip_prechecks (default: false) If true, don't run the ios_build_prechecks and ios_build_preflight
#
# @called_by CI
#
desc 'Builds and uploads for distribution to App Center'
lane :build_and_upload_app_center do |options|
ios_build_prechecks(skip_confirm: options[:skip_confirm], internal: true) unless options[:skip_prechecks]
ios_build_preflight unless options[:skip_prechecks]
sentry_check_cli_installed
internal_code_signing
gym(
scheme: 'WordPress Internal',
workspace: WORKSPACE_PATH,
clean: true,
output_directory: BUILD_PRODUCTS_PATH,
output_name: 'WordPress Internal',
derived_data_path: DERIVED_DATA_PATH,
export_team_id: get_required_env('INT_EXPORT_TEAM_ID'),
export_method: 'enterprise',
export_options: { method: 'enterprise' }
)
appcenter_upload(
api_token: get_required_env('APPCENTER_API_TOKEN'),
owner_name: APPCENTER_OWNER_NAME,
owner_type: APPCENTER_OWNER_TYPE,
app_name: 'WP-Internal',
file: lane_context[SharedValues::IPA_OUTPUT_PATH],
dsym: lane_context[SharedValues::DSYM_OUTPUT_PATH],
notify_testers: false
)
sentry_upload_dsym(
auth_token: get_required_env('SENTRY_AUTH_TOKEN'),
org_slug: SENTRY_ORG_SLUG,
project_slug: SENTRY_PROJECT_SLUG_WORDPRESS,
dsym_path: lane_context[SharedValues::DSYM_OUTPUT_PATH]
)
end
# Builds the WordPress app for an Installable Build ("WordPress Alpha" scheme), and uploads it to App Center
#
# @called_by CI
#
desc 'Builds and uploads an installable build'
lane :build_and_upload_installable_build do
sentry_check_cli_installed
alpha_code_signing
# Get the current build version, and update it if needed
version_config_path = File.join(PROJECT_ROOT_FOLDER, 'config', 'Version.internal.xcconfig')
versions = Xcodeproj::Config.new(File.new(version_config_path)).to_hash
build_number = generate_installable_build_number
UI.message("Updating build version to #{build_number}")
versions['VERSION_LONG'] = build_number
new_config = Xcodeproj::Config.new(versions)
new_config.save_as(Pathname.new(version_config_path))
gym(
scheme: 'WordPress Alpha',
workspace: WORKSPACE_PATH,
clean: true,
output_directory: BUILD_PRODUCTS_PATH,
output_name: 'WordPress Alpha',
derived_data_path: DERIVED_DATA_PATH,
export_team_id: ENV.fetch('INT_EXPORT_TEAM_ID', nil),
export_method: 'enterprise',
export_options: { method: 'enterprise' }
)
appcenter_upload(
api_token: get_required_env('APPCENTER_API_TOKEN'),
owner_name: APPCENTER_OWNER_NAME,
owner_type: APPCENTER_OWNER_TYPE,
app_name: 'WPiOS-One-Offs',
file: lane_context[SharedValues::IPA_OUTPUT_PATH],
dsym: lane_context[SharedValues::DSYM_OUTPUT_PATH],
destinations: 'All-users-of-WPiOS-One-Offs',
notify_testers: false
)
sentry_upload_dsym(
auth_token: get_required_env('SENTRY_AUTH_TOKEN'),
org_slug: SENTRY_ORG_SLUG,
project_slug: SENTRY_PROJECT_SLUG_WORDPRESS,
dsym_path: lane_context[SharedValues::DSYM_OUTPUT_PATH]
)
post_installable_build_pr_comment(app_name: 'WordPress', build_number: build_number, url_slug: 'WPiOS-One-Offs')
end
# Builds the Jetpack app for an Installable Build ("Jetpack" scheme), and uploads it to App Center
#
# @called_by CI
#
desc 'Builds and uploads a Jetpack installable build'
lane :build_and_upload_jetpack_installable_build do
sentry_check_cli_installed
jetpack_alpha_code_signing
# Get the current build version, and update it if needed
version_config_path = File.join(PROJECT_ROOT_FOLDER, 'config', 'Version.internal.xcconfig')
versions = Xcodeproj::Config.new(File.new(version_config_path)).to_hash
build_number = generate_installable_build_number
UI.message("Updating build version to #{build_number}")
versions['VERSION_LONG'] = build_number
new_config = Xcodeproj::Config.new(versions)
new_config.save_as(Pathname.new(version_config_path))
gym(
scheme: 'Jetpack',
workspace: WORKSPACE_PATH,
configuration: 'Release-Alpha',
clean: true,
output_directory: BUILD_PRODUCTS_PATH,
output_name: 'Jetpack Alpha',
derived_data_path: DERIVED_DATA_PATH,
export_team_id: ENV.fetch('INT_EXPORT_TEAM_ID', nil),
export_method: 'enterprise',
export_options: { method: 'enterprise' }
)
appcenter_upload(
api_token: get_required_env('APPCENTER_API_TOKEN'),
owner_name: APPCENTER_OWNER_NAME,
owner_type: APPCENTER_OWNER_TYPE,
app_name: 'jetpack-installable-builds',
file: lane_context[SharedValues::IPA_OUTPUT_PATH],
dsym: lane_context[SharedValues::DSYM_OUTPUT_PATH],
destinations: 'Collaborators',
notify_testers: false
)
sentry_upload_dsym(
auth_token: get_required_env('SENTRY_AUTH_TOKEN'),
org_slug: SENTRY_ORG_SLUG,
project_slug: SENTRY_PROJECT_SLUG_JETPACK,
dsym_path: lane_context[SharedValues::DSYM_OUTPUT_PATH]
)
post_installable_build_pr_comment(app_name: 'Jetpack', build_number: build_number, url_slug: 'jetpack-installable-builds')
end
#################################################
# Helper Functions
#################################################
# Generates a build number for Installable Builds, based on the PR number and short commit SHA1
#
# @note This function uses Buildkite-specific ENV vars
#
def generate_installable_build_number
if ENV['BUILDKITE']
commit = ENV.fetch('BUILDKITE_COMMIT', nil)[0, 7]
branch = ENV.fetch('BUILDKITE_BRANCH', nil)
pr_num = ENV.fetch('BUILDKITE_PULL_REQUEST', nil)
pr_num == 'false' ? "#{branch}-#{commit}" : "pr#{pr_num}-#{commit}"
else
repo = Git.open(PROJECT_ROOT_FOLDER)
commit = repo.current_branch
branch = repo.revparse('HEAD')[0, 7]
"#{branch}-#{commit}"
end
end
# Posts a comment on the current PR to inform where to download a given Installable Build that was just published to App Center.
#
# Use this only after `upload_to_app_center` as been called, as it announces how said App Center build can be installed.
#
# @param [String] app_name The display name to use in the comment text to identify which app this Installable Build is about
# @param [String] build_number The App Center's build number for this build
# @param [String] url_slug The last component of the `install.appcenter.ms` URL used to install the app. Typically a sluggified version of the app name in App Center (e.g. `WPiOS-One-Offs`)
#
# @called_by CI — especially, relies on `BUILDKITE_PULL_REQUEST` being defined
#
def post_installable_build_pr_comment(app_name:, build_number:, url_slug:)
UI.message("Successfully built and uploaded installable build `#{build_number}` to App Center.")
return if ENV['BUILDKITE_PULL_REQUEST'].nil?
install_url = "https://install.appcenter.ms/orgs/automattic/apps/#{url_slug}/"
qr_code_url = "https://chart.googleapis.com/chart?chs=500x500&cht=qr&chl=#{CGI.escape(install_url)}&choe=UTF-8"
comment_body = <<~COMMENT_BODY
You can test the changes in <strong>#{app_name}</strong> from this Pull Request by:<ul>
<li><a href='#{install_url}'>Clicking here</a> or scanning the QR code below to access App Center</li>
<li>Then installing the build number <code>#{build_number}</code> on your iPhone</li>
</ul>
<img src='#{qr_code_url}' width='150' height='150' />
If you need access to App Center, please ask a maintainer to add you.
COMMENT_BODY
comment_on_pr(
project: 'wordpress-mobile/wordpress-ios',
pr_number: Integer(ENV.fetch('BUILDKITE_PULL_REQUEST', nil)),
reuse_identifier: "installable-build-link--#{url_slug}",
body: comment_body
)
end
# Returns the value of `VERSION_SHORT`` from the `Version.public.xcconfig` file
#
# FIXME: This ought to be extracted into the release toolkit, ideally in a configurable way but with smart defaults.
# See discussion in https://github.com/wordpress-mobile/WordPress-iOS/pull/16805/files/5f3009c5e0d01448cf0369656dddc1fe3757e45f#r664069046
#
def read_version_from_config
fastlane_require 'Xcodeproj'
# If the file is not available, the method will raise so we should be fine not handling that case. We'll never return an empty string.
File.open(File.join(PROJECT_ROOT_FOLDER, 'Config', 'Version.public.xcconfig')) do |config|
configuration = Xcodeproj::Config.new(config)
configuration.attributes['VERSION_SHORT']
end
end
end