Skip to content

Commit

Permalink
feat: blobs.disable (#3884)
Browse files Browse the repository at this point in the history
Allows to template-disable specific blob configurations.

Closes #3883
  • Loading branch information
caarlos0 committed Mar 23, 2023
1 parent 7229a0d commit f82a32c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/pipe/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package blob
import (
"fmt"

"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
)

Expand Down Expand Up @@ -33,11 +35,23 @@ func (Pipe) Default(ctx *context.Context) error {
// Publish to specified blob bucket url.
func (Pipe) Publish(ctx *context.Context) error {
g := semerrgroup.New(ctx.Parallelism)
skips := pipe.SkipMemento{}
for _, conf := range ctx.Config.Blobs {
conf := conf
g.Go(func() error {
b, err := tmpl.New(ctx).Bool(conf.Disable)
if err != nil {
return err
}
if b {
skips.Remember(pipe.Skip("configuration is disabled"))
return nil
}
return doUpload(ctx, conf)
})
}
return g.Wait()
if err := g.Wait(); err != nil {
return err
}
return skips.Evaluate()
}
89 changes: 89 additions & 0 deletions internal/pipe/blob/blob_minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,95 @@ func TestMinioUploadInvalidCustomBucketID(t *testing.T) {
require.Error(t, Pipe{}.Publish(ctx))
}

func TestMinioUploadSkip(t *testing.T) {
name := "basic"
folder := t.TempDir()
debpath := filepath.Join(folder, "bin.deb")
tgzpath := filepath.Join(folder, "bin.tar.gz")
require.NoError(t, os.WriteFile(tgzpath, []byte("fake\ntargz"), 0o744))
require.NoError(t, os.WriteFile(debpath, []byte("fake\ndeb"), 0o744))

buildCtx := func(uploadID string) *context.Context {
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "testupload",
Blobs: []config.Blob{
{
Provider: "s3",
Bucket: name,
Region: "us-east",
Endpoint: "http://" + listen,
IDs: []string{"foo"},
Disable: `{{ eq .Env.UPLOAD_ID "foo" }}`,
},
{
Provider: "s3",
Bucket: name,
Region: "us-east",
Endpoint: "http://" + listen,
Disable: `{{ eq .Env.UPLOAD_ID "bar" }}`,
IDs: []string{"bar"},
},
},
},
testctx.WithCurrentTag("v1.0.0"),
testctx.WithEnv(map[string]string{
"UPLOAD_ID": uploadID,
}),
)
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
Path: tgzpath,
Extra: map[string]interface{}{
artifact.ExtraID: "foo",
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.LinuxPackage,
Name: "bin.deb",
Path: debpath,
Extra: map[string]interface{}{
artifact.ExtraID: "bar",
},
})
return ctx
}

setupBucket(t, testlib.MustDockerPool(t), name)

t.Run("upload only foo", func(t *testing.T) {
ctx := buildCtx("foo")
require.NoError(t, Pipe{}.Default(ctx))
testlib.AssertSkipped(t, Pipe{}.Publish(ctx))
require.Subset(t, getFiles(t, ctx, ctx.Config.Blobs[0]), []string{
"testupload/v1.0.0/bin.deb",
})
})

t.Run("upload only bar", func(t *testing.T) {
ctx := buildCtx("bar")
require.NoError(t, Pipe{}.Default(ctx))
testlib.AssertSkipped(t, Pipe{}.Publish(ctx))
require.Subset(t, getFiles(t, ctx, ctx.Config.Blobs[0]), []string{
"testupload/v1.0.0/bin.tar.gz",
})
})

t.Run("invalid tmpl", func(t *testing.T) {
ctx := buildCtx("none")
ctx.Config.Blobs = []config.Blob{{
Provider: "s3",
Bucket: name,
Endpoint: "http://" + listen,
Disable: `{{ .Env.NOME }}`,
}}
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Publish(ctx))
})
}

func prepareEnv() {
os.Setenv("AWS_ACCESS_KEY_ID", minioUser)
os.Setenv("AWS_SECRET_ACCESS_KEY", minioPwd)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ type Blob struct {
IDs []string `yaml:"ids,omitempty" json:"ids,omitempty"`
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"` // used for minio for example
ExtraFiles []ExtraFile `yaml:"extra_files,omitempty" json:"extra_files,omitempty"`
Disable string `yaml:"disable,omitempty" json:"disable,omitempty" jsonschema:"oneof_type=string;boolean"`
}

// Upload configuration.
Expand Down
6 changes: 6 additions & 0 deletions www/docs/customization/blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ blobs:
# Default is `{{ .ProjectName }}/{{ .Tag }}`
folder: "foo/bar/{{.Version}}"

# Template to disable this particular upload configuration.
#
# Since: v1.17
# Default: false
disable: '{{ neq .BLOB_UPLOAD_ONLY "foo" }}'

# You can add extra pre-existing files to the bucket.
# The filename on the release will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
Expand Down
10 changes: 10 additions & 0 deletions www/docs/static/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f82a32c

Please sign in to comment.