-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathdeploy.ts
More file actions
724 lines (620 loc) · 22.6 KB
/
deploy.ts
File metadata and controls
724 lines (620 loc) · 22.6 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import childProcess from 'child_process'
import fs from 'fs'
import { join } from 'path'
import { sprintf } from 'sprintf-js'
import { deleteOldDirsSync } from './cleanDirectories'
const BUILD_ARCHIVE_MONTHS = 6
const LATEST_TEST_FILE = 'latestTestFile.json'
const argv = process.argv
const mylog = console.log
const _rootProjectDir = join(__dirname, '../')
const githubSshKey =
process.env.GITHUB_SSH_KEY ?? join(_rootProjectDir, 'id_github')
let _currentPath = __dirname
const baseDir = join(_currentPath, '..')
const now = new Date()
const cutoffDate = new Date()
cutoffDate.setMonth(now.getMonth() - BUILD_ARCHIVE_MONTHS)
/**
* Things we expect to be set in the config file:
*/
interface BuildConfigFile {
// Common build options:
envJson: Record<string, object>
// Android build options:
androidKeyStore: string
androidKeyStoreAlias: string
androidKeyStorePassword: string
androidTask: string
// iOS build options:
appleDeveloperTeamId: string
appleDeveloperTeamName: string
xcodeScheme: string
xcodeWorkspace: string
bundleId: string
// Upload options:
zealotUrl?: string
zealotApiToken?: string
zealotChannelKey?: string
hockeyAppId: string
hockeyAppTags: string
hockeyAppToken: string
productName: string
projectName: string
rsyncLocation?: string
testRepoUrl?: string
}
/**
* These are basically global variables:
*/
interface BuildObj extends BuildConfigFile {
// Set in makeCommonPre:
guiDir: string
guiPlatformDir: string
platformType: string // 'android' | 'ios'
maestroBuild: boolean
repoBranch: string // 'develop' | 'master' | 'test'
tmpDir: string
buildArchivesDir: string
bundleToolPath: string
productNameClean: string
// Set in makeCommonPost:
buildNum: string
bundleMapFile: string
bundlePath: string
guiHash: string
version: string
// Set in build steps:
dSymFile: string
dSymZip: string
ipaFile: string // Also APK
}
interface LatestTestFile {
platformType: string
branch: string
buildNum: string
version: string
filePath: string
gitHash: string
}
main()
function main(): void {
if (argv.length < 4) {
mylog(
'Usage: node -r sucrase/register deploy.ts [project] [platform] [branch] [test build]'
)
mylog(' project options: edge')
mylog(' platform options: ios, android')
mylog(' branch options: master, develop')
mylog(' test build options (optional): maestro')
}
const buildObj: BuildObj = {} as any
makeCommonPre(argv, buildObj)
makeProject(buildObj)
makeCommonPost(buildObj)
if (buildObj.platformType === 'ios') {
if (buildObj.maestroBuild) {
buildIosMaestro(buildObj)
} else {
buildIos(buildObj)
}
} else if (buildObj.platformType === 'android') {
buildAndroid(buildObj)
}
buildCommonPost(buildObj)
}
function makeCommonPre(argv: string[], buildObj: BuildObj): void {
buildObj.guiDir = _rootProjectDir
buildObj.maestroBuild = argv[5] === 'maestro'
buildObj.repoBranch = argv[4] // master or develop
buildObj.platformType = argv[3]
buildObj.projectName = argv[2]
buildObj.guiPlatformDir = buildObj.guiDir + buildObj.platformType
buildObj.tmpDir = `${buildObj.guiDir}temp`
buildObj.buildArchivesDir = '/Users/jenkins/buildArchives'
}
function makeProject(buildObj: BuildObj): void {
const project = buildObj.projectName
const config = JSON.parse(
fs.readFileSync(`${buildObj.guiDir}/deploy-config.json`, 'utf8')
)
Object.assign(buildObj, config[project])
Object.assign(buildObj, config[project][buildObj.platformType])
Object.assign(
buildObj,
config[project][buildObj.platformType][buildObj.repoBranch]
)
console.log(buildObj)
}
function makeCommonPost(buildObj: BuildObj): void {
const envJsonPath = buildObj.guiDir + '/env.json'
let envJson
if (fs.existsSync(envJsonPath)) {
envJson = JSON.parse(fs.readFileSync(envJsonPath, 'utf8'))
}
if (buildObj.envJson != null) {
if (envJson == null) throw new Error('env.json file is missing')
envJson = { ...envJson, ...buildObj.envJson[buildObj.repoBranch] }
}
if (buildObj.maestroBuild) {
if (envJson == null) throw new Error('env.json file is missing')
envJson = { ...envJson, ENABLE_MAESTRO_BUILD: true }
}
if (envJson != null) {
fs.chmodSync(envJsonPath, 0o600)
fs.writeFileSync(envJsonPath, JSON.stringify(envJson, null, 2))
}
const buildVersionFile = buildObj.guiDir + '/release-version.json'
const buildVersionJson = JSON.parse(fs.readFileSync(buildVersionFile, 'utf8'))
buildObj.buildNum = buildVersionJson.build
buildObj.version = buildVersionJson.version
chdir(buildObj.guiDir)
buildObj.guiHash = rmNewline(cmd('git rev-parse --short HEAD'))
if (buildObj.platformType === 'android') {
buildObj.bundlePath = `${buildObj.guiPlatformDir}/app/build/intermediates/assets/release/index.android.bundle`
buildObj.bundleMapFile = `${buildObj.guiPlatformDir}/app/build/generated/sourcemaps/react/release/index.android.bundle.map`
} else if (buildObj.platformType === 'ios') {
buildObj.bundlePath = `${buildObj.guiPlatformDir}/main.jsbundle`
buildObj.bundleMapFile = '../ios-release.bundle.map'
}
buildObj.productNameClean = buildObj.productName.replace(' ', '')
}
function buildIos(buildObj: BuildObj): void {
chdir(buildObj.guiDir)
if (
process.env.BUILD_REPO_URL != null &&
process.env.BUILD_REPO_URL !== '' &&
// process.env.GITHUB_SSH_KEY != null &&
process.env.HOME != null &&
process.env.MATCH_KEYCHAIN_PASSWORD != null &&
process.env.MATCH_PASSWORD != null
) {
call(
`security unlock-keychain -p '${process.env.KEYCHAIN_PASSWORD ?? ''}' "${
process.env.HOME ?? ''
}/Library/Keychains/login.keychain"`
)
call(
`security set-keychain-settings -l ${
process.env.HOME ?? ''
}/Library/Keychains/login.keychain`
)
mylog('Using Fastlane for provisioning profiles')
const matchFileLoc = join(buildObj.guiDir, '.fastlane', 'Matchfile')
let matchFile = fs.readFileSync(matchFileLoc, { encoding: 'utf8' })
matchFile = matchFile.replace('BUILD_REPO_URL', process.env.BUILD_REPO_URL)
fs.writeFileSync(matchFileLoc, matchFile, { encoding: 'utf8' })
// Set up symlink so Fastlane writes to old location but Xcode reads from new
const profileDirOld = join(
process.env.HOME,
'Library',
'MobileDevice',
'Provisioning Profiles'
)
const profileDirNew = join(
process.env.HOME,
'Library',
'Developer',
'Xcode',
'UserData',
'Provisioning Profiles'
)
// Clear old provisioning profiles
mylog('Clearing old provisioning profiles...')
call(`rm -rf ${escapePath(profileDirOld)}`)
// Create new directory and symlink old -> new so Xcode 16 sees the profiles
call(`mkdir -p ${escapePath(profileDirNew)}`)
mylog('Creating symlink: old location -> new Xcode 16 location')
call(`ln -sfn ${escapePath(profileDirNew)} ${escapePath(profileDirOld)}`)
// Use fastlane match directly - it installs profiles to the system location
// The symlink ensures they land in Xcode 16's new location
call(
`GIT_SSH_COMMAND="ssh -i ${githubSshKey}" fastlane match adhoc --git_branch="${buildObj.appleDeveloperTeamName}" --app_identifier="${buildObj.bundleId}" --team_id="${buildObj.appleDeveloperTeamId}" --api_key_path="fastlane.json" --force_for_new_devices`
)
call(
`GIT_SSH_COMMAND="ssh -i ${githubSshKey}" fastlane match development --git_branch="${buildObj.appleDeveloperTeamName}" --app_identifier="${buildObj.bundleId}" --team_id="${buildObj.appleDeveloperTeamId}" --api_key_path="fastlane.json" --force_for_new_devices`
)
call(
`GIT_SSH_COMMAND="ssh -i ${githubSshKey}" fastlane match appstore --git_branch="${buildObj.appleDeveloperTeamName}" --app_identifier="${buildObj.bundleId}" --team_id="${buildObj.appleDeveloperTeamId}" --api_key_path="fastlane.json"`
)
} else {
mylog('Missing or incomplete Fastlane params. Not using Fastlane')
}
const patchDir = getPatchDir(buildObj)
if (fs.existsSync(join(patchDir, 'GoogleService-Info.plist'))) {
call(`cp -a ${join(patchDir, 'GoogleService-Info.plist')} ios/edge/`)
} else if (fs.existsSync(`${buildObj.guiDir}/GoogleService-Info.plist`)) {
call(
`cp -a ${buildObj.guiDir}/GoogleService-Info.plist ${buildObj.guiPlatformDir}/edge/`
)
}
// Bug fixes for React Native 0.46
call('mkdir -p node_modules/react-native/scripts/')
call('mkdir -p node_modules/react-native/packager/')
call(
'cp -a node_modules/react-native/scripts/* node_modules/react-native/packager/'
)
call(
'cp -a node_modules/react-native/packager/* node_modules/react-native/scripts/'
)
// call('cp -a ../third-party node_modules/react-native/')
// chdir(buildObj.guiDir + '/node_modules/react-native/third-party/glog-0.3.4')
// call('../../scripts/ios-configure-glog.sh')
// chdir(buildObj.guiDir)
// call('react-native bundle --dev false --entry-file index.ios.js --bundle-output ios/main.jsbundle --platform ios')
const xcodeArchiveDir = `${
process.env.HOME ?? ''
}/Library/Developer/Xcode/Archives/`
// Delete old archive directories
deleteOldDirsSync(xcodeArchiveDir, cutoffDate)
chdir(buildObj.guiPlatformDir)
let cmdStr
cmdStr = `security unlock-keychain -p '${
process.env.KEYCHAIN_PASSWORD ?? ''
}' "${process.env.HOME ?? ''}/Library/Keychains/login.keychain"`
call(cmdStr)
call(
`security set-keychain-settings -l ${
process.env.HOME ?? ''
}/Library/Keychains/login.keychain`
)
// Use manual signing with explicit profile specifier
// This prevents Xcode from downloading its own profiles
// Note: We archive with AdHoc profile since that's what we export with
// AdHoc profiles require "Apple Distribution" certificate (not "Apple Development")
cmdStr = `xcodebuild -workspace ${buildObj.xcodeWorkspace} -scheme ${buildObj.xcodeScheme} -destination 'generic/platform=iOS' CODE_SIGN_STYLE=Manual CODE_SIGN_IDENTITY="Apple Distribution" PROVISIONING_PROFILE_SPECIFIER="match AdHoc ${buildObj.bundleId}" archive`
if (process.env.DISABLE_XCPRETTY === 'false') cmdStr = cmdStr + ' | xcpretty'
cmdStr = cmdStr + ' && exit ${PIPE' + 'STATUS[0]}'
call(cmdStr)
const buildDate = builddate()
const buildDir = `${xcodeArchiveDir}${buildDate}`
chdir(buildDir)
let archiveDir = cmd('ls -t')
const archiveDirArray = archiveDir.split('\n')
archiveDir = archiveDirArray[0]
buildObj.dSymFile = escapePath(
`${buildDir}/${archiveDir}/dSYMs/${buildObj.productName}.app.dSYM`
)
// const appFile = sprintf('%s/%s/Products/Applications/%s.app', buildDir, archiveDir, buildObj.xcodeScheme)
buildObj.dSymZip = escapePath(
`${buildObj.tmpDir}/${buildObj.productNameClean}-${buildObj.repoBranch}-${
buildObj.buildNum
}-${buildObj.guiHash.slice(0, 8)}.dSYM.zip`
)
buildObj.ipaFile = escapePath(
`${buildObj.tmpDir}/${buildObj.productNameClean}-${buildObj.repoBranch}-${
buildObj.buildNum
}-${buildObj.guiHash.slice(0, 8)}.ipa`
)
if (fs.existsSync(buildObj.ipaFile)) {
call('rm ' + buildObj.ipaFile)
}
if (fs.existsSync(buildObj.dSymZip)) {
call('rm ' + buildObj.dSymZip)
}
mylog('Creating IPA for ' + buildObj.xcodeScheme)
chdir(buildObj.guiPlatformDir)
// Update exportOptions.plist with actual values for adhoc export
let plist = fs.readFileSync(
buildObj.guiPlatformDir + '/exportOptions.plist',
{ encoding: 'utf8' }
)
plist = plist.replace('EXPORT_METHOD', 'release-testing')
plist = plist.replace('Your10CharacterTeamId', buildObj.appleDeveloperTeamId)
plist = plist.replace('YourBundleIdHere', buildObj.bundleId)
plist = plist.replace(
'YourProvisioningProfileNameHere',
`match AdHoc ${buildObj.bundleId}`
)
fs.writeFileSync(buildObj.guiPlatformDir + '/exportOptions.plist', plist)
cmdStr = `security unlock-keychain -p '${
process.env.KEYCHAIN_PASSWORD ?? ''
}' "${process.env.HOME ?? ''}/Library/Keychains/login.keychain"`
call(cmdStr)
call(
`security set-keychain-settings -l ${
process.env.HOME ?? ''
}/Library/Keychains/login.keychain`
)
cmdStr = `xcodebuild -exportArchive -archivePath "${buildDir}/${archiveDir}" -exportPath ${buildObj.tmpDir}/ -exportOptionsPlist ./exportOptions.plist`
call(cmdStr)
mylog('Zipping dSYM for ' + buildObj.xcodeScheme)
cmdStr = `/usr/bin/zip -r ${buildObj.dSymZip} ${buildObj.dSymFile}`
call(cmdStr)
mylog(`Renaming IPA file to ${buildObj.ipaFile}`)
const buildOutputIpaFile = `${buildObj.tmpDir}/${buildObj.productName}.ipa`
fs.renameSync(buildOutputIpaFile, buildObj.ipaFile)
cmdStr = `cp -a "${buildDir}/${archiveDir}/Products/Applications/${buildObj.productName}.app/main.jsbundle" ${buildObj.guiPlatformDir}/`
call(cmdStr)
// Do not update the testRepo for production iOS builds. Only simulator builds are usable for testing
buildObj.testRepoUrl = undefined
}
function buildIosMaestro(buildObj: BuildObj): void {
const {
buildNum,
guiDir,
guiHash,
guiPlatformDir,
productName,
productNameClean,
repoBranch,
tmpDir,
xcodeScheme,
xcodeWorkspace
} = buildObj
chdir(guiDir)
const patchDir = getPatchDir(buildObj)
if (fs.existsSync(join(patchDir, 'GoogleService-Info.plist'))) {
call(`cp -a ${join(patchDir, 'GoogleService-Info.plist')} ios/edge/`)
} else if (fs.existsSync(`${guiDir}/GoogleService-Info.plist`)) {
call(`cp -a ${guiDir}/GoogleService-Info.plist ${guiPlatformDir}/edge/`)
}
const buildDir = `${tmpDir}/derivedData`
chdir(guiPlatformDir)
let cmdStr
cmdStr = `xcodebuild -workspace ${xcodeWorkspace} -scheme ${xcodeScheme} -sdk iphonesimulator -configuration Release -derivedDataPath ${buildDir}`
if (process.env.DISABLE_XCPRETTY === 'false') cmdStr = cmdStr + ' | xcpretty'
cmdStr = cmdStr + ' && exit ${PIPE' + 'STATUS[0]}'
call(cmdStr)
const appFile = escapePath(`${productName}.app`)
const appFileDir = escapePath(
`${buildDir}/Build/Products/Release-iphonesimulator/`
)
buildObj.ipaFile = escapePath(
`${tmpDir}/${productNameClean}-${repoBranch}-${buildNum}-${guiHash.slice(
0,
8
)}.zip`
)
if (fs.existsSync(buildObj.ipaFile)) {
call('rm ' + buildObj.ipaFile)
}
mylog('Creating Zipped .app for ' + xcodeScheme)
chdir(appFileDir)
cmdStr = `/usr/bin/zip -r ${buildObj.ipaFile} ./${appFile}`
call(cmdStr)
}
function buildAndroid(buildObj: BuildObj): void {
const {
buildArchivesDir,
buildNum,
platformType,
repoBranch,
guiPlatformDir,
maestroBuild,
bundleToolPath,
androidKeyStore,
androidKeyStoreAlias,
androidKeyStorePassword
} = buildObj
const keyStoreFile = join('/', _rootProjectDir, 'keystores', androidKeyStore)
const patchDir = getPatchDir(buildObj)
if (fs.existsSync(join(patchDir, 'google-services.json'))) {
call(`cp -a ${join(patchDir, 'google-services.json')} android/app/`)
} else if (fs.existsSync(`${buildObj.guiDir}/google-services.json`)) {
call(
`cp -a ${buildObj.guiDir}/google-services.json ${buildObj.guiPlatformDir}/app/`
)
}
chdir(buildObj.guiDir)
process.env.ORG_GRADLE_PROJECT_storeFile = keyStoreFile
process.env.ORG_GRADLE_PROJECT_storePassword =
buildObj.androidKeyStorePassword
process.env.ORG_GRADLE_PROJECT_keyAlias = buildObj.androidKeyStoreAlias
process.env.ORG_GRADLE_PROJECT_keyPassword = buildObj.androidKeyStorePassword
const archivePlatformDir = join(buildArchivesDir, repoBranch, platformType)
// Delete old archive directories
deleteOldDirsSync(archivePlatformDir, cutoffDate)
chdir(buildObj.guiPlatformDir)
call('./gradlew clean')
call('./gradlew signingReport')
call(sprintf('./gradlew %s', buildObj.androidTask))
const testBuild = maestroBuild ? '-maestro' : ''
// Process the AAB files created into APK format and place in archive directory
const outfile = `${buildObj.productNameClean}-${buildObj.repoBranch}-${buildObj.buildNum}${testBuild}`
const archiveDir = join(archivePlatformDir, String(buildNum))
fs.mkdirSync(archiveDir, { recursive: true })
const aabPath = join(archiveDir, `${outfile}.aab`)
const apksPath = join(archiveDir, `${outfile}.apks`)
const apkPathDir = join(archiveDir, `${outfile}_apk_container`)
fs.copyFileSync(
join(guiPlatformDir, '/app/build/outputs/bundle/release/app-release.aab'),
aabPath
)
call(
`java -jar ${bundleToolPath} build-apks --overwrite --mode=universal --bundle=${aabPath} --output=${apksPath} --ks=${keyStoreFile} --ks-key-alias=${androidKeyStoreAlias} --ks-pass=pass:${androidKeyStorePassword}`
)
call(`unzip ${apksPath} -d ${apkPathDir}`)
const universalApk = join(apkPathDir, 'universal.apk')
buildObj.ipaFile = join(apkPathDir, `${outfile}.apk`)
fs.renameSync(universalApk, buildObj.ipaFile)
}
function buildCommonPost(buildObj: BuildObj): void {
const { maestroBuild, zealotApiToken, zealotChannelKey, zealotUrl } = buildObj
let curl
const notes = `${buildObj.productName} ${buildObj.version} (${buildObj.buildNum}) branch: ${buildObj.repoBranch} #${buildObj.guiHash}`
if (
buildObj.hockeyAppToken != null &&
buildObj.hockeyAppToken !== '' &&
buildObj.hockeyAppId != null &&
buildObj.hockeyAppId !== '' &&
!maestroBuild
) {
mylog('\n\nUploading to HockeyApp')
mylog('**********************\n')
const url = sprintf(
'https://rink.hockeyapp.net/api/2/apps/%s/app_versions/upload',
buildObj.hockeyAppId
)
curl = sprintf(
'/usr/bin/curl -F ipa=@%s -H "X-HockeyAppToken: %s" -F "notes_type=1" -F "status=2" -F "notify=0" -F "tags=%s" -F "notes=%s" ',
buildObj.ipaFile,
buildObj.hockeyAppToken,
buildObj.hockeyAppTags,
notes
)
if (buildObj.dSymZip !== undefined) {
curl += sprintf('-F dsym=@%s ', buildObj.dSymZip)
}
curl += url
call(curl)
mylog('\nUploaded to HockeyApp')
}
if (
zealotApiToken != null &&
zealotUrl != null &&
zealotChannelKey != null &&
!maestroBuild
) {
const branch = encodeURIComponent(buildObj.repoBranch)
const gitCommit = encodeURIComponent(buildObj.guiHash)
chdir(buildObj.guiDir)
const changes = cmd(
`git diff HEAD^ HEAD CHANGELOG.md | { grep '^+[^+]' || true; }`
)
const changelog = encodeURIComponent(changes)
mylog(`\n\nUploading to Zealot: ${zealotUrl}`)
mylog(
'***********************************************************************\n'
)
call(
`curl -X POST "${zealotUrl}/api/apps/upload?token=${zealotApiToken}&channel_key=${zealotChannelKey}&branch=${branch}&git_commit=${gitCommit}&changelog=${changelog}" -F "file=@${buildObj.ipaFile}"`
)
mylog('\n*** Upload to Zealot Complete ***')
}
if (buildObj.rsyncLocation != null) {
const {
buildNum,
guiHash,
platformType,
productNameClean,
repoBranch,
testRepoUrl,
version
} = buildObj
mylog(`\n\nUploading to rsyncLocation ${buildObj.rsyncLocation}`)
mylog(
'***********************************************************************\n'
)
const datePrefix = new Date()
.toISOString()
.slice(2, 19)
.replace(/:/gi, '')
.replace(/-/gi, '')
const [fileExtension] = buildObj.ipaFile.split('.').reverse()
const testBuild = maestroBuild ? '--maestro' : ''
const rsyncFile = escapePath(
`${datePrefix}--${productNameClean}--${platformType}--${repoBranch}--${buildNum}--${guiHash.slice(
0,
8
)}${testBuild}.${fileExtension}`
)
const rsyncFilePath = join(buildObj.rsyncLocation, rsyncFile)
call(
`rsync -avz -e "ssh -i ${githubSshKey}" ${buildObj.ipaFile} ${rsyncFilePath}`
)
mylog('\n*** Upload to rsyncLocation Complete ***')
// Only update the test repo for maestro builds
if (testRepoUrl != null && maestroBuild) {
mylog(`\n\nUpdating test repo ${buildObj.testRepoUrl}`)
mylog('***********************************************************\n')
const pathTemp = testRepoUrl.split('/')
const repo = pathTemp[pathTemp.length - 1].replace('.git', '')
const repoPath = join(baseDir, repo)
const testFilePath = join(repoPath, LATEST_TEST_FILE)
let retries = 10
let success = false
while (--retries > 0) {
if (fs.existsSync(repoPath)) {
call(`rm -rf ${repoPath}`)
}
chdir(baseDir)
call(
`GIT_SSH_COMMAND="ssh -i ${githubSshKey}" git clone ${testRepoUrl}`
)
const latestTestFileObj: LatestTestFile = {
platformType,
branch: repoBranch,
buildNum,
version,
filePath: rsyncFilePath,
gitHash: guiHash
}
const platformBranch = `${repoBranch}/${platformType}`
chdir(repoPath)
try {
call(`git checkout -b ${platformBranch} origin/${platformBranch}`)
} catch (e) {
call(`git checkout -b ${platformBranch}`)
}
const latestTestFileString = JSON.stringify(latestTestFileObj, null, 2)
fs.writeFileSync(testFilePath, latestTestFileString, {
encoding: 'utf8'
})
call(`git add ${LATEST_TEST_FILE}`)
call(
`git commit -m "latestTestFile. ${buildNum} ${version} ${guiHash} ${platformBranch}"`
)
try {
call(
`GIT_SSH_COMMAND="ssh -i ${githubSshKey}" git push -u origin ${platformBranch}`
)
success = true
break
} catch (e: any) {
console.log('Error pushing version file...')
}
}
if (success) {
mylog('\n*** Updating test repo Complete ***')
} else {
mylog('\n*** Updating test repo FAILED ***')
throw new Error('Updating test repo FAILED')
}
}
}
}
function builddate(): string {
const date = new Date()
const dateStr = sprintf(
'%d-%02d-%02d',
date.getFullYear(),
date.getMonth() + 1,
date.getDate()
)
return dateStr
}
function rmNewline(text: string): string {
return text.replace(/(\r\n|\n|\r)/gm, '')
}
function chdir(path: string): void {
console.log('chdir: ' + path)
_currentPath = path
}
function call(cmdstring: string): void {
console.log('call: ' + cmdstring)
childProcess.execSync(cmdstring, {
encoding: 'utf8',
timeout: 3600000,
stdio: 'inherit',
cwd: _currentPath,
killSignal: 'SIGKILL'
})
}
function cmd(cmdstring: string): string {
console.log('cmd: ' + cmdstring)
const r = childProcess.execSync(cmdstring, {
encoding: 'utf8',
timeout: 3600000,
cwd: _currentPath,
killSignal: 'SIGKILL'
})
return r
}
function getPatchDir(buildObj: BuildObj): string {
const { projectName, guiDir, repoBranch } = buildObj
return join(guiDir, 'deployPatches', projectName, repoBranch)
}
function escapePath(path: string): string {
return path.replace(/(\s+)/g, '\\$1')
}