forked from bitrise-steplib/steps-deploy-to-bitrise-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
265 lines (216 loc) · 7.9 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
package main
import (
"bytes"
"fmt"
"html/template"
"os"
"path/filepath"
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/test"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/ziputil"
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/uploaders"
)
var fileBaseNamesToSkip = []string{".DS_Store"}
// Config ...
type Config struct {
BuildURL string `env:"build_url,required"`
APIToken string `env:"build_api_token,required"`
IsCompress string `env:"is_compress,opt[true,false]"`
ZipName string `env:"zip_name"`
DeployPath string `env:"deploy_path,required"`
NotifyUserGroups string `env:"notify_user_groups"`
NotifyEmailList string `env:"notify_email_list"`
IsPublicPageEnabled string `env:"is_enable_public_page,opt[true,false]"`
PublicInstallPageMapFormat string `env:"public_install_page_url_map_format,required"`
BuildSlug string `env:"BITRISE_BUILD_SLUG,required"`
TestDeployDir string `env:"BITRISE_TEST_DEPLOY_DIR,required"`
AppSlug string `env:"BITRISE_APP_SLUG,required"`
AddonAPIBaseURL string `env:"addon_api_base_url,required"`
AddonAPIToken string `env:"addon_api_token"`
DebugMode bool `env:"debug_mode,opt[true,false]"`
}
// PublicInstallPage ...
type PublicInstallPage struct {
File string
URL string
}
func fail(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
func main() {
var config Config
if err := stepconf.Parse(&config); err != nil {
fail("Issue with input: %s", err)
}
if err := validateGoTemplate(config.PublicInstallPageMapFormat); err != nil {
fail("PublicInstallPageMapFormat - %s", err)
}
stepconf.Print(config)
fmt.Println()
log.SetEnableDebugLog(config.DebugMode)
absDeployPth, err := pathutil.AbsPath(config.DeployPath)
if err != nil {
fail("Failed to expand path: %s, error: %s", config.DeployPath, err)
}
filesToDeploy := []string{}
tmpDir, err := pathutil.NormalizedOSTempDirPath("__deploy-to-bitrise-io__")
if err != nil {
fail("Failed to create tmp dir, error: %s", err)
}
// Collect files to deploy
isDeployPathDir, err := pathutil.IsDirExists(absDeployPth)
if err != nil {
fail("Failed to check if DeployPath (%s) is a directory or a file, error: %s", absDeployPth, err)
}
if !isDeployPathDir {
fmt.Println()
log.Infof("Deploying single file")
filesToDeploy = []string{absDeployPth}
} else if config.IsCompress == "true" {
fmt.Println()
log.Infof("Deploying compressed Deploy directory")
zipName := filepath.Base(absDeployPth)
if config.ZipName != "" {
zipName = config.ZipName
}
tmpZipPath := filepath.Join(tmpDir, zipName+".zip")
if err := ziputil.ZipDir(absDeployPth, tmpZipPath, true); err != nil {
fail("Failed to zip output dir, error: %s", err)
}
filesToDeploy = []string{tmpZipPath}
} else {
fmt.Println()
log.Infof("Deploying the content of the Deploy directory separately")
pattern := filepath.Join(absDeployPth, "*")
pths, err := filepath.Glob(pattern)
if err != nil {
fail("Failed to list files in DeployPath, error: %s", err)
}
for _, pth := range pths {
if isDir, err := pathutil.IsDirExists(pth); err != nil {
fail("Failed to check if path (%s) is a directory or a file, error: %s", pth, err)
} else if !isDir {
filesToDeploy = append(filesToDeploy, pth)
}
}
}
clearedFilesToDeploy := []string{}
for _, pth := range filesToDeploy {
for _, fileBaseNameToSkip := range fileBaseNamesToSkip {
if filepath.Base(pth) == fileBaseNameToSkip {
log.Warnf("skipping: %s", pth)
} else {
clearedFilesToDeploy = append(clearedFilesToDeploy, pth)
}
}
}
fmt.Println()
log.Infof("List of files to deploy")
for _, pth := range clearedFilesToDeploy {
log.Printf("- %s", pth)
}
// ---
// Deploy files
fmt.Println()
log.Infof("Deploying files")
publicInstallPages := make(map[string]string)
for _, pth := range clearedFilesToDeploy {
ext := filepath.Ext(pth)
fmt.Println()
switch ext {
case ".ipa":
log.Donef("Uploading ipa file: %s", pth)
installPage, err := uploaders.DeployIPA(pth, config.BuildURL, config.APIToken, config.NotifyUserGroups, config.NotifyEmailList, config.IsPublicPageEnabled)
if err != nil {
fail("Deploy failed, error: %s", err)
}
if installPage != "" {
publicInstallPages[filepath.Base(pth)] = installPage
}
case ".apk":
log.Donef("Uploading apk file: %s", pth)
installPage, err := uploaders.DeployAPK(pth, config.BuildURL, config.APIToken, config.NotifyUserGroups, config.NotifyEmailList, config.IsPublicPageEnabled)
if err != nil {
fail("Deploy failed, error: %s", err)
}
if installPage != "" {
publicInstallPages[filepath.Base(pth)] = installPage
}
case ".aab":
log.Donef("Uploading aab file: %s", pth)
installPage, err := uploaders.DeployAAB(pth, config.BuildURL, config.APIToken, config.NotifyUserGroups, config.NotifyEmailList, config.IsPublicPageEnabled)
if err != nil {
fail("Deploy failed, error: %s", err)
}
if installPage != "" {
publicInstallPages[filepath.Base(pth)] = installPage
}
default:
log.Donef("Uploading file: %s", pth)
installPage, err := uploaders.DeployFile(pth, config.BuildURL, config.APIToken, config.NotifyUserGroups, config.NotifyEmailList, config.IsPublicPageEnabled)
if err != nil {
fail("Deploy failed, error: %s", err)
}
if installPage != "" {
publicInstallPages[filepath.Base(pth)] = installPage
} else if config.IsPublicPageEnabled == "true" {
log.Warnf("is_enable_public_page is set, but public download isn't allowed for this type of file")
}
}
}
fmt.Println()
log.Donef("Success")
log.Printf("You can find the Artifact on Bitrise, on the Build's page: %s", config.BuildURL)
if len(publicInstallPages) > 0 {
temp := template.New("Public Install Page template")
var pages []PublicInstallPage
for file, url := range publicInstallPages {
pages = append(pages, PublicInstallPage{
File: file,
URL: url,
})
}
if err := tools.ExportEnvironmentWithEnvman("BITRISE_PUBLIC_INSTALL_PAGE_URL", pages[0].URL); err != nil {
fail("Failed to export BITRISE_PUBLIC_INSTALL_PAGE_URL, error: %s", err)
}
log.Printf("The public install page url is now available in the Environment Variable: BITRISE_PUBLIC_INSTALL_PAGE_URL (value: %s)\n", pages[0].URL)
temp, err := temp.Parse(config.PublicInstallPageMapFormat)
if err != nil {
fail("Error during parsing PublicInstallPageMap: ", err)
}
buf := new(bytes.Buffer)
if err := temp.Execute(buf, pages); err != nil {
fail("Execute: ", err)
}
if err := tools.ExportEnvironmentWithEnvman("BITRISE_PUBLIC_INSTALL_PAGE_URL_MAP", buf.String()); err != nil {
fail("Failed to export BITRISE_PUBLIC_INSTALL_PAGE_URL_MAP, error: %s", err)
}
log.Printf("A map of deployed files and their public install page urls is now available in the Environment Variable: BITRISE_PUBLIC_INSTALL_PAGE_URL_MAP (value: %s)", buf.String())
log.Printf("")
}
// Deploy test files
if config.AddonAPIToken != "" {
fmt.Println()
log.Infof("Upload test results")
testResults, err := test.ParseTestResults(config.TestDeployDir)
if err != nil {
log.Warnf("Error during parsing test results: ", err)
} else {
log.Printf("- uploading (%d) test results", len(testResults))
if err := testResults.Upload(config.AddonAPIToken, config.AddonAPIBaseURL, config.AppSlug, config.BuildSlug); err != nil {
log.Warnf("Failed to upload test results: ", err)
} else {
log.Donef("Success")
}
}
}
}
func validateGoTemplate(publicInstallPageMapFormat string) error {
temp := template.New("Public Install Page Map template")
_, err := temp.Parse(publicInstallPageMapFormat)
return err
}