diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go index 8661cb85353..042718afffc 100644 --- a/internal/pipe/archive/archive.go +++ b/internal/pipe/archive/archive.go @@ -190,11 +190,15 @@ func doCreate(ctx *context.Context, arch config.Archive, binaries []*artifact.Ar } bins := []string{} for _, binary := range binaries { + dst := binary.Name + if arch.StripParentBinaryFolder { + dst = filepath.Base(dst) + } if err := a.Add(config.File{ Source: binary.Path, - Destination: binary.Name, + Destination: dst, }); err != nil { - return fmt.Errorf("failed to add: '%s' -> '%s': %w", binary.Path, binary.Name, err) + return fmt.Errorf("failed to add: '%s' -> '%s': %w", binary.Path, dst, err) } bins = append(bins, binary.Name) } diff --git a/internal/pipe/archive/archive_test.go b/internal/pipe/archive/archive_test.go index 4fba3b4196f..e863063eaa6 100644 --- a/internal/pipe/archive/archive_test.go +++ b/internal/pipe/archive/archive_test.go @@ -33,9 +33,35 @@ func createFakeBinary(t *testing.T, dist, arch, bin string) { func TestRunPipe(t *testing.T) { folder := testlib.Mktmp(t) - for _, format := range []string{"tar.gz", "zip"} { - t.Run("Archive format "+format, func(t *testing.T) { - dist := filepath.Join(folder, format+"_dist") + for _, dets := range []struct { + Format string + Strip bool + }{ + { + Format: "tar.gz", + Strip: true, + }, + { + Format: "tar.gz", + Strip: false, + }, + + { + Format: "zip", + Strip: true, + }, + { + Format: "zip", + Strip: false, + }, + } { + format := dets.Format + name := "archive." + format + if dets.Strip { + name = "strip_" + name + } + t.Run(name, func(t *testing.T) { + dist := filepath.Join(folder, name+"_dist") require.NoError(t, os.Mkdir(dist, 0o755)) for _, arch := range []string{"darwinamd64v1", "darwinall", "linux386", "linuxarm7", "linuxmipssoftfloat", "linuxamd64v3"} { createFakeBinary(t, dist, arch, "bin/mybin") @@ -56,9 +82,10 @@ func TestRunPipe(t *testing.T) { ProjectName: "foobar", Archives: []config.Archive{ { - ID: "myid", - Builds: []string{"default"}, - NameTemplate: defaultNameTemplate, + ID: "myid", + Builds: []string{"default"}, + NameTemplate: defaultNameTemplate, + StripParentBinaryFolder: dets.Strip, Files: []config.File{ {Source: "README.{{.Os}}.*"}, {Source: "./foo/**/*"}, @@ -169,6 +196,7 @@ func TestRunPipe(t *testing.T) { ctx.Config.Archives[0].Format = format require.NoError(t, Pipe{}.Run(ctx)) archives := ctx.Artifacts.Filter(artifact.ByType(artifact.UploadableArchive)).List() + for _, arch := range archives { expectBin := "bin/mybin" if arch.Goos == "windows" { @@ -181,6 +209,11 @@ func TestRunPipe(t *testing.T) { require.Len(t, archives, 7) // TODO: should verify the artifact fields here too + expectBin := "bin/mybin" + if dets.Strip { + expectBin = "mybin" + } + if format == "tar.gz" { // Check archive contents for name, os := range map[string]string{ @@ -196,7 +229,7 @@ func TestRunPipe(t *testing.T) { []string{ fmt.Sprintf("README.%s.md", os), "foo/bar/foobar/blah.txt", - "bin/mybin", + expectBin, }, tarFiles(t, filepath.Join(dist, name)), ) @@ -208,7 +241,7 @@ func TestRunPipe(t *testing.T) { []string{ "README.windows.md", "foo/bar/foobar/blah.txt", - "bin/mybin.exe", + expectBin + ".exe", }, zipFiles(t, filepath.Join(dist, "foobar_0.0.1_windows_amd64.zip")), ) diff --git a/pkg/config/config.go b/pkg/config/config.go index 5d834dd8964..e880fe27ce4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -493,6 +493,7 @@ type Archive struct { Format string `yaml:"format,omitempty" json:"format,omitempty"` FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty" json:"format_overrides,omitempty"` WrapInDirectory string `yaml:"wrap_in_directory,omitempty" json:"wrap_in_directory,omitempty" jsonschema:"oneof_type=string;boolean"` + StripParentBinaryFolder bool `yaml:"strip_parent_binary_folder,omitempty" json:"strip_parent_binary_folder,omitempty"` Files []File `yaml:"files,omitempty" json:"files,omitempty"` Meta bool `yaml:"meta,omitempty" json:"meta,omitempty"` AllowDifferentBinaryCount bool `yaml:"allow_different_binary_count,omitempty" json:"allow_different_binary_count,omitempty"` diff --git a/www/docs/customization/archive.md b/www/docs/customization/archive.md index 803b137c281..239eff61285 100644 --- a/www/docs/customization/archive.md +++ b/www/docs/customization/archive.md @@ -56,6 +56,11 @@ archives: # Default is false. wrap_in_directory: true + # If set to true, will strip the parent directories away from binary files. + # This might be useful if you have your binary be built with a subdir for some reason, but do no want that subdir inside the archive. + # Default is false. + strip_parent_binary_folder: true + # Can be used to change the archive formats for specific GOOSs. # Most common use case is to archive as zip on Windows. # Default is empty.