-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.go
112 lines (95 loc) · 2.8 KB
/
program.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
package main
import (
"fmt"
"log"
"os"
"strings"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
storage "google.golang.org/api/storage/v1"
)
func fatalf(service *storage.Service, errorMessage string, args ...interface{}) {
log.Fatalf("Dying with error:\n"+errorMessage, args...)
}
func main() {
email := os.Getenv("GCS_EMAIL")
if email == "" {
log.Fatalf("$GCS_EMAIL is not provided!")
os.Exit(1)
}
privateKey := os.Getenv("GCS_PRIVATE_KEY")
if privateKey == "" {
log.Fatalf("$GCS_PRIVATE_KEY is not provided!")
os.Exit(1)
}
privateKey = strings.Replace(privateKey, "\\n", "\n", -1)
bucketName := os.Getenv("GCS_BUCKET")
if bucketName == "" {
log.Fatalf("$GCS_BUCKET is not provided!")
os.Exit(1)
}
projectID := os.Getenv("GCS_PROJECT_ID")
if projectID == "" {
log.Fatalf("$GCS_PROJECT_ID is not provided!")
os.Exit(1)
}
targetFolder := os.Getenv("GCS_FOLDER")
if targetFolder == "" {
log.Fatalf("$GCS_FOLDER is not provided!")
os.Exit(1)
}
fileName := os.Getenv("GCS_FILENAME")
if fileName == "" {
log.Fatalf("$GCS_FILENAME is not provided!")
os.Exit(1)
}
ipaPath := os.Getenv("BITRISE_IPA_PATH")
if ipaPath == "" {
log.Fatalf("$BITRISE_IPA_PATH is not provided!")
os.Exit(1)
}
branch := os.Getenv("BITRISE_GIT_BRANCH")
if branch == "" {
log.Fatalf("$BITRISE_GIT_BRANCH is not provided!")
os.Exit(1)
}
conf := &jwt.Config{
Email: email,
PrivateKey: []byte(privateKey),
Scopes: []string{
"https://www.googleapis.com/auth/devstorage.read_write",
},
TokenURL: google.JWTTokenURL,
}
client := conf.Client(oauth2.NoContext)
// if err != nil {
// log.Fatalf("Unable to get default client: %v", err)
// }
service, err := storage.New(client)
if err != nil {
log.Fatalf("Unable to create storage service: %v", err)
}
// If the bucket already exists and the user has access, warn the user, but don't try to create it.
if _, err := service.Buckets.Get(bucketName).Do(); err == nil {
fmt.Printf("Bucket %s already exists - skipping buckets.insert call.", bucketName)
} else {
// Create a bucket.
if res, err := service.Buckets.Insert(projectID, &storage.Bucket{Name: bucketName}).Do(); err == nil {
fmt.Printf("Created bucket %v at location %v\n\n", res.Name, res.SelfLink)
} else {
fatalf(service, "Failed creating bucket %s: %v", bucketName, err)
}
}
// Insert an object into a bucket.
object := &storage.Object{Name: fmt.Sprintf("%v/%v/%s.ipa", targetFolder, branch, fileName)}
file, err := os.Open(ipaPath)
if err != nil {
fatalf(service, "Error opening %q: %v", ipaPath, err)
}
if res, err := service.Objects.Insert(bucketName, object).Media(file).Do(); err == nil {
fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
} else {
fatalf(service, "Objects.Insert failed: %v", err)
}
}