1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00

feat: added blob.region and blob.disableSSL (#1279)

* feat: added blob.region and blob.disableSSL

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* fix: simplified url

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2019-12-27 14:18:41 -03:00 committed by GitHub
parent 60f4fe7d2d
commit 05116ff8e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 14 deletions

View File

@ -36,6 +36,7 @@ func TestMinioUpload(t *testing.T) {
{ {
Provider: "s3", Provider: "s3",
Bucket: "test", Bucket: "test",
Region: "us-east",
Endpoint: "http://" + listen, Endpoint: "http://" + listen,
IDs: []string{"foo", "bar"}, IDs: []string{"foo", "bar"},
}, },

View File

@ -241,6 +241,65 @@ func TestPipe_Publish(t *testing.T) {
} }
} }
func TestURL(t *testing.T) {
var buck = Bucket{}
t.Run("s3 with opts", func(t *testing.T) {
url, err := buck.url(context.New(config.Project{}), config.Blob{
Bucket: "foo",
Provider: "s3",
Region: "us-west-1",
Folder: "foo",
Endpoint: "s3.foobar.com",
DisableSSL: true,
})
require.NoError(t, err)
require.Equal(t, "s3://foo?disableSSL=true&endpoint=s3.foobar.com&region=us-west-1&s3ForcePathStyle=true", url)
})
t.Run("s3 with some opts", func(t *testing.T) {
url, err := buck.url(context.New(config.Project{}), config.Blob{
Bucket: "foo",
Provider: "s3",
Region: "us-west-1",
DisableSSL: true,
})
require.NoError(t, err)
require.Equal(t, "s3://foo?disableSSL=true&region=us-west-1", url)
})
t.Run("gs with opts", func(t *testing.T) {
url, err := buck.url(context.New(config.Project{}), config.Blob{
Bucket: "foo",
Provider: "gs",
Region: "us-west-1",
Folder: "foo",
Endpoint: "s3.foobar.com",
DisableSSL: true,
})
require.NoError(t, err)
require.Equal(t, "gs://foo", url)
})
t.Run("s3 no opts", func(t *testing.T) {
url, err := buck.url(context.New(config.Project{}), config.Blob{
Bucket: "foo",
Provider: "s3",
})
require.NoError(t, err)
require.Equal(t, "s3://foo", url)
})
t.Run("gs no opts", func(t *testing.T) {
url, err := buck.url(context.New(config.Project{}), config.Blob{
Bucket: "foo",
Provider: "gs",
})
require.NoError(t, err)
require.Equal(t, "gs://foo", url)
})
}
func setEnv(env map[string]string) { func setEnv(env map[string]string) {
for k, v := range env { for k, v := range env {
os.Setenv(k, v) os.Setenv(k, v)

View File

@ -3,6 +3,7 @@ package blob
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/url"
"path/filepath" "path/filepath"
"github.com/apex/log" "github.com/apex/log"
@ -51,22 +52,48 @@ func (b Bucket) Connect(ctx *context.Context, bucketURL string) (*blob.Bucket, e
return conn, nil return conn, nil
} }
func (b Bucket) url(ctx *context.Context, conf config.Blob) (string, error) {
bucket, err := tmpl.New(ctx).Apply(conf.Bucket)
if err != nil {
return "", err
}
bucketURL := fmt.Sprintf("%s://%s", conf.Provider, bucket)
if conf.Provider != "s3" {
return bucketURL, nil
}
var query = url.Values{}
if conf.Endpoint != "" {
query.Add("endpoint", conf.Endpoint)
query.Add("s3ForcePathStyle", "true")
}
if conf.Region != "" {
query.Add("region", conf.Region)
}
if conf.DisableSSL {
query.Add("disableSSL", "true")
}
if len(query) > 0 {
bucketURL = bucketURL + "?" + query.Encode()
}
return bucketURL, nil
}
// Upload takes connection initilized from newOpenBucket to upload goreleaser artifacts // Upload takes connection initilized from newOpenBucket to upload goreleaser artifacts
// Takes goreleaser context(which includes artificats) and bucketURL for upload destination (gs://gorelease-bucket) // Takes goreleaser context(which includes artificats) and bucketURL for upload destination (gs://gorelease-bucket)
func (b Bucket) Upload(ctx *context.Context, conf config.Blob) error { func (b Bucket) Upload(ctx *context.Context, conf config.Blob) error {
bucket, err := tmpl.New(ctx).Apply(conf.Bucket)
if err != nil {
return err
}
folder, err := tmpl.New(ctx).Apply(conf.Folder) folder, err := tmpl.New(ctx).Apply(conf.Folder)
if err != nil { if err != nil {
return err return err
} }
var bucketURL = fmt.Sprintf("%s://%s", conf.Provider, bucket) bucketURL, err := b.url(ctx, conf)
if conf.Endpoint != "" && conf.Provider == "s3" { if err != nil {
bucketURL = fmt.Sprintf("%s?endpoint=%s&s3ForcePathStyle=true", bucketURL, conf.Endpoint) return err
} }
// Get the openbucket connection for specific provider // Get the openbucket connection for specific provider
@ -97,6 +124,7 @@ func (b Bucket) Upload(ctx *context.Context, conf config.Blob) error {
"artifact": artifact.Name, "artifact": artifact.Name,
}).Info("uploading") }).Info("uploading")
// TODO: replace this with ?prefix=folder on the bucket url
w, err := conn.NewWriter(ctx, filepath.Join(folder, artifact.Name), nil) w, err := conn.NewWriter(ctx, filepath.Join(folder, artifact.Name), nil)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to obtain writer") return errors.Wrap(err, "failed to obtain writer")

View File

@ -325,12 +325,14 @@ type S3 struct {
// Blob contains config for GO CDK blob // Blob contains config for GO CDK blob
type Blob struct { type Blob struct {
Bucket string `yaml:",omitempty"` Bucket string `yaml:",omitempty"`
Provider string `yaml:",omitempty"` Provider string `yaml:",omitempty"`
Folder string `yaml:",omitempty"` Region string `yaml:",omitempty"`
KMSKey string `yaml:",omitempty"` DisableSSL bool `yaml:"disableSSL,omitempty"`
IDs []string `yaml:"ids,omitempty"` Folder string `yaml:",omitempty"`
Endpoint string `yaml:",omitempty"` // used for minio for example KMSKey string `yaml:",omitempty"`
IDs []string `yaml:"ids,omitempty"`
Endpoint string `yaml:",omitempty"` // used for minio for example
} }
// Upload configuration // Upload configuration

View File

@ -23,6 +23,16 @@ blobs:
# Implies s3ForcePathStyle and requires provider to be `s3` # Implies s3ForcePathStyle and requires provider to be `s3`
endpoint: https://minio.foo.bar endpoint: https://minio.foo.bar
# Sets the bucket region.
# Requires provider to be `s3`
# Defaults to empty.
region: us-west-1
# Disables SSL
# Requires provider to be `s3`
# Defaults to false
disableSSL: true
# Template for the bucket name # Template for the bucket name
bucket: goreleaser-bucket bucket: goreleaser-bucket