Skip to content

Commit

Permalink
fix: gomod.env not being used (#3434)
Browse files Browse the repository at this point in the history
Fixes  #3426

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Oct 4, 2022
1 parent ae9c8ac commit 4a51099
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 3 additions & 1 deletion internal/pipe/gomod/gomod.go
Expand Up @@ -33,7 +33,9 @@ func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.GoMod.Mod != "" {
flags = append(flags, "-mod="+ctx.Config.GoMod.Mod)
}
out, err := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...).CombinedOutput()
cmd := exec.CommandContext(ctx, ctx.Config.GoMod.GoBinary, flags...)
cmd.Env = append(ctx.Env.Strings(), ctx.Config.GoMod.Env...)
out, err := cmd.CombinedOutput()
result := strings.TrimSpace(string(out))
if result == go115NotAGoModuleError || result == go116NotAGoModuleError {
return pipe.Skip("not a go module")
Expand Down
13 changes: 13 additions & 0 deletions internal/pipe/gomod/gomod_test.go
Expand Up @@ -29,6 +29,19 @@ func TestRunCustomMod(t *testing.T) {
require.Equal(t, "github.com/goreleaser/goreleaser", ctx.ModulePath)
}

func TestCustomEnv(t *testing.T) {
bin := filepath.Join(t.TempDir(), "go.bin")
require.NoError(t, os.WriteFile(bin, []byte("#!/bin/sh\nenv | grep -qw FOO=bar"), 0o755))
ctx := context.New(config.Project{
GoMod: config.GoMod{
GoBinary: bin,
Env: []string{"FOO=bar"},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Run(ctx))
}

func TestRunOutsideGoModule(t *testing.T) {
dir := testlib.Mktmp(t)
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\nfunc main() {println(0)}"), 0o666))
Expand Down
3 changes: 2 additions & 1 deletion www/docs/customization/build.md
Expand Up @@ -60,7 +60,8 @@ builds:
- feature

# Custom environment variables to be set during the builds.
# Default is empty.
#
# Default: `os.Environ()` merged with what you set the root `env` section.
env:
- CGO_ENABLED=0

Expand Down
8 changes: 5 additions & 3 deletions www/docs/customization/verifiable_builds.md
Expand Up @@ -18,12 +18,13 @@ gomod:
# this setting.
# Notice: for this to work your `build.main` must be a package, not a `.go` file.
#
# Default is false.
# Default: false.
proxy: true

# If proxy is true, use these environment variables when running `go mod`
# commands (namely, `go mod tidy`).
# Defaults to `os.Environ()`.
#
# Default: `os.Environ()` merged with what you set the root `env` section.
env:
- GOPROXY=https://proxy.golang.org,direct
- GOSUMDB=sum.golang.org
Expand All @@ -36,7 +37,8 @@ gomod:
mod: mod

# Which Go binary to use.
# Defaults to `go`.
#
# Default: `go`.
gobinary: go1.17
```

Expand Down

0 comments on commit 4a51099

Please sign in to comment.