diff --git a/README.md b/README.md index 0106cf0a8..3fb8070e7 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 0ec76564f..e9e1c403d 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -25,7 +25,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) @@ -49,16 +49,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(runtimeTarget, cmd, env) } func build(ctx *context.Context, name string, target buildTarget) error { @@ -77,12 +77,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(target, cmd, ctx.Config.Build.Env) } -func run(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...) cmd.Env = append( cmd.Env, "GOOS="+target.goos, diff --git a/pipeline/build/build_test.go b/pipeline/build/build_test.go index cb986dbb8..3053e4a4b 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(runtimeTarget, []string{"go", "list", "./..."}, emptyEnv)) } func TestRunInvalidCommand(t *testing.T) { - assert.Error(t, run(runtimeTarget, []string{"gggggo", "nope"})) + assert.Error(t, run(runtimeTarget, []string{"gggggo", "nope"}, emptyEnv)) } 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{