1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00

Merge pull request #232 from goreleaser/env

custom env support
This commit is contained in:
Carlos Alexandro Becker 2017-05-11 13:20:36 -03:00 committed by GitHub
commit 8ef9998866
5 changed files with 20 additions and 8 deletions

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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(runtimeTarget, cmd, env)
}
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(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,

View File

@ -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{