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,