diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go index e6371a95971..1fda16039a6 100644 --- a/internal/tmpl/tmpl.go +++ b/internal/tmpl/tmpl.go @@ -119,7 +119,10 @@ func New(ctx *context.Context) *Template { func (t *Template) WithEnvS(envs []string) *Template { result := map[string]string{} for _, env := range envs { - k, v, _ := strings.Cut(env, "=") + k, v, ok := strings.Cut(env, "=") + if !ok || k == "" { + continue + } result[k] = v } return t.WithEnv(result) diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go index 14f8bb169a6..bccb35ecc26 100644 --- a/internal/tmpl/tmpl_test.go +++ b/internal/tmpl/tmpl_test.go @@ -159,12 +159,21 @@ func TestWithEnv(t *testing.T) { "FOO": "BAR", } ctx.Git.CurrentTag = "v1.2.3" - out, err := New(ctx).WithEnvS([]string{ + tpl := New(ctx).WithEnvS([]string{ "FOO=foo", "BAR=bar", - }).Apply("{{ .Env.FOO }}-{{ .Env.BAR }}") + "NOVAL=", + "=NOKEY", + "=", + "NOTHING", + }) + out, err := tpl.Apply("{{ .Env.FOO }}-{{ .Env.BAR }}") require.NoError(t, err) require.Equal(t, "foo-bar", out) + + out, err = tpl.Apply(`{{ range $idx, $key := .Env }}{{ $idx }},{{ end }}`) + require.NoError(t, err) + require.Equal(t, "BAR,FOO,NOVAL,", out) } func TestFuncMap(t *testing.T) { diff --git a/www/docs/customization/build.md b/www/docs/customization/build.md index 472cf50c197..c3dbbcad0cd 100644 --- a/www/docs/customization/build.md +++ b/www/docs/customization/build.md @@ -71,6 +71,8 @@ builds: # Custom environment variables to be set during the builds. # + # Invalid environment variables will be ignored. + # # Default: `os.Environ()` merged with what you set the root `env` section. env: - CGO_ENABLED=0