From c49c771220cb4b2ff95a5331310005b68f0dc21a Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 6 Jun 2021 19:44:24 -0300 Subject: [PATCH] feat: builds. no_unique_dist_dir (#2280) * Implemented build option output_path * feat: output path renamed to dist_path Signed-off-by: Carlos A Becker * fix: CR Signed-off-by: Carlos A Becker * fix: binary name Signed-off-by: Carlos A Becker Co-authored-by: Thomas Meckel --- internal/pipe/build/build.go | 8 ++++-- internal/pipe/build/build_test.go | 31 +++++++++++++++++++--- pkg/config/config.go | 43 ++++++++++++++++--------------- www/docs/customization/build.md | 9 +++++++ 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/internal/pipe/build/build.go b/internal/pipe/build/build.go index 5f6f8441d..eb7d87aa5 100644 --- a/internal/pipe/build/build.go +++ b/internal/pipe/build/build.go @@ -186,10 +186,14 @@ func buildOptionsForTarget(ctx *context.Context, build config.Build, target stri build.Binary = binary name := build.Binary + ext + dir := fmt.Sprintf("%s_%s", build.ID, target) + if build.NoUniqueDistDir { + dir = "" + } path, err := filepath.Abs( filepath.Join( ctx.Config.Dist, - fmt.Sprintf("%s_%s", build.ID, target), + dir, name, ), ) @@ -198,7 +202,7 @@ func buildOptionsForTarget(ctx *context.Context, build config.Build, target stri } log.WithField("binary", path).Info("building") - buildOpts.Name = name + buildOpts.Name = filepath.Base(name) buildOpts.Path = path return &buildOpts, nil } diff --git a/internal/pipe/build/build_test.go b/internal/pipe/build/build_test.go index b27f7f59c..d2eb7e892 100644 --- a/internal/pipe/build/build_test.go +++ b/internal/pipe/build/build_test.go @@ -140,6 +140,7 @@ func TestRunFullPipe(t *testing.T) { } ctx := context.New(config) ctx.Git.CurrentTag = "2.4.5" + require.NoError(t, Pipe{}.Default(ctx)) require.NoError(t, Pipe{}.Run(ctx)) require.Equal(t, ctx.Artifacts.List(), []*artifact.Artifact{{ Name: "testing", @@ -712,6 +713,7 @@ func TestBuildOptionsForTarget(t *testing.T) { name string build config.Build expectedOpts *api.Options + expectedErr string }{ { name: "simple options for target", @@ -747,6 +749,24 @@ func TestBuildOptionsForTarget(t *testing.T) { Arch: "amd64", }, }, + { + name: "overriding dist path", + build: config.Build{ + ID: "testid", + Binary: "distpath/{{.Os}}/{{.Arch}}/testbinary_{{.Os}}_{{.Arch}}", + Targets: []string{ + "linux_amd64", + }, + NoUniqueDistDir: true, + }, + expectedOpts: &api.Options{ + Name: "testbinary_linux_amd64", + Path: filepath.Join(tmpDir, "distpath", "linux", "amd64", "testbinary_linux_amd64"), + Target: "linux_amd64", + Os: "linux", + Arch: "amd64", + }, + }, } for _, tc := range testCases { @@ -755,9 +775,14 @@ func TestBuildOptionsForTarget(t *testing.T) { Dist: tmpDir, Builds: []config.Build{tc.build}, }) - opts, err := buildOptionsForTarget(ctx, tc.build, tc.build.Targets[0]) - require.NoError(t, err) - require.Equal(t, tc.expectedOpts, opts) + require.NoError(t, Pipe{}.Default(ctx)) + opts, err := buildOptionsForTarget(ctx, ctx.Config.Builds[0], ctx.Config.Builds[0].Targets[0]) + if tc.expectedErr == "" { + require.NoError(t, err) + require.Equal(t, tc.expectedOpts, opts) + } else { + require.EqualError(t, err, tc.expectedErr) + } }) } } diff --git a/pkg/config/config.go b/pkg/config/config.go index e25f1bb18..93181e012 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -183,27 +183,28 @@ func (a *FlagArray) UnmarshalYAML(unmarshal func(interface{}) error) error { // Build contains the build configuration section. type Build struct { - ID string `yaml:",omitempty"` - Goos []string `yaml:",omitempty"` - Goarch []string `yaml:",omitempty"` - Goarm []string `yaml:",omitempty"` - Gomips []string `yaml:",omitempty"` - Targets []string `yaml:",omitempty"` - Ignore []IgnoredBuild `yaml:",omitempty"` - Dir string `yaml:",omitempty"` - Main string `yaml:",omitempty"` - Ldflags StringArray `yaml:",omitempty"` - Tags FlagArray `yaml:",omitempty"` - Flags FlagArray `yaml:",omitempty"` - Binary string `yaml:",omitempty"` - Hooks HookConfig `yaml:",omitempty"` - Env []string `yaml:",omitempty"` - Lang string `yaml:",omitempty"` - Asmflags StringArray `yaml:",omitempty"` - Gcflags StringArray `yaml:",omitempty"` - ModTimestamp string `yaml:"mod_timestamp,omitempty"` - Skip bool `yaml:",omitempty"` - GoBinary string `yaml:",omitempty"` + ID string `yaml:",omitempty"` + Goos []string `yaml:",omitempty"` + Goarch []string `yaml:",omitempty"` + Goarm []string `yaml:",omitempty"` + Gomips []string `yaml:",omitempty"` + Targets []string `yaml:",omitempty"` + Ignore []IgnoredBuild `yaml:",omitempty"` + Dir string `yaml:",omitempty"` + Main string `yaml:",omitempty"` + Ldflags StringArray `yaml:",omitempty"` + Tags FlagArray `yaml:",omitempty"` + Flags FlagArray `yaml:",omitempty"` + Binary string `yaml:",omitempty"` + Hooks HookConfig `yaml:",omitempty"` + Env []string `yaml:",omitempty"` + Lang string `yaml:",omitempty"` + Asmflags StringArray `yaml:",omitempty"` + Gcflags StringArray `yaml:",omitempty"` + ModTimestamp string `yaml:"mod_timestamp,omitempty"` + Skip bool `yaml:",omitempty"` + GoBinary string `yaml:",omitempty"` + NoUniqueDistDir bool `yaml:"no_unique_dist_dir,omitempty"` } type HookConfig struct { diff --git a/www/docs/customization/build.md b/www/docs/customization/build.md index 320efb739..e8b001f59 100644 --- a/www/docs/customization/build.md +++ b/www/docs/customization/build.md @@ -134,6 +134,15 @@ builds: # Useful for library projects. # Default is false skip: false + + # By default, GoRelaser will create your binaries inside `dist/${BuildID}_${BuildTarget}`, which is an unique directory per build target in the matrix. + # You are able to set subdirs within that folder using the `binary` property. + # + # However, if for some reason you don't want that unique directory to be created, you can set this property. + # If you do, you are responsible of keeping different builds from overriding each other. + # + # Defaults to `false`. + no_unique_dist_dir: true ``` !!! tip