forked from harness-community/drone-s3-upload-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (108 loc) · 3.5 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var (
pluginVersion = "1.0.0"
)
func main() {
app := cli.NewApp()
app.Name = "drone-s3-upload-publish"
app.Usage = "Drone plugin to upload file/directories to AWS S3 Bucket and display the bucket url under 'Executions > Artifacts' tab"
app.Action = run
app.Version = pluginVersion
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "aws-access-key",
Usage: "AWS Access Key ID",
EnvVar: "PLUGIN_AWS_ACCESS_KEY_ID",
},
cli.StringFlag{
Name: "aws-secret-key",
Usage: "AWS Secret Access Key",
EnvVar: "PLUGIN_AWS_SECRET_ACCESS_KEY",
},
cli.StringFlag{
Name: "aws-default-region",
Usage: "AWS Default Region",
EnvVar: "PLUGIN_AWS_DEFAULT_REGION",
},
cli.StringFlag{
Name: "aws-bucket",
Usage: "AWS S3 Bucket",
EnvVar: "PLUGIN_AWS_BUCKET",
},
cli.StringFlag{
Name: "source",
Usage: "Source",
EnvVar: "PLUGIN_SOURCE",
},
cli.StringFlag{
Name: "target-path",
Usage: "target",
EnvVar: "PLUGIN_TARGET",
},
cli.StringFlag{
Name: "artifact-file",
Usage: "Artifact file",
EnvVar: "PLUGIN_ARTIFACT_FILE",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
awsAccessKey := c.String("aws-access-key")
awsSecretKey := c.String("aws-secret-key")
awsDefaultRegion := c.String("aws-default-region")
awsBucket := c.String("aws-bucket")
source := c.String("source")
target := c.String("target-path")
newFolder := filepath.Base(source)
artifactFilePath := c.String("artifact-file")
var urls string
if strings.ContainsAny(source, "*") {
log.Fatal("Glob pattern not allowed!")
}
fileType, err := os.Stat(source)
if err != nil {
log.Fatal(err)
}
// AWS config commands to set ACCESS_KEY_ID and SECRET_ACCESS_KEY
exec.Command("aws", "configure", "set", "aws_access_key_id", awsAccessKey).Run()
exec.Command("aws", "configure", "set", "aws_secret_access_key", awsSecretKey).Run()
var Uploadcmd *exec.Cmd
if fileType.IsDir() {
if target != "" {
Uploadcmd = exec.Command("aws", "s3", "cp", source, "s3://"+awsBucket+"/"+target+"/"+newFolder, "--region", awsDefaultRegion, "--recursive")
urls = "https://s3.console.aws.amazon.com/s3/buckets/" + awsBucket + "?region=" + awsDefaultRegion + "&prefix=" + target + "/" + newFolder + "/&showversions=false"
} else {
Uploadcmd = exec.Command("aws", "s3", "cp", source, "s3://"+awsBucket+"/"+newFolder+"/", "--region", awsDefaultRegion, "--recursive")
urls = "https://s3.console.aws.amazon.com/s3/buckets/" + awsBucket + "?region=" + awsDefaultRegion + "&prefix=" + newFolder + "/&showversions=false"
}
} else {
if target != "" {
Uploadcmd = exec.Command("aws", "s3", "cp", source, "s3://"+awsBucket+"/"+target+"/", "--region", awsDefaultRegion)
urls = "https://s3.console.aws.amazon.com/s3/object/" + awsBucket + "?region=" + awsDefaultRegion + "&prefix=" + target + "/" + newFolder
} else {
Uploadcmd = exec.Command("aws", "s3", "cp", source, "s3://"+awsBucket+"/", "--region", awsDefaultRegion)
urls = "https://s3.console.aws.amazon.com/s3/object/" + awsBucket + "?region=" + awsDefaultRegion + "&prefix=" + newFolder
}
}
out, err := Uploadcmd.Output()
if err != nil {
return err
}
fmt.Printf("Output: %s\n", out)
// End of S3 upload operation
files := make([]File, 0)
files = append(files, File{Name: artifactFilePath, URL: urls})
return writeArtifactFile(files, artifactFilePath)
}