From 9a3c8dbafbda9fd36fca8565756c892a64ff5169 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 11 May 2017 10:43:25 -0300 Subject: [PATCH 1/3] custom env --- README.md | 5 +++++ config/config.go | 1 + goreleaser.yml | 2 ++ pipeline/build/build.go | 13 +++++++------ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bf2dee3f3..18d254167 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,11 @@ build: # Date format is `2006-01-02_15:04:05` ldflags: -s -w -X main.build={{.Version}} + # Custom environment variables to be set durign the builds. + # Default is empty + env: + - CGO_ENABLED=0 + # GOOS list to build in. # For more info refer to https://golang.org/doc/install/source#environment # Defaults are darwin and linux diff --git a/config/config.go b/config/config.go index 3e951e3ff..68516e79a 100644 --- a/config/config.go +++ b/config/config.go @@ -55,6 +55,7 @@ type Build struct { Flags string `yaml:",omitempty"` Binary string `yaml:",omitempty"` Hooks Hooks `yaml:",omitempty"` + Env []string `yaml:",omitempty"` } // FormatOverride is used to specify a custom format for a specific GOOS. diff --git a/goreleaser.yml b/goreleaser.yml index 0854c2de8..0b62b0ed3 100644 --- a/goreleaser.yml +++ b/goreleaser.yml @@ -1,6 +1,8 @@ homepage: &homepage http://goreleaser.github.io description: &description Deliver Go binaries as fast and easily as possible build: + env: + - CGO_ENABLED=0 goos: - linux - darwin diff --git a/pipeline/build/build.go b/pipeline/build/build.go index b41675dd2..37f8d4404 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -24,7 +24,7 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { - if err := runHook(ctx.Config.Build.Hooks.Pre); err != nil { + if err := runHook(ctx.Config.Build.Env, ctx.Config.Build.Hooks.Pre); err != nil { return err } sem := make(chan bool, 4) @@ -48,16 +48,16 @@ func (Pipe) Run(ctx *context.Context) error { if err := g.Wait(); err != nil { return err } - return runHook(ctx.Config.Build.Hooks.Post) + return runHook(ctx.Config.Build.Env, ctx.Config.Build.Hooks.Post) } -func runHook(hook string) error { +func runHook(env []string, hook string) error { if hook == "" { return nil } log.Println("Running hook", hook) cmd := strings.Fields(hook) - return run(runtimeTarget, cmd) + return run(env, runtimeTarget, cmd) } func build(ctx *context.Context, name string, target buildTarget) error { @@ -76,12 +76,13 @@ func build(ctx *context.Context, name string, target buildTarget) error { return err } cmd = append(cmd, "-ldflags="+flags, "-o", output, ctx.Config.Build.Main) - return run(target, cmd) + return run(ctx.Config.Build.Env, target, cmd) } -func run(target buildTarget, command []string) error { +func run(env []string, target buildTarget, command []string) error { cmd := exec.Command(command[0], command[1:]...) cmd.Env = append(cmd.Env, os.Environ()...) + cmd.Env = append(cmd.Env, env...) cmd.Env = append( cmd.Env, "GOOS="+target.goos, From a6822c53ab7d5d47297f9737e3034361854839f6 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 11 May 2017 10:44:44 -0300 Subject: [PATCH 2/3] test --- pipeline/build/build_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pipeline/build/build_test.go b/pipeline/build/build_test.go index cb986dbb8..fc179d720 100644 --- a/pipeline/build/build_test.go +++ b/pipeline/build/build_test.go @@ -12,16 +12,18 @@ import ( "github.com/stretchr/testify/assert" ) +var emptyEnv []string + func TestPipeDescription(t *testing.T) { assert.NotEmpty(t, Pipe{}.Description()) } func TestRun(t *testing.T) { - assert.NoError(t, run(runtimeTarget, []string{"go", "list", "./..."})) + assert.NoError(t, run(emptyEnv, runtimeTarget, []string{"go", "list", "./..."})) } func TestRunInvalidCommand(t *testing.T) { - assert.Error(t, run(runtimeTarget, []string{"gggggo", "nope"})) + assert.Error(t, run(emptyEnv, runtimeTarget, []string{"gggggo", "nope"})) } func TestBuild(t *testing.T) { @@ -30,6 +32,7 @@ func TestBuild(t *testing.T) { Build: config.Build{ Binary: "testing", Flags: "-n", + Env: []string{"BLAH=1"}, }, } var ctx = &context.Context{ From 44d9ddd1046b9aebda90a474e86e9731a866b337 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 11 May 2017 10:47:03 -0300 Subject: [PATCH 3/3] reorder --- pipeline/build/build.go | 6 +++--- pipeline/build/build_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pipeline/build/build.go b/pipeline/build/build.go index 37f8d4404..495cfaa2a 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -57,7 +57,7 @@ func runHook(env []string, hook string) error { } log.Println("Running hook", hook) cmd := strings.Fields(hook) - return run(env, runtimeTarget, cmd) + return run(runtimeTarget, cmd, env) } func build(ctx *context.Context, name string, target buildTarget) error { @@ -76,10 +76,10 @@ func build(ctx *context.Context, name string, target buildTarget) error { return err } cmd = append(cmd, "-ldflags="+flags, "-o", output, ctx.Config.Build.Main) - return run(ctx.Config.Build.Env, target, cmd) + return run(target, cmd, ctx.Config.Build.Env) } -func run(env []string, target buildTarget, command []string) error { +func run(target buildTarget, command, env []string) error { cmd := exec.Command(command[0], command[1:]...) cmd.Env = append(cmd.Env, os.Environ()...) cmd.Env = append(cmd.Env, env...) diff --git a/pipeline/build/build_test.go b/pipeline/build/build_test.go index fc179d720..3053e4a4b 100644 --- a/pipeline/build/build_test.go +++ b/pipeline/build/build_test.go @@ -19,11 +19,11 @@ func TestPipeDescription(t *testing.T) { } func TestRun(t *testing.T) { - assert.NoError(t, run(emptyEnv, runtimeTarget, []string{"go", "list", "./..."})) + assert.NoError(t, run(runtimeTarget, []string{"go", "list", "./..."}, emptyEnv)) } func TestRunInvalidCommand(t *testing.T) { - assert.Error(t, run(emptyEnv, runtimeTarget, []string{"gggggo", "nope"})) + assert.Error(t, run(runtimeTarget, []string{"gggggo", "nope"}, emptyEnv)) } func TestBuild(t *testing.T) {