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

feat: strip_parent_binary_folder #3261

Merged
merged 4 commits into from Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/pipe/archive/archive.go
Expand Up @@ -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)
}
Expand Down
49 changes: 41 additions & 8 deletions internal/pipe/archive/archive_test.go
Expand Up @@ -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")
Expand All @@ -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/**/*"},
Expand Down Expand Up @@ -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" {
Expand All @@ -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{
Expand All @@ -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)),
)
Expand All @@ -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")),
)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -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"`
Expand Down
5 changes: 5 additions & 0 deletions www/docs/customization/archive.md
Expand Up @@ -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.
Expand Down