-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
98 lines (78 loc) · 2.31 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
package main
import (
"fmt"
"log"
"os"
"github.com/alephao/cacheutil"
"github.com/mholt/archiver"
)
const (
BITRISE_GIT_BRANCH = "BITRISE_GIT_BRANCH"
BITRISE_OSX_STACK_REV_ID = "BITRISE_OSX_STACK_REV_ID"
CACHE_AWS_ACCESS_KEY_ID = "cache_aws_access_key_id"
CACHE_AWS_SECRET_ACCESS_KEY = "cache_aws_secret_access_key"
CACHE_AWS_REGION = "cache_aws_region"
CACHE_BUCKET_NAME = "cache_bucket_name"
CACHE_KEY = "cache_key"
CACHE_PATH = "cache_path"
)
func generateBucketKey(cacheKey string) (string, error) {
branch := os.Getenv(BITRISE_GIT_BRANCH)
stackrev := os.Getenv(BITRISE_OSX_STACK_REV_ID)
functionExecuter := cacheutil.NewCacheKeyFunctionExecuter(branch, stackrev)
keyParser := cacheutil.NewKeyParser(&functionExecuter)
return keyParser.Parse(cacheKey)
}
func main() {
awsAccessKeyId := GetEnvOrExit(CACHE_AWS_ACCESS_KEY_ID)
awsSecretAccessKey := GetEnvOrExit(CACHE_AWS_SECRET_ACCESS_KEY)
awsRegion := GetEnvOrExit(CACHE_AWS_REGION)
bucketName := GetEnvOrExit(CACHE_BUCKET_NAME)
cacheKey := GetEnvOrExit(CACHE_KEY)
cachePath := GetEnvOrExit(CACHE_PATH)
failed := false
CreateTempFolder(func(tempFolderPath string) {
s3 := NewAwsS3(
awsRegion,
awsAccessKeyId,
awsSecretAccessKey,
bucketName,
)
bucketKey, err := generateBucketKey(cacheKey)
if err != nil {
log.Printf("Failed to parse cache key '%s'\n", cacheKey)
log.Printf("Error: %s\n", err.Error())
failed = true
return
}
log.Printf("Checking if cache exists for key '%s'\n", bucketKey)
cacheExists := s3.CacheExists(bucketKey)
if cacheExists {
log.Println("Cache found! Skiping...")
return
}
log.Println("Cache not found, trying to compress the folder.")
outputPath := fmt.Sprintf("%s/%s.zip", tempFolderPath, bucketKey)
err = archiver.Archive([]string{cachePath}, outputPath)
if err != nil {
log.Printf("Failed to compress '%s'\n", cachePath)
log.Printf("Error: %s\n", err.Error())
failed = true
return
}
log.Println("Compression was successful, trying to upload to aws.")
err = s3.UploadToAws(
bucketKey,
outputPath,
)
if err != nil {
log.Printf("Failed to upload! Failing gracefully. Error: %s\n", err)
return
}
log.Println("Upload was successful!")
})
if failed {
os.Exit(1)
}
os.Exit(0)
}