-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
537 lines (465 loc) · 16.4 KB
/
main.go
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
package main
import (
"bytes"
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/bitrise-io/go-steputils/cache"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/retry"
"github.com/kballard/go-shellquote"
)
const failedToFindTargetWithHashString = `Failed to find target with hash string `
const failedToFindBuildToolRevision = `Failed to find Build Tools revision `
const failedToFindPlatformSDKWithPath = `Failed to find Platform SDK with path: `
const couldNotFind = `Could not find `
const couldNotHEAD = `Could not HEAD `
const connectionTimedOut = `Connection timed out`
const couldNotRead = `Could not read `
const couldNotGetResource = `Could not get resource `
const couldNotGET = `Could not GET `
const couldNotDownload = `Could not download `
const receivedStatusCode503 = `Received status code 503 from server: Service Temporarily Unavailable`
const causeErrorInOpeningZipFile = `Cause: error in opening zip file.`
const failedToDownloadResource = `Failed to download resource`
const failedToDownloadSHA1ForResource = `Failed to download SHA1 for resource`
var automaticRetryReasonPatterns = []string{
failedToFindTargetWithHashString,
failedToFindBuildToolRevision,
failedToFindPlatformSDKWithPath,
couldNotFind,
couldNotHEAD,
connectionTimedOut,
couldNotRead,
couldNotGetResource,
couldNotGET,
couldNotDownload,
receivedStatusCode503,
causeErrorInOpeningZipFile,
failedToDownloadResource,
failedToDownloadSHA1ForResource,
}
// Config ...
type Config struct {
// Gradle Inputs
GradleFile string `env:"gradle_file"`
GradleTasks string `env:"gradle_task,required"`
GradlewPath string `env:"gradlew_path,file"`
GradleOptions string `env:"gradle_options"`
AppFileIncludeFilter string `env:"app_file_include_filter,required"`
AppFileExcludeFilter string `env:"app_file_exclude_filter"`
TestApkFileIncludeFilter string `env:"test_apk_file_include_filter"`
TestApkFileExcludeFilter string `env:"test_apk_file_exclude_filter"`
MappingFileIncludeFilter string `env:"mapping_file_include_filter"`
MappingFileExcludeFilter string `env:"mapping_file_exclude_filter"`
// Other configs
DeployDir string `env:"BITRISE_DEPLOY_DIR"`
// Cache configs
CacheLevel string `env:"cache_level,opt['all','only_deps','none']"`
// Deprecated
ApkFileIncludeFilter string `env:"apk_file_include_filter"`
ApkFileExcludeFilter string `env:"apk_file_exclude_filter"`
}
func isStringFoundInOutput(searchStr, outputToSearchIn string) bool {
r, err := regexp.Compile("(?i)" + searchStr)
if err != nil {
log.Warnf("Failed to compile regexp: %s", err)
return false
}
return r.MatchString(outputToSearchIn)
}
func runGradleTask(gradleTool, buildFile, tasks, options string, isAutomaticRetryOnReason bool) error {
optionSlice, err := shellquote.Split(options)
if err != nil {
return err
}
taskSlice, err := shellquote.Split(tasks)
if err != nil {
return err
}
cmdSlice := []string{gradleTool}
if buildFile != "" {
cmdSlice = append(cmdSlice, "--build-file", buildFile)
}
cmdSlice = append(cmdSlice, taskSlice...)
cmdSlice = append(cmdSlice, optionSlice...)
log.Printf(command.PrintableCommandArgs(false, cmdSlice))
fmt.Println()
var outBuffer bytes.Buffer
outWriter := io.MultiWriter(os.Stdout, &outBuffer)
cmd := command.New(cmdSlice[0], cmdSlice[1:]...)
cmd.SetStdout(outWriter)
cmd.SetStderr(outWriter)
if err := cmd.Run(); err != nil {
if isAutomaticRetryOnReason {
for _, retryReasonPattern := range automaticRetryReasonPatterns {
if isStringFoundInOutput(retryReasonPattern, outBuffer.String()) {
log.Warnf("Automatic retry reason found in log: %s - retrying...", retryReasonPattern)
return runGradleTask(gradleTool, buildFile, tasks, options, false)
}
}
}
return err
}
return nil
}
func computeMD5String(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer func() {
if err := f.Close(); err != nil {
log.Errorf("Failed to close file(%s), error: %s", filePath, err)
}
}()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
func filterEmpty(in []string) (out []string) {
for _, item := range in {
if strings.TrimSpace(item) != "" {
out = append(out, item)
}
}
return
}
func createDeployPth(deployDir, apkName string) (string, error) {
deployPth := filepath.Join(deployDir, apkName)
if exist, err := pathutil.IsPathExists(deployPth); err != nil {
return "", err
} else if exist {
return "", fmt.Errorf("file already exists at: %s", deployPth)
}
return deployPth, nil
}
func findDeployPth(deployDir, baseName, ext string) (string, error) {
deployPth := ""
retryApkName := baseName + ext
err := retry.Times(10).Wait(1 * time.Second).Try(func(attempt uint) error {
if attempt > 0 {
log.Warnf(" Retrying...")
}
pth, pathErr := createDeployPth(deployDir, retryApkName)
if pathErr != nil {
log.Warnf(" %d attempt failed:", attempt+1)
log.Printf(pathErr.Error())
}
t := time.Now()
retryApkName = baseName + t.Format("20060102150405") + ext
deployPth = pth
return pathErr
})
return deployPth, err
}
func validateAndMigrateConfig(config *Config) error {
if config.GradleFile != "" {
if exist, err := pathutil.IsPathExists(config.GradleFile); err != nil {
return fmt.Errorf("failed to check if GradleFile exists at: %s, error: %s", config.GradleFile, err)
} else if !exist {
return fmt.Errorf("GradleFile does not exist at: %s", config.GradleFile)
}
}
if strings.TrimSpace(config.ApkFileIncludeFilter) != "" {
log.Warnf(`Step input 'APK file include filter' (apk_file_include_filter) is deprectad and will be removed soon,
use 'APK and AAB file include filter' (app_file_include_filter) instead.`)
fmt.Println()
log.Infof(`'APK file include filter' (apk_file_include_filter) is used, 'APK and AAB file include filter' (app_file_include_filter) is ignored.
Use 'APK and AAB file include filter' and set 'APK file include filter' to empty value.`)
fmt.Println()
config.AppFileIncludeFilter = config.ApkFileIncludeFilter
}
if strings.TrimSpace(config.ApkFileExcludeFilter) != "" {
log.Warnf(`Step input 'APK file exclude filter' (apk_file_exclude_filter) is deprectad and will be removed soon,
use 'APK and AAB file exclude filter' (app_file_exclude_filter) instead.`)
fmt.Println()
log.Infof(`'APK file exclude filter' (apk_file_exclude_filter) is used, 'APK and AAB file exclude filter' (app_file_exclude_filter) is ignored.
Use 'APK and AAB file exclude filter' and set 'APK file exclude filter' to empty value.`)
fmt.Println()
config.AppFileExcludeFilter = config.ApkFileExcludeFilter
}
return nil
}
func exportEnvironmentWithEnvman(keyStr, valueStr string) error {
cmd := command.New("envman", "add", "--key", keyStr)
cmd.SetStdin(strings.NewReader(valueStr))
return cmd.Run()
}
func failf(message string, args ...interface{}) {
log.Errorf(message, args...)
os.Exit(1)
}
func main() {
var configs Config
if err := stepconf.Parse(&configs); err != nil {
failf("Issue with input: %s", err)
}
stepconf.Print(configs)
if err := validateAndMigrateConfig(&configs); err != nil {
failf("Issue with input: %s", err)
}
fmt.Println()
gradlewPath, err := filepath.Abs(configs.GradlewPath)
if err != nil {
failf("Can't get absolute path for gradlew file (%s), error: %s", configs.GradlewPath, err)
}
if err := os.Chmod(gradlewPath, 0770); err != nil {
failf("Failed to add executable permission on gradlew file (%s), error: %s", gradlewPath, err)
}
gradleStarted := time.Now()
log.Infof("Running gradle task...")
if err := runGradleTask(gradlewPath, configs.GradleFile, configs.GradleTasks, configs.GradleOptions, true); err != nil {
failf("Gradle task failed, error: %s", err)
}
// Collecting caches
if configs.CacheLevel != "none" {
fmt.Println()
log.Infof("Collecting gradle caches...")
gradleCache := cache.New()
homeDir := pathutil.UserHomeDir()
collectCaches := true
includePths := []string{}
projectRoot, err := filepath.Abs(".")
if err != nil {
log.Warnf("Cache collection skipped: failed to determine project root path, error: %s", err)
collectCaches = false
}
lockFilePath := filepath.Join(projectRoot, "gradle.deps")
if configs.CacheLevel == "all" || configs.CacheLevel == "only_deps" {
// create dependencies lockfile
log.Printf(" Generate dependencies map...")
lockfileContent := ""
if err := filepath.Walk(projectRoot, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() && strings.HasSuffix(f.Name(), ".gradle") && !strings.Contains(path, "node_modules") {
if md5Hash, err := computeMD5String(path); err != nil {
log.Warnf("Failed to compute MD5 hash of file(%s), error: %s", path, err)
} else {
lockfileContent += md5Hash
}
}
return nil
}); err != nil {
log.Warnf("Dependency map generation skipped: failed to collect dependencies, error: ", err)
collectCaches = false
} else {
err := fileutil.WriteStringToFile(lockFilePath, lockfileContent)
if err != nil {
log.Warnf("Dependency map generation skipped: failed to write lockfile, error: %s", err)
collectCaches = false
}
}
includePths = append(includePths, fmt.Sprintf("%s -> %s", filepath.Join(homeDir, ".gradle"), lockFilePath))
includePths = append(includePths, fmt.Sprintf("%s -> %s", filepath.Join(homeDir, ".kotlin"), lockFilePath))
includePths = append(includePths, fmt.Sprintf("%s -> %s", filepath.Join(homeDir, ".m2"), lockFilePath))
}
if configs.CacheLevel == "all" {
includePths = append(includePths, fmt.Sprintf("%s -> %s", filepath.Join(homeDir, ".android", "build-cache"), lockFilePath))
}
excludePths := []string{
"~/.gradle/**",
"~/.android/build-cache/**",
"*.lock",
"*.bin",
"/**/build/**.json",
"/**/build/**.html",
"/**/build/**.xml",
"/**/build/**.properties",
"/**/build/**/zip-cache/**",
"*.log",
"*.txt",
"*.rawproto",
"!*.ap_",
"!*.apk",
}
if configs.CacheLevel == "all" {
if err := filepath.Walk(projectRoot, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
if f.Name() == "build" {
includePths = append(includePths, path)
}
if f.Name() == ".gradle" {
includePths = append(includePths, path)
}
}
return nil
}); err != nil {
log.Warnf("Cache collection skipped: failed to determine cache paths, error: ", err)
collectCaches = false
}
}
if collectCaches {
gradleCache.IncludePath(strings.Join(includePths, "\n"))
gradleCache.ExcludePath(strings.Join(excludePths, "\n"))
if err := gradleCache.Commit(); err != nil {
log.Warnf("Cache collection skipped: failed to commit cache paths, error: %s", err)
}
}
log.Donef("Done")
}
// Move apk and aab files
fmt.Println()
log.Infof("Move APK and AAB files...")
appFiles, err := findArtifacts(".",
filePatterns{
include: filterEmpty(strings.Split(configs.AppFileIncludeFilter, "\n")),
exclude: filterEmpty(strings.Split(configs.AppFileExcludeFilter, "\n")),
})
if err != nil {
failf("Failed to find APK or AAB files, error: %s", err)
}
if len(appFiles) == 0 {
log.Warnf("No file name matched app filters")
}
var copiedApkFiles []string
var copiedAabFiles []string
for _, appFile := range appFiles {
fi, err := os.Lstat(appFile)
if err != nil {
failf("Failed to get file info, error: %s", err)
}
if fi.ModTime().Before(gradleStarted) {
log.Warnf("skipping: %s, modified before the gradle task has started", appFile)
continue
}
ext := filepath.Ext(appFile)
baseName := filepath.Base(appFile)
baseName = strings.TrimSuffix(baseName, ext)
deployPth, err := findDeployPth(configs.DeployDir, baseName, ext)
if err != nil {
failf("Failed to create apk deploy path, error: %s", err)
}
log.Printf("copy %s to %s", appFile, deployPth)
if err := command.CopyFile(appFile, deployPth); err != nil {
failf("Failed to copy apk, error: %s", err)
}
switch strings.ToLower(ext) {
case ".apk":
copiedApkFiles = append(copiedApkFiles, deployPth)
case ".aab":
copiedAabFiles = append(copiedAabFiles, deployPth)
default:
}
}
for appEnv, appFiles := range map[string][]string{
"BITRISE_APK_PATH": copiedApkFiles,
"BITRISE_AAB_PATH": copiedAabFiles} {
if len(appFiles) != 0 {
lastCopiedFile := appFiles[len(appFiles)-1]
if err := exportEnvironmentWithEnvman(appEnv, lastCopiedFile); err != nil {
failf("Failed to export enviroment (%s), error: %s", appEnv, err)
}
log.Donef("The app path is now available in the Environment Variable: $%s (value: %s)", appEnv, lastCopiedFile)
}
}
for appListEnv, appFiles := range map[string][]string{
"BITRISE_APK_PATH_LIST": copiedApkFiles,
"BITRISE_AAB_PATH_LIST": copiedAabFiles} {
if len(appFiles) != 0 {
appList := strings.Join(appFiles, "|")
if err := exportEnvironmentWithEnvman(appListEnv, appList); err != nil {
failf("Failed to export enviroment (%s), error: %s", appListEnv, err)
}
log.Donef("The app paths list is now available in the Environment Variable: $%s (value: %s)", appListEnv, appList)
}
}
testApkFiles, err := findArtifacts(".",
filePatterns{
include: filterEmpty(strings.Split(configs.TestApkFileIncludeFilter, "\n")),
exclude: filterEmpty(strings.Split(configs.TestApkFileExcludeFilter, "\n")),
})
if err != nil {
failf("Failed to find test apk files, error: %s", err)
}
if len(testApkFiles) == 0 {
log.Warnf("No file name matched test apk filters")
}
lastCopiedTestApkFile := ""
for _, apkFile := range testApkFiles {
fi, err := os.Lstat(apkFile)
if err != nil {
failf("Failed to get file info, error: %s", err)
}
if fi.ModTime().Before(gradleStarted) {
log.Warnf("skipping: %s, modified before the gradle task has started", apkFile)
continue
}
ext := filepath.Ext(apkFile)
baseName := filepath.Base(apkFile)
baseName = strings.TrimSuffix(baseName, ext)
deployPth, err := findDeployPth(configs.DeployDir, baseName, ext)
if err != nil {
failf("Failed to create apk deploy path, error: %s", err)
}
log.Printf("copy %s to %s", apkFile, deployPth)
if err := command.CopyFile(apkFile, deployPth); err != nil {
failf("Failed to copy apk, error: %s", err)
}
lastCopiedTestApkFile = deployPth
}
if lastCopiedTestApkFile != "" {
if err := exportEnvironmentWithEnvman("BITRISE_TEST_APK_PATH", lastCopiedTestApkFile); err != nil {
failf("Failed to export enviroment (BITRISE_TEST_APK_PATH), error: %s", err)
}
log.Donef("The apk path is now available in the Environment Variable: $BITRISE_TEST_APK_PATH (value: %s)", lastCopiedTestApkFile)
}
// Move mapping files
log.Infof("Move mapping files...")
mappingFiles, err := findArtifacts(".",
filePatterns{
include: filterEmpty(strings.Split(configs.MappingFileIncludeFilter, "\n")),
exclude: filterEmpty(strings.Split(configs.MappingFileExcludeFilter, "\n")),
})
if err != nil {
failf("Failed to find mapping files, error: %s", err)
}
if len(mappingFiles) == 0 {
log.Printf("No mapping file matched the filters")
}
lastCopiedMappingFile := ""
for _, mappingFile := range mappingFiles {
fi, err := os.Lstat(mappingFile)
if err != nil {
failf("Failed to get file info, error: %s", err)
}
if fi.ModTime().Before(gradleStarted) {
log.Warnf("skipping: %s, modified before the gradle task has started", mappingFile)
continue
}
ext := filepath.Ext(mappingFile)
baseName := filepath.Base(mappingFile)
baseName = strings.TrimSuffix(baseName, ext)
deployPth, err := findDeployPth(configs.DeployDir, baseName, ext)
if err != nil {
failf("Failed to create mapping deploy path, error: %s", err)
}
log.Printf("copy %s to %s", mappingFile, deployPth)
if err := command.CopyFile(mappingFile, deployPth); err != nil {
failf("Failed to copy mapping file, error: %s", err)
}
lastCopiedMappingFile = deployPth
}
if lastCopiedMappingFile != "" {
if err := exportEnvironmentWithEnvman("BITRISE_MAPPING_PATH", lastCopiedMappingFile); err != nil {
failf("Failed to export enviroment (BITRISE_MAPPING_PATH), error: %s", err)
}
log.Donef("The mapping path is now available in the Environment Variable: $BITRISE_MAPPING_PATH (value: %s)", lastCopiedMappingFile)
}
}