forked from bitrise-steplib/bitrise-step-build-router-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (141 loc) · 5.13 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-steplib/bitrise-step-build-router-start/bitrise"
)
const envBuildSlugs = "ROUTER_STARTED_BUILD_SLUGS"
// Config ...
type Config struct {
AppSlug string `env:"BITRISE_APP_SLUG,required"`
BuildSlug string `env:"BITRISE_BUILD_SLUG,required"`
BuildNumber string `env:"BITRISE_BUILD_NUMBER,required"`
AccessToken stepconf.Secret `env:"access_token,required"`
WaitForBuilds string `env:"wait_for_builds"`
BuildArtifactsSavePath string `env:"build_artifacts_save_path"`
AbortBuildsOnFail string `env:"abort_on_fail"`
Workflows string `env:"workflows,required"`
Environments string `env:"environment_key_list"`
IsVerboseLog bool `env:"verbose,required"`
}
func failf(s string, a ...interface{}) {
log.Errorf(s, a...)
os.Exit(1)
}
func main() {
var cfg Config
if err := stepconf.Parse(&cfg); err != nil {
failf("Issue with an input: %s", err)
}
stepconf.Print(cfg)
fmt.Println()
log.SetEnableDebugLog(cfg.IsVerboseLog)
app := bitrise.NewAppWithDefaultURL(cfg.AppSlug, string(cfg.AccessToken))
build, err := app.GetBuild(cfg.BuildSlug)
if err != nil {
failf("failed to get build, error: %s", err)
}
log.Infof("Starting builds:")
var buildSlugs []string
environments := createEnvs(cfg.Environments)
for _, wf := range strings.Split(strings.TrimSpace(cfg.Workflows), "\n") {
wf = strings.TrimSpace(wf)
startedBuild, err := app.StartBuild(wf, build.OriginalBuildParams, cfg.BuildNumber, environments)
if err != nil {
log.Warnf("Failed to start build, error: %s", err)
}
if startedBuild.BuildSlug == "" {
log.Warnf("Build was not started. This could mean that manual build approval is enabled for this project and it's blocking this step from starting builds.")
} else {
buildSlugs = append(buildSlugs, startedBuild.BuildSlug)
log.Printf("- %s started (https://app.bitrise.io/build/%s)", startedBuild.TriggeredWorkflow, startedBuild.BuildSlug)
}
}
if err := tools.ExportEnvironmentWithEnvman(envBuildSlugs, strings.Join(buildSlugs, "\n")); err != nil {
failf("Failed to export environment variable, error: %s", err)
}
if cfg.WaitForBuilds != "true" {
return
}
fmt.Println()
log.Infof("Waiting for builds:")
if err := app.WaitForBuilds(buildSlugs, func(build bitrise.Build) {
var failReason string
switch build.Status {
case 0:
log.Printf("- %s %s", build.TriggeredWorkflow, build.StatusText)
case 1:
log.Donef("- %s successful", build.TriggeredWorkflow)
case 2:
log.Errorf("- %s failed", build.TriggeredWorkflow)
failReason = "failed"
case 3:
log.Warnf("- %s aborted", build.TriggeredWorkflow)
failReason = "aborted"
case 4:
log.Infof("- %s cancelled", build.TriggeredWorkflow)
failReason = "cancelled"
}
if cfg.AbortBuildsOnFail == "yes" && build.Status > 1 {
for _, buildSlug := range buildSlugs {
if buildSlug != build.Slug {
abortErr := app.AbortBuild(buildSlug, "Abort on Fail - Build [https://app.bitrise.io/build/"+build.Slug+"] "+failReason+"\nAuto aborted by parent build")
if abortErr != nil {
log.Warnf("failed to abort build, error: %s", abortErr)
}
log.Donef("Build " + buildSlug + " aborted due to associated build failure")
}
}
}
if build.Status != 0 {
buildArtifactSaveDir := strings.TrimSpace(cfg.BuildArtifactsSavePath)
if buildArtifactSaveDir != "" {
artifactsResponse, err := build.GetBuildArtifacts(app)
if err != nil {
log.Warnf("failed to get build artifacts: %s", err)
}
for _, artifactSlug := range artifactsResponse.ArtifactSlugs {
artifactObj, err := build.GetBuildArtifact(app, artifactSlug.ArtifactSlug)
if err != nil {
log.Warnf("failed to get build artifact: %s", err)
continue
}
if err = os.MkdirAll(buildArtifactSaveDir, 0777); err != nil {
log.Warnf("failed to ensure artifact path %s exists: %s", buildArtifactSaveDir, err)
continue
}
fullBuildArtifactsSavePath := filepath.Join(buildArtifactSaveDir, artifactObj.Artifact.Title)
downloadErr := artifactObj.Artifact.DownloadArtifact(fullBuildArtifactsSavePath)
if downloadErr != nil {
log.Warnf("failed to download %s artifact: %s", artifactObj.Artifact.Title, downloadErr)
} else {
log.Donef("Downloaded %s to %s", artifactObj.Artifact.Title, fullBuildArtifactsSavePath)
}
}
}
}
}); err != nil {
failf("An error occurred: %s", err)
}
}
func createEnvs(environmentKeys string) []bitrise.Environment {
environmentKeys = strings.Replace(environmentKeys, "$", "", -1)
environmentsKeyList := strings.Split(environmentKeys, "\n")
var environments []bitrise.Environment
for _, key := range environmentsKeyList {
if key == "" {
continue
}
env := bitrise.Environment{
MappedTo: key,
Value: os.Getenv(key),
}
environments = append(environments, env)
}
return environments
}