/
run_util.go
944 lines (808 loc) · 35.5 KB
/
run_util.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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
package cli
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/bitrise-io/bitrise/analytics"
"github.com/bitrise-io/bitrise/bitrise"
"github.com/bitrise-io/bitrise/configs"
"github.com/bitrise-io/bitrise/log"
"github.com/bitrise-io/bitrise/log/logwriter"
"github.com/bitrise-io/bitrise/models"
"github.com/bitrise-io/bitrise/toolkits"
"github.com/bitrise-io/bitrise/tools"
"github.com/bitrise-io/envman/env"
envmanModels "github.com/bitrise-io/envman/models"
"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/pointers"
"github.com/bitrise-io/go-utils/retry"
coreanalytics "github.com/bitrise-io/go-utils/v2/analytics"
"github.com/bitrise-io/go-utils/versions"
stepmanModels "github.com/bitrise-io/stepman/models"
)
func isPRMode(prGlobalFlagPtr *bool, inventoryEnvironments []envmanModels.EnvironmentItemModel) (bool, error) {
if prGlobalFlagPtr != nil {
return *prGlobalFlagPtr, nil
}
prIDEnv := os.Getenv(configs.PullRequestIDEnvKey)
prModeEnv := os.Getenv(configs.PRModeEnvKey)
if prIDEnv != "" || prModeEnv == "true" {
return true, nil
}
for _, env := range inventoryEnvironments {
key, value, err := env.GetKeyValuePair()
if err != nil {
return false, err
}
if key == configs.PullRequestIDEnvKey && value != "" {
return true, nil
}
if key == configs.PRModeEnvKey && value == "true" {
return true, nil
}
}
return false, nil
}
func registerPrMode(isPRMode bool) error {
configs.IsPullRequestMode = isPRMode
return os.Setenv(configs.PRModeEnvKey, strconv.FormatBool(isPRMode))
}
func isCIMode(ciGlobalFlagPtr *bool, inventoryEnvironments []envmanModels.EnvironmentItemModel) (bool, error) {
if ciGlobalFlagPtr != nil {
return *ciGlobalFlagPtr, nil
}
ciModeEnv := os.Getenv(configs.CIModeEnvKey)
if ciModeEnv == "true" {
return true, nil
}
for _, env := range inventoryEnvironments {
key, value, err := env.GetKeyValuePair()
if err != nil {
return false, err
}
if key == configs.CIModeEnvKey && value == "true" {
return true, nil
}
}
return false, nil
}
func registerCIMode(isCIMode bool) error {
configs.IsCIMode = isCIMode
return os.Setenv(configs.CIModeEnvKey, strconv.FormatBool(isCIMode))
}
func isSecretFiltering(filteringFlag *bool, inventoryEnvironments []envmanModels.EnvironmentItemModel) (bool, error) {
if filteringFlag != nil {
return *filteringFlag, nil
}
expandedEnvs, err := tools.ExpandEnvItems(inventoryEnvironments, os.Environ())
if err != nil {
return false, err
}
value, ok := expandedEnvs[configs.IsSecretFilteringKey]
if ok {
if value == "true" {
return true, nil
} else if value == "false" {
return false, nil
}
}
return true, nil
}
func registerSecretFiltering(filtering bool) error {
configs.IsSecretFiltering = filtering
return os.Setenv(configs.IsSecretFilteringKey, strconv.FormatBool(filtering))
}
func isSecretEnvsFiltering(filteringFlag *bool, inventoryEnvironments []envmanModels.EnvironmentItemModel) (bool, error) {
if filteringFlag != nil {
return *filteringFlag, nil
}
expandedEnvs, err := tools.ExpandEnvItems(inventoryEnvironments, os.Environ())
if err != nil {
return false, err
}
value, ok := expandedEnvs[configs.IsSecretEnvsFilteringKey]
if ok {
if value == "true" {
return true, nil
} else if value == "false" {
return false, nil
}
}
return true, nil
}
func registerSecretEnvsFiltering(filtering bool) error {
configs.IsSecretEnvsFiltering = filtering
return os.Setenv(configs.IsSecretEnvsFilteringKey, strconv.FormatBool(filtering))
}
func isDirEmpty(path string) (bool, error) {
entries, err := ioutil.ReadDir(path)
if err != nil {
return false, err
}
return len(entries) == 0, nil
}
// GetBitriseConfigFromBase64Data ...
func GetBitriseConfigFromBase64Data(configBase64Str string) (models.BitriseDataModel, []string, error) {
configBase64Bytes, err := base64.StdEncoding.DecodeString(configBase64Str)
if err != nil {
return models.BitriseDataModel{}, []string{}, fmt.Errorf("Failed to decode base 64 string, error: %s", err)
}
config, warnings, err := bitrise.ConfigModelFromYAMLBytes(configBase64Bytes)
if err != nil {
return models.BitriseDataModel{}, warnings, fmt.Errorf("Failed to parse bitrise config, error: %s", err)
}
return config, warnings, nil
}
// GetBitriseConfigFilePath ...
func GetBitriseConfigFilePath(bitriseConfigPath string) (string, error) {
if bitriseConfigPath == "" {
bitriseConfigPath = filepath.Join(configs.CurrentDir, DefaultBitriseConfigFileName)
if exist, err := pathutil.IsPathExists(bitriseConfigPath); err != nil {
return "", err
} else if !exist {
return "", fmt.Errorf("bitrise.yml path not defined and not found on it's default path: %s", bitriseConfigPath)
}
}
return bitriseConfigPath, nil
}
// CreateBitriseConfigFromCLIParams ...
func CreateBitriseConfigFromCLIParams(bitriseConfigBase64Data, bitriseConfigPath string) (models.BitriseDataModel, []string, error) {
bitriseConfig := models.BitriseDataModel{}
warnings := []string{}
if bitriseConfigBase64Data != "" {
config, warns, err := GetBitriseConfigFromBase64Data(bitriseConfigBase64Data)
warnings = warns
if err != nil {
return models.BitriseDataModel{}, warnings, fmt.Errorf("Failed to get config (bitrise.yml) from base 64 data, err: %s", err)
}
bitriseConfig = config
} else {
bitriseConfigPath, err := GetBitriseConfigFilePath(bitriseConfigPath)
if err != nil {
return models.BitriseDataModel{}, []string{}, fmt.Errorf("Failed to get config (bitrise.yml) path: %s", err)
}
if bitriseConfigPath == "" {
return models.BitriseDataModel{}, []string{}, errors.New("Failed to get config (bitrise.yml) path: empty bitriseConfigPath")
}
config, warns, err := bitrise.ReadBitriseConfig(bitriseConfigPath)
warnings = warns
if err != nil {
return models.BitriseDataModel{}, warnings, fmt.Errorf("Config (path:%s) is not valid: %s", bitriseConfigPath, err)
}
bitriseConfig = config
}
isConfigVersionOK, err := versions.IsVersionGreaterOrEqual(models.Version, bitriseConfig.FormatVersion)
if err != nil {
log.Warn("bitrise CLI model version: ", models.Version)
log.Warn("bitrise.yml Format Version: ", bitriseConfig.FormatVersion)
return models.BitriseDataModel{}, warnings, fmt.Errorf("Failed to compare bitrise CLI models's version with the bitrise.yml FormatVersion: %s", err)
}
if !isConfigVersionOK {
log.Warnf("The bitrise.yml has a higher Format Version (%s) than the bitrise CLI model's version (%s).", bitriseConfig.FormatVersion, models.Version)
return models.BitriseDataModel{}, warnings, errors.New("This bitrise.yml was created with and for a newer version of bitrise CLI, please upgrade your bitrise CLI to use this bitrise.yml")
}
return bitriseConfig, warnings, nil
}
// GetInventoryFromBase64Data ...
func GetInventoryFromBase64Data(inventoryBase64Str string) ([]envmanModels.EnvironmentItemModel, error) {
inventoryBase64Bytes, err := base64.StdEncoding.DecodeString(inventoryBase64Str)
if err != nil {
return []envmanModels.EnvironmentItemModel{}, fmt.Errorf("Failed to decode base 64 string, error: %s", err)
}
inventory, err := bitrise.InventoryModelFromYAMLBytes(inventoryBase64Bytes)
if err != nil {
return []envmanModels.EnvironmentItemModel{}, err
}
return inventory.Envs, nil
}
// GetInventoryFilePath ...
func GetInventoryFilePath(inventoryPath string) (string, error) {
if inventoryPath == "" {
log.Debug("[BITRISE_CLI] - Inventory path not defined, searching for " + DefaultSecretsFileName + " in current folder...")
inventoryPath = filepath.Join(configs.CurrentDir, DefaultSecretsFileName)
if exist, err := pathutil.IsPathExists(inventoryPath); err != nil {
return "", err
} else if !exist {
inventoryPath = ""
}
}
return inventoryPath, nil
}
// CreateInventoryFromCLIParams ...
func CreateInventoryFromCLIParams(inventoryBase64Data, inventoryPath string) ([]envmanModels.EnvironmentItemModel, error) {
inventoryEnvironments := []envmanModels.EnvironmentItemModel{}
if inventoryBase64Data != "" {
inventory, err := GetInventoryFromBase64Data(inventoryBase64Data)
if err != nil {
return []envmanModels.EnvironmentItemModel{}, fmt.Errorf("Failed to get inventory from base 64 data, err: %s", err)
}
inventoryEnvironments = inventory
} else {
inventoryPath, err := GetInventoryFilePath(inventoryPath)
if err != nil {
return []envmanModels.EnvironmentItemModel{}, fmt.Errorf("Failed to get inventory path: %s", err)
}
if inventoryPath != "" {
bytes, err := fileutil.ReadBytesFromFile(inventoryPath)
if err != nil {
return []envmanModels.EnvironmentItemModel{}, err
}
if len(bytes) == 0 {
return []envmanModels.EnvironmentItemModel{}, errors.New("empty config")
}
inventory, err := bitrise.CollectEnvironmentsFromFile(inventoryPath)
if err != nil {
return []envmanModels.EnvironmentItemModel{}, fmt.Errorf("Invalid inventory format: %s", err)
}
inventoryEnvironments = inventory
}
}
return inventoryEnvironments, nil
}
func getCurrentBitriseSourceDir(envlist []envmanModels.EnvironmentItemModel) (string, error) {
bitriseSourceDir := os.Getenv(configs.BitriseSourceDirEnvKey)
for i := len(envlist) - 1; i >= 0; i-- {
env := envlist[i]
key, value, err := env.GetKeyValuePair()
if err != nil {
return bitriseSourceDir, err
}
if key == configs.BitriseSourceDirEnvKey && value != "" {
return value, nil
}
}
return bitriseSourceDir, nil
}
func checkAndInstallStepDependencies(step stepmanModels.StepModel) error {
if len(step.Dependencies) > 0 {
log.Warnf("step.dependencies is deprecated... Use step.deps instead.")
}
if step.Deps != nil && (len(step.Deps.Brew) > 0 || len(step.Deps.AptGet) > 0) {
//
// New dependency handling
switch runtime.GOOS {
case "darwin":
for _, brewDep := range step.Deps.Brew {
if err := bitrise.InstallWithBrewIfNeeded(brewDep, configs.IsCIMode); err != nil {
log.Infof("Failed to install (%s) with brew", brewDep.Name)
return err
}
log.Infof(" * "+colorstring.Green("[OK]")+" Step dependency (%s) installed, available.", brewDep.GetBinaryName())
}
case "linux":
for _, aptGetDep := range step.Deps.AptGet {
log.Infof("Start installing (%s) with apt-get", aptGetDep.Name)
if err := bitrise.InstallWithAptGetIfNeeded(aptGetDep, configs.IsCIMode); err != nil {
log.Infof("Failed to install (%s) with apt-get", aptGetDep.Name)
return err
}
log.Infof(" * "+colorstring.Green("[OK]")+" Step dependency (%s) installed, available.", aptGetDep.GetBinaryName())
}
default:
return errors.New("unsupported os")
}
} else if len(step.Dependencies) > 0 {
log.Info("Deprecated dependencies found")
//
// Deprecated dependency handling
for _, dep := range step.Dependencies {
isSkippedBecauseOfPlatform := false
switch dep.Manager {
case depManagerBrew:
if runtime.GOOS == "darwin" {
err := bitrise.InstallWithBrewIfNeeded(stepmanModels.BrewDepModel{Name: dep.Name}, configs.IsCIMode)
if err != nil {
return err
}
} else {
isSkippedBecauseOfPlatform = true
}
break
default:
return errors.New("Not supported dependency (" + dep.Manager + ") (" + dep.Name + ")")
}
if isSkippedBecauseOfPlatform {
log.Debugf(" * Dependency (%s) skipped, manager (%s) not supported on this platform (%s)", dep.Name, dep.Manager, runtime.GOOS)
} else {
log.Infof(" * "+colorstring.Green("[OK]")+" Step dependency (%s) installed, available.", dep.Name)
}
}
}
return nil
}
func executeStep(
stepUUID string,
step stepmanModels.StepModel, sIDData models.StepIDData,
stepAbsDirPath, bitriseSourceDir string,
secrets []string) (int, error) {
toolkitForStep := toolkits.ToolkitForStep(step)
toolkitName := toolkitForStep.ToolkitName()
if err := toolkitForStep.PrepareForStepRun(step, sIDData, stepAbsDirPath); err != nil {
return 1, fmt.Errorf("Failed to prepare the step for execution through the required toolkit (%s), error: %s",
toolkitName, err)
}
cmd, err := toolkitForStep.StepRunCommandArguments(step, sIDData, stepAbsDirPath)
if err != nil {
return 1, fmt.Errorf("Toolkit (%s) rejected the step, error: %s",
toolkitName, err)
}
timeout := time.Duration(-1)
if step.Timeout != nil && *step.Timeout > 0 {
timeoutSeconds := *step.Timeout
timeout = time.Duration(timeoutSeconds) * time.Second
}
noOutputTimeout := configs.NoOutputTimeout
if step.NoOutputTimeout != nil {
noOutputTimeout = time.Duration(*step.NoOutputTimeout) * time.Second
}
opts := log.GetGlobalLoggerOpts()
opts.Producer = log.Step
opts.ProducerID = stepUUID
logWriter := logwriter.NewLogWriter(log.NewLogger(opts))
return tools.EnvmanRun(
configs.InputEnvstorePath,
bitriseSourceDir,
cmd,
timeout,
noOutputTimeout,
secrets,
nil,
&logWriter,
&logWriter)
}
func runStep(
stepUUID string,
step stepmanModels.StepModel, stepIDData models.StepIDData, stepDir string,
environments []envmanModels.EnvironmentItemModel, secrets []string) (int, []envmanModels.EnvironmentItemModel, error) {
log.Debugf("[BITRISE_CLI] - Try running step: %s (%s)", stepIDData.IDorURI, stepIDData.Version)
// Check & Install Step Dependencies
// [!] Make sure this happens BEFORE the Toolkit Bootstrap,
// so that if a Toolkit requires/allows the use of additional dependencies
// required for the step (e.g. a brew installed OpenSSH) it can be done
// with a Toolkit+Deps
if err := retry.Times(2).Try(func(attempt uint) error {
if attempt > 0 {
log.Print()
log.Warn("Installing Step dependency failed, retrying ...")
}
return checkAndInstallStepDependencies(step)
}); err != nil {
return 1, []envmanModels.EnvironmentItemModel{},
fmt.Errorf("Failed to install Step dependency, error: %s", err)
}
if err := tools.EnvmanInit(configs.InputEnvstorePath, true); err != nil {
return 1, []envmanModels.EnvironmentItemModel{}, err
}
if err := tools.EnvmanAddEnvs(configs.InputEnvstorePath, environments); err != nil {
return 1, []envmanModels.EnvironmentItemModel{}, err
}
// Run step
bitriseSourceDir, err := getCurrentBitriseSourceDir(environments)
if err != nil {
return 1, []envmanModels.EnvironmentItemModel{}, err
}
if bitriseSourceDir == "" {
bitriseSourceDir = configs.CurrentDir
}
if exit, err := executeStep(stepUUID, step, stepIDData, stepDir, bitriseSourceDir, secrets); err != nil {
stepOutputs, envErr := bitrise.CollectEnvironmentsFromFile(configs.OutputEnvstorePath)
if envErr != nil {
return 1, []envmanModels.EnvironmentItemModel{}, envErr
}
updatedStepOutputs, updateErr := stepOutputs, error(nil)
if configs.IsSecretEnvsFiltering {
updatedStepOutputs, updateErr = bitrise.ApplySensitiveOutputs(updatedStepOutputs, step.Outputs)
if updateErr != nil {
return 1, []envmanModels.EnvironmentItemModel{}, updateErr
}
}
updatedStepOutputs, updateErr = bitrise.ApplyOutputAliases(updatedStepOutputs, step.Outputs)
if updateErr != nil {
return 1, []envmanModels.EnvironmentItemModel{}, updateErr
}
return exit, updatedStepOutputs, err
}
stepOutputs, err := bitrise.CollectEnvironmentsFromFile(configs.OutputEnvstorePath)
if err != nil {
return 1, []envmanModels.EnvironmentItemModel{}, err
}
updatedStepOutputs, updateErr := stepOutputs, error(nil)
if configs.IsSecretEnvsFiltering {
updatedStepOutputs, updateErr = bitrise.ApplySensitiveOutputs(updatedStepOutputs, step.Outputs)
if updateErr != nil {
return 1, []envmanModels.EnvironmentItemModel{}, updateErr
}
}
updatedStepOutputs, updateErr = bitrise.ApplyOutputAliases(updatedStepOutputs, step.Outputs)
if updateErr != nil {
return 1, []envmanModels.EnvironmentItemModel{}, updateErr
}
log.Debugf("[BITRISE_CLI] - Step executed: %s (%s)", stepIDData.IDorURI, stepIDData.Version)
return 0, updatedStepOutputs, nil
}
func activateStepLibStep(stepIDData models.StepIDData, destination, stepYMLCopyPth string, isStepLibUpdated bool) (stepmanModels.StepInfoModel, bool, error) {
didStepLibUpdate := false
log.Debugf("[BITRISE_CLI] - Steplib (%s) step (id:%s) (version:%s) found, activating step", stepIDData.SteplibSource, stepIDData.IDorURI, stepIDData.Version)
if err := tools.StepmanSetup(stepIDData.SteplibSource); err != nil {
return stepmanModels.StepInfoModel{}, false, err
}
versionConstraint, err := stepmanModels.ParseRequiredVersion(stepIDData.Version)
if err != nil {
return stepmanModels.StepInfoModel{}, false,
fmt.Errorf("activating step (%s) from source (%s) failed, invalid version specified: %s", stepIDData.IDorURI, stepIDData.SteplibSource, err)
}
if versionConstraint.VersionLockType == stepmanModels.InvalidVersionConstraint {
return stepmanModels.StepInfoModel{}, false,
fmt.Errorf("activating step (%s) from source (%s) failed, version constraint is invalid", stepIDData.IDorURI, stepIDData.SteplibSource)
}
isStepLibUpdateNeeded := (versionConstraint.VersionLockType == stepmanModels.Latest) ||
(versionConstraint.VersionLockType == stepmanModels.MinorLocked) ||
(versionConstraint.VersionLockType == stepmanModels.MajorLocked)
if !isStepLibUpdated && isStepLibUpdateNeeded {
log.Print("Step uses latest version, updating StepLib...")
if err := tools.StepmanUpdate(stepIDData.SteplibSource); err != nil {
log.Warnf("Step version constraint is latest or version locked, but failed to update StepLib, err: %s", err)
} else {
didStepLibUpdate = true
}
}
info, err := tools.StepmanStepInfo(stepIDData.SteplibSource, stepIDData.IDorURI, stepIDData.Version)
if err != nil {
if isStepLibUpdated {
return stepmanModels.StepInfoModel{}, didStepLibUpdate, fmt.Errorf("stepman JSON steplib step info failed: %s", err)
}
// May StepLib should be updated
log.Infof("Step info not found in StepLib (%s) -- Updating ...", stepIDData.SteplibSource)
if err := tools.StepmanUpdate(stepIDData.SteplibSource); err != nil {
return stepmanModels.StepInfoModel{}, didStepLibUpdate, err
}
didStepLibUpdate = true
info, err = tools.StepmanStepInfo(stepIDData.SteplibSource, stepIDData.IDorURI, stepIDData.Version)
if err != nil {
return stepmanModels.StepInfoModel{}, didStepLibUpdate, fmt.Errorf("stepman JSON steplib step info failed: %s", err)
}
}
if info.Step.Title == nil || *info.Step.Title == "" {
info.Step.Title = pointers.NewStringPtr(info.ID)
}
info.OriginalVersion = stepIDData.Version
if err := tools.StepmanActivate(stepIDData.SteplibSource, stepIDData.IDorURI, info.Version, destination, stepYMLCopyPth); err != nil {
return stepmanModels.StepInfoModel{}, didStepLibUpdate, err
}
log.Debugf("[BITRISE_CLI] - Step activated: (ID:%s) (version:%s)", stepIDData.IDorURI, stepIDData.Version)
return info, didStepLibUpdate, nil
}
func activateAndRunSteps(
plan models.WorkflowExecutionPlan,
workflow models.WorkflowModel,
defaultStepLibSource string,
buildRunResults models.BuildRunResultsModel,
environments *[]envmanModels.EnvironmentItemModel,
secrets []envmanModels.EnvironmentItemModel,
isLastWorkflow bool,
tracker analytics.Tracker,
workflowIDProperties coreanalytics.Properties) models.BuildRunResultsModel {
log.Debug("[BITRISE_CLI] - Activating and running steps")
if len(workflow.Steps) == 0 {
log.Warnf("%s workflow has no steps to run, moving on to the next workflow...", workflow.Title)
return buildRunResults
}
// ------------------------------------------
// In function global variables - These are global for easy use in local register step run result methods.
var stepStartTime time.Time
runResultCollector := newBuildRunResultCollector(tracker)
// ------------------------------------------
// Main - Preparing & running the steps
for idx, stepListItm := range workflow.Steps {
stepPlan := plan.Steps[idx]
stepExecutionID := stepPlan.UUID
stepIDProperties := coreanalytics.Properties{analytics.StepExecutionID: stepExecutionID}
stepStartedProperties := workflowIDProperties.Merge(stepIDProperties)
// Per step variables
stepStartTime = time.Now()
isLastStep := isLastWorkflow && (idx == len(workflow.Steps)-1)
stepInfoPtr := stepmanModels.StepInfoModel{}
stepIdxPtr := idx
// Per step cleanup
if err := bitrise.SetBuildFailedEnv(buildRunResults.IsBuildFailed()); err != nil {
log.Error("Failed to set Build Status envs")
}
if err := bitrise.CleanupStepWorkDir(); err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}
//
// Preparing the step
if err := tools.EnvmanInit(configs.InputEnvstorePath, true); err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}
if err := tools.EnvmanAddEnvs(configs.InputEnvstorePath, *environments); err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}
// Get step id & version data
compositeStepIDStr, workflowStep, err := models.GetStepIDStepDataPair(stepListItm)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}
stepInfoPtr.ID = compositeStepIDStr
if workflowStep.Title != nil && *workflowStep.Title != "" {
stepInfoPtr.Step.Title = pointers.NewStringPtr(*workflowStep.Title)
} else {
stepInfoPtr.Step.Title = pointers.NewStringPtr(compositeStepIDStr)
}
stepIDData, err := models.CreateStepIDDataFromString(compositeStepIDStr, defaultStepLibSource)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}
stepInfoPtr.ID = stepIDData.IDorURI
if stepInfoPtr.Step.Title == nil || *stepInfoPtr.Step.Title == "" {
stepInfoPtr.Step.Title = pointers.NewStringPtr(stepIDData.IDorURI)
}
stepInfoPtr.Version = stepIDData.Version
stepInfoPtr.Library = stepIDData.SteplibSource
//
// Activating the step
stepDir := configs.BitriseWorkStepsDirPath
activator := newStepActivator()
stepYMLPth, origStepYMLPth, err := activator.activateStep(stepIDData, &buildRunResults, stepDir, configs.BitriseWorkDirPath, &workflowStep, &stepInfoPtr)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}
// Fill step info with default step info, if exist
mergedStep := workflowStep
if stepYMLPth != "" {
specStep, err := bitrise.ReadSpecStep(stepYMLPth)
log.Debugf("Spec read from YML: %#v", specStep)
if err != nil {
ymlPth := stepYMLPth
if origStepYMLPth != "" {
// in case of local step (path:./) we use the original step definition path,
// instead of the activated step's one.
ymlPth = origStepYMLPth
}
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, fmt.Errorf("failed to parse step definition (%s): %s", ymlPth, err),
isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}
mergedStep, err = models.MergeStepWith(specStep, workflowStep)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, stepmanModels.StepModel{}, stepInfoPtr, stepIdxPtr,
"", models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, true, map[string]string{}, stepStartedProperties)
continue
}
}
if mergedStep.SupportURL != nil {
stepInfoPtr.Step.SupportURL = pointers.NewStringPtr(*mergedStep.SupportURL)
}
if mergedStep.SourceCodeURL != nil {
stepInfoPtr.Step.SourceCodeURL = pointers.NewStringPtr(*mergedStep.SourceCodeURL)
}
if mergedStep.RunIf != nil {
stepInfoPtr.Step.RunIf = pointers.NewStringPtr(*mergedStep.RunIf)
}
if mergedStep.Timeout != nil {
stepInfoPtr.Step.Timeout = pointers.NewIntPtr(*mergedStep.Timeout)
}
if mergedStep.NoOutputTimeout != nil {
stepInfoPtr.Step.NoOutputTimeout = pointers.NewIntPtr(*mergedStep.NoOutputTimeout)
}
// At this point we have a filled up step info model and also have a step model which is contains the merged step
// data from the bitrise.yml and the steps step.yml.
// If the step title contains the step id or the step library as a prefix then we will take the original steps
// title instead.
// Here are a couple of before and after examples:
// git::https://github.com/bitrise-steplib/bitrise-step-simple-git-clone.git -> Simple Git Clone
// certificate-and-profile-installer@1 -> Certificate and profile installer
if stepInfoPtr.Step.Title != nil && (strings.HasPrefix(*stepInfoPtr.Step.Title, stepInfoPtr.ID) || strings.HasPrefix(*stepInfoPtr.Step.Title, stepInfoPtr.Library)) {
if mergedStep.Title != nil && *mergedStep.Title != "" {
*stepInfoPtr.Step.Title = *mergedStep.Title
}
}
//
// Run step
logStepStarted(stepInfoPtr, mergedStep, idx, stepExecutionID, stepStartTime)
if mergedStep.RunIf != nil && *mergedStep.RunIf != "" {
envList, err := tools.EnvmanReadEnvList(configs.InputEnvstorePath)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodePreparationFailed, 1, fmt.Errorf("EnvmanReadEnvList failed, err: %s", err),
isLastStep, false, map[string]string{}, stepStartedProperties)
continue
}
isRun, err := bitrise.EvaluateTemplateToBool(*mergedStep.RunIf, configs.IsCIMode, configs.IsPullRequestMode, buildRunResults, envList)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodePreparationFailed, 1, err, isLastStep, false, map[string]string{}, stepStartedProperties)
continue
}
if !isRun {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodeSkippedWithRunIf, 0, err, isLastStep, false, map[string]string{}, stepStartedProperties)
continue
}
}
isAlwaysRun := stepmanModels.DefaultIsAlwaysRun
if mergedStep.IsAlwaysRun != nil {
isAlwaysRun = *mergedStep.IsAlwaysRun
} else {
log.Warnf("Step (%s) mergedStep.IsAlwaysRun is nil, should not!", stepIDData.IDorURI)
}
if buildRunResults.IsBuildFailed() && !isAlwaysRun {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodeSkipped, 0, err, isLastStep, false, map[string]string{}, stepStartedProperties)
} else {
// beside of the envs coming from the current parent process these will be added as an extra
var additionalEnvironments []envmanModels.EnvironmentItemModel
// add this environment variable so all child processes can connect their events to their step lifecycle events
additionalEnvironments = append(additionalEnvironments, envmanModels.EnvironmentItemModel{
analytics.StepExecutionIDEnvKey: stepExecutionID,
})
// add an extra env for the next step run to be able to access the step's source location
additionalEnvironments = append(additionalEnvironments, envmanModels.EnvironmentItemModel{
"BITRISE_STEP_SOURCE_DIR": stepDir,
})
// ensure a new testDirPath and if created successfuly then attach it to the step process by and env
testDirPath, err := ioutil.TempDir(os.Getenv(configs.BitriseTestDeployDirEnvKey), "test_result")
if err != nil {
log.Errorf("Failed to create test result dir, error: %s", err)
}
if testDirPath != "" {
// managed to create the test dir, set the env for it for the next step run
additionalEnvironments = append(additionalEnvironments, envmanModels.EnvironmentItemModel{
configs.BitrisePerStepTestResultDirEnvKey: testDirPath,
})
}
environmentItemModels := append(*environments, additionalEnvironments...)
envSource := &env.DefaultEnvironmentSource{}
stepDeclaredEnvironments, expandedStepEnvironment, redactedInputsWithType, err := prepareStepEnvironment(prepareStepInputParams{
environment: environmentItemModels,
inputs: mergedStep.Inputs,
buildRunResults: buildRunResults,
isCIMode: configs.IsCIMode,
isPullRequestMode: configs.IsPullRequestMode,
}, envSource)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodePreparationFailed, 1,
fmt.Errorf("failed to prepare step environment variables: %s", err),
isLastStep, false, map[string]string{}, stepStartedProperties)
continue
}
stepSecrets := tools.GetSecretValues(secrets)
if configs.IsSecretEnvsFiltering {
sensitiveEnvs, err := getSensitiveEnvs(stepDeclaredEnvironments, expandedStepEnvironment)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodePreparationFailed, 1,
fmt.Errorf("failed to get sensitive inputs: %s", err),
isLastStep, false, map[string]string{}, stepStartedProperties)
continue
}
stepSecrets = append(stepSecrets, tools.GetSecretValues(sensitiveEnvs)...)
}
redactedStepInputs, redactedOriginalInputs, err := redactStepInputs(expandedStepEnvironment, mergedStep.Inputs, stepSecrets)
if err != nil {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodePreparationFailed, 1,
fmt.Errorf("failed to redact step inputs: %s", err),
isLastStep, false, map[string]string{}, stepStartedProperties)
continue
}
for key, value := range redactedStepInputs {
if _, ok := redactedInputsWithType[key]; !ok {
redactedInputsWithType[key] = value
}
}
tracker.SendStepStartedEvent(stepStartedProperties, prepareAnalyticsStepInfo(mergedStep, stepInfoPtr), redactedInputsWithType, redactedOriginalInputs)
exit, outEnvironments, err := runStep(stepExecutionID, mergedStep, stepIDData, stepDir, stepDeclaredEnvironments, stepSecrets)
if testDirPath != "" {
if err := addTestMetadata(testDirPath, models.TestResultStepInfo{Number: idx, Title: *mergedStep.Title, ID: stepIDData.IDorURI, Version: stepIDData.Version}); err != nil {
log.Errorf("Failed to normalize test result dir, error: %s", err)
}
}
if err := tools.EnvmanClear(configs.OutputEnvstorePath); err != nil {
log.Errorf("Failed to clear output envstore, error: %s", err)
}
*environments = append(*environments, outEnvironments...)
if err != nil {
if *mergedStep.IsSkippable {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodeFailedSkippable, exit, err, isLastStep, false, redactedStepInputs, stepIDProperties)
} else {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodeFailed, exit, err, isLastStep, false, redactedStepInputs, stepIDProperties)
}
} else {
runResultCollector.registerStepRunResults(&buildRunResults, stepExecutionID, stepStartTime, mergedStep, stepInfoPtr, stepIdxPtr,
*mergedStep.RunIf, models.StepRunStatusCodeSuccess, 0, nil, isLastStep, false, redactedStepInputs, stepIDProperties)
}
}
}
return buildRunResults
}
func logStepStarted(stepInfo stepmanModels.StepInfoModel, step stepmanModels.StepModel, idx int, stepExcutionId string, stepStartTime time.Time) {
title := ""
if stepInfo.Step.Title != nil && *stepInfo.Step.Title != "" {
title = *stepInfo.Step.Title
}
params := log.StepStartedParams{
ExecutionId: stepExcutionId,
Position: idx,
Title: title,
Id: stepInfo.ID,
Version: stepInfo.Version,
Collection: stepInfo.Library,
Toolkit: toolkits.ToolkitForStep(step).ToolkitName(),
StartTime: stepStartTime.Format(time.RFC3339),
}
log.PrintStepStartedEvent(params)
}
func prepareAnalyticsStepInfo(step stepmanModels.StepModel, stepInfoPtr stepmanModels.StepInfoModel) analytics.StepInfo {
return analytics.StepInfo{
StepID: stepInfoPtr.ID,
StepTitle: pointers.StringWithDefault(step.Title, ""),
StepVersion: stepInfoPtr.Version,
StepSource: pointers.StringWithDefault(step.SourceCodeURL, ""),
Skippable: pointers.BoolWithDefault(step.IsSkippable, false),
}
}
func runWorkflow(
plan models.WorkflowExecutionPlan,
workflowID string,
workflow models.WorkflowModel,
steplibSource string,
buildRunResults models.BuildRunResultsModel,
environments *[]envmanModels.EnvironmentItemModel, secrets []envmanModels.EnvironmentItemModel,
isLastWorkflow bool, tracker analytics.Tracker, buildIDProperties coreanalytics.Properties) models.BuildRunResultsModel {
workflowIDProperties := coreanalytics.Properties{analytics.WorkflowExecutionID: plan.UUID}
bitrise.PrintRunningWorkflow(workflow.Title)
tracker.SendWorkflowStarted(buildIDProperties.Merge(workflowIDProperties), workflowID, workflow.Title)
*environments = append(*environments, workflow.Environments...)
results := activateAndRunSteps(plan, workflow, steplibSource, buildRunResults, environments, secrets, isLastWorkflow, tracker, workflowIDProperties)
tracker.SendWorkflowFinished(workflowIDProperties, results.IsBuildFailed())
return results
}
func addTestMetadata(testDirPath string, testResultStepInfo models.TestResultStepInfo) error {
// check if the test dir is empty
if empty, err := isDirEmpty(testDirPath); err != nil {
return fmt.Errorf("failed to check if dir empty: %s, error: %s", testDirPath, err)
} else if empty {
// if the test dir is empty then we need to remove the dir from the temp location to not to spam the system with empty dirs
if err := os.Remove(testDirPath); err != nil {
return fmt.Errorf("failed to remove dir: %s, error: %s", testDirPath, err)
}
} else {
// if the step put files into the test dir(so it is used) then we won't need to remove the test dir, moreover we need to add extra info from the step parameters
stepInfoFilePath := filepath.Join(testDirPath, "step-info.json")
stepResultInfoFile, err := os.Create(stepInfoFilePath)
if err != nil {
return fmt.Errorf("failed to open file: %s, error: %s", stepInfoFilePath, err)
}
if err := json.NewEncoder(stepResultInfoFile).Encode(testResultStepInfo); err != nil {
return fmt.Errorf("failed to encode to JSON, error: %s", err)
}
}
return nil
}