Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bucket object --folder option. #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ _test
*.[568vq]
[568vq].out

# IDE settings
.idea/

*.cgo1.go
*.cgo2.c
_cgo_defun.c
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,55 @@ docker run --rm \
-w $(pwd) \
plugins/gcs
```

#### Optional object folder setting

The object folder can be specified as part of the `PLUGIN_TARGET` or as part of the `PLUGIN_FOLDER` setting. The value
of the `PLUGIN_FOLDER` setting will be appended to any value supplied as part of the `PLUGIN_TARGET`.

```console
docker run --rm \
-e PLUGIN_SOURCE="dist" \
-e PLUGIN_TARGET="bucket/dir/" \
-e PLUGIN_FOLDER="${DRONE_PULL_REQUEST}" \
-e PLUGIN_IGNORE="bin/*" \
-e PLUGIN_ACL="allUsers:READER,[email protected]:OWNER" \
-e PLUGIN_GZIP="js,css,html" \
-e PLUGIN_CACHE_CONTROL="public,max-age=3600" \
-e PLUGIN_METADATA='{"x-goog-meta-foo":"bar"}' \
-v $(pwd):$(pwd) \
-w $(pwd) \
plugins/gcs
```

One potential use case for specifying the `PLUGIN_FOLDER` separately is when storing the value for `PLUGIN_TARGET` as a
drone secret while needing to generate a final object folder value during CI. The above example illustrates how it may
be possible to publish to `bucket/dir/DRONE_PULL_REQUEST`.

Perhaps a more illustrative example:

```yaml
kind: pipeline
name: upload prs

trigger:
branch:
include:
- develop
event:
- pull_request

steps:
- name: upload to cloud storage prs bucket
image: plugins/gcs
settings:
source: build
target:
from_secret: prs_bucket
folder: "prs/${DRONE_PULL_REQUEST}"
gzip: js,css,html
token:
from_secret: prs_token
```

The above drone configuration file is not meant to be complete.
17 changes: 10 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ go 1.15

require (
cloud.google.com/go v0.21.0
github.com/golang/protobuf v1.0.0
github.com/googleapis/gax-go v2.0.0+incompatible
github.com/golang/glog v0.0.0-20210429001901-424d2337a529 // indirect
github.com/golang/protobuf v1.0.0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/googleapis/gax-go v2.0.0+incompatible // indirect
github.com/pkg/errors v0.8.0
github.com/urfave/cli v1.20.0
go.opencensus.io v0.8.0
go.opencensus.io v0.8.0 // indirect
golang.org/x/net v0.0.0-20180415214307-500e7a4f953d
golang.org/x/oauth2 v0.0.0-20180402223937-921ae394b943
golang.org/x/text v0.3.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/text v0.3.0 // indirect
google.golang.org/api v0.0.0-20180413000347-fca24fcb4112
google.golang.org/appengine v1.0.0
google.golang.org/genproto v0.0.0-20180413175816-7fd901a49ba6
google.golang.org/grpc v1.11.3
google.golang.org/appengine v1.0.0 // indirect
google.golang.org/genproto v0.0.0-20180413175816-7fd901a49ba6 // indirect
google.golang.org/grpc v1.11.3 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
cloud.google.com/go v0.21.0 h1:083TLU7hqb1CzKeMhzDsCTP4uXkBGr6nnWuaHdVNBCI=
cloud.google.com/go v0.21.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/golang/glog v0.0.0-20210429001901-424d2337a529 h1:2voWjNECnrZRbfwXxHB1/j8wa6xdKn85B5NzgVL/pTU=
github.com/golang/glog v0.0.0-20210429001901-424d2337a529/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/protobuf v1.0.0 h1:lsek0oXi8iFE9L+EXARyHIjU5rlWIhhTkjDz3vHhWWQ=
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/googleapis/gax-go v2.0.0+incompatible h1:j0GKcs05QVmm7yesiZq2+9cxHkNK9YM6zKx4D2qucQU=
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
Expand All @@ -14,10 +18,15 @@ golang.org/x/net v0.0.0-20180415214307-500e7a4f953d h1:YbPBOQdSo5fFDiZgBDcltE4Xj
golang.org/x/net v0.0.0-20180415214307-500e7a4f953d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20180402223937-921ae394b943 h1:hE+k6oRG1aru6/y8A0HI01Zqp65EZcUcKToTkbPsFFM=
golang.org/x/oauth2 v0.0.0-20180402223937-921ae394b943/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.0.0-20180413000347-fca24fcb4112 h1:ZwIBv243B9oUEb2HVMTQs//+WVhsL5hsUoCAu1ydobs=
google.golang.org/api v0.0.0-20180413000347-fca24fcb4112/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/appengine v1.0.0 h1:dN4LljjBKVChsv0XCSI+zbyzdqrkEwX5LQFUMRSGqOc=
google.golang.org/appengine v1.0.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180413175816-7fd901a49ba6 h1:VrRtqEIrO5wUzNwL/A2WTNUtDuAtvb3KPK3OrUriLqI=
google.golang.org/genproto v0.0.0-20180413175816-7fd901a49ba6/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ func main() {
},
cli.StringFlag{
Name: "target",
Usage: "destination to copy files to, including bucket name",
Usage: "destination bucket to copy files to, can include folder path",
EnvVar: "PLUGIN_TARGET",
},
cli.StringFlag{
Name: "folder",
Usage: "destination folder path to copy files to, will be appended to target",
EnvVar: "PLUGIN_FOLDER",
},
cli.StringSliceFlag{
Name: "gzip",
Usage: `files with the specified extensions will be gzipped and uploaded with "gzip" Content-Encoding header`,
Expand Down Expand Up @@ -84,6 +89,7 @@ func run(c *cli.Context) error {
ACL: c.StringSlice("acl"),
Source: c.String("source"),
Target: c.String("target"),
Folder: c.String("folder"),
Ignore: c.String("ignore"),
Gzip: c.StringSlice("gzip"),
CacheControl: c.String("cache-control"),
Expand Down
34 changes: 26 additions & 8 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ type (
// Copies the files from the specified directory.
Source string

// Destination to copy files to, including bucket name
// Destination bucket to copy files to, can include folder path
Target string

// Destination folder path to copy files to, will be appended to target
Folder string

// Exclude files matching this pattern.
Ignore string

Expand Down Expand Up @@ -113,11 +116,11 @@ func (p *Plugin) Exec(client *storage.Client) error {
rel, err := filepath.Rel(p.Config.Source, f)

if err != nil {
res <- &result{f, err}
res <- &result{rel, err}
return
}

err = p.uploadFile(path.Join(p.Config.Target, rel), f)
err = p.uploadFile(f)
res <- &result{rel, err}

<-buf // free up
Expand Down Expand Up @@ -146,23 +149,38 @@ func (p *Plugin) errorf(format string, args ...interface{}) {
p.printf(format, args...)
}

// uploadFile uploads the file to dst using global bucket.
// objectFolderPath returns a bucket object folder name based on plugin target and folder values.
// It is intended that the bucket name has already been stripped from the target.
func (p *Plugin) objectFolderPath() string {
return path.Join(p.Config.Target, p.Config.Folder)
}

// objectFolderPathForFile returns a bucket object name including a resolved object folder path.
func (p *Plugin) objectFolderPathForFile(file string) (string, error) {
rel, err := filepath.Rel(p.Config.Source, file)

if err != nil {
return "", err
}

return path.Join(p.objectFolderPath(), rel), nil
}

// uploadFile uploads the file, relative to plugin source directory, to dst using global bucket.
// To get a more robust upload use retryUpload instead.
func (p *Plugin) uploadFile(dst, file string) error {
func (p *Plugin) uploadFile(file string) error {
r, gz, err := p.gzipper(file)

if err != nil {
return err
}

defer r.Close()
rel, err := filepath.Rel(p.Config.Source, file)

name, err := p.objectFolderPathForFile(file)
if err != nil {
return err
}

name := path.Join(p.Config.Target, rel)
w := p.bucket.Object(name).NewWriter(context.Background())
w.CacheControl = p.Config.CacheControl
w.Metadata = p.Config.Metadata
Expand Down
Loading