Repository: goreleaser/goreleaser
Pull request #2246 (2021-05-25 01:29:18)

feat: env template support 
closes #1845 #1820

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/goreleaser/CbDXPKuw5yibasue6KuH22Mua9ph](https://vercel.com/goreleaser/goreleaser/CbDXPKuw5yibasue6KuH22Mua9ph)  
✅ Preview: *In Progress*


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2246?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#2246](https://codecov.io/gh/goreleaser/goreleaser/pull/2246?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (19f5799) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/d521343b0610c139d0e5540d3806e622b8633d68?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (d521343) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2246/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/goreleaser/pull/2246?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master    #2246      +/-   ##
==========================================
+ Coverage   82.71%   82.73%   +0.02%     
==========================================
  Files          76       76              
  Lines        4194     4199       +5     
==========================================
+ Hits         3469     3474       +5     
  Misses        588      588              
  Partials      137      137              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2246?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [internal/pipe/env/env.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2246/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9lbnYvZW52Lmdv) | `97.05% <100.00%> (+0.23%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2246?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2246?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [d521343...19f5799](https://codecov.io/gh/goreleaser/goreleaser/pull/2246?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).


Patch:
diff --git a/internal/pipe/env/env.go b/internal/pipe/env/env.go
index 3fe38793..4fe35dbf 100644
--- a/internal/pipe/env/env.go
+++ b/internal/pipe/env/env.go
@@ -9,6 +9,7 @@ import (
 	"os"
 
 	"github.com/apex/log"
+	"github.com/goreleaser/goreleaser/internal/tmpl"
 	"github.com/goreleaser/goreleaser/pkg/context"
 	homedir "github.com/mitchellh/go-homedir"
 )
@@ -42,6 +43,15 @@ func setDefaultTokenFiles(ctx *context.Context) {
 
 // Run the pipe.
 func (Pipe) Run(ctx *context.Context) error {
+	templ := tmpl.New(ctx).WithEnvS(os.Environ())
+	for i := range ctx.Config.Env {
+		env, err := templ.Apply(ctx.Config.Env[i])
+		if err != nil {
+			return err
+		}
+		ctx.Config.Env[i] = env
+	}
+
 	setDefaultTokenFiles(ctx)
 	githubToken, githubTokenErr := loadEnv("GITHUB_TOKEN", ctx.Config.EnvFiles.GitHubToken)
 	gitlabToken, gitlabTokenErr := loadEnv("GITLAB_TOKEN", ctx.Config.EnvFiles.GitLabToken)
diff --git a/www/docs/customization/env.md b/www/docs/customization/env.md
index 5367cc56..0fff1e32 100644
--- a/www/docs/customization/env.md
+++ b/www/docs/customization/env.md
@@ -21,3 +21,7 @@ builds:
 This way, both `go mod tidy` and the underlying `go build` will have
 `GO111MODULE` set to `on`.
 
+The root `env` section also accepts templates.
+
+!!! tip
+    Learn more about the [name template engine](/customization/templates/).


Test patch:
diff --git a/internal/pipe/env/env_test.go b/internal/pipe/env/env_test.go
index 0229c1aa..81877f84 100644
--- a/internal/pipe/env/env_test.go
+++ b/internal/pipe/env/env_test.go
@@ -33,6 +33,28 @@ func TestSetDefaultTokenFiles(t *testing.T) {
 		setDefaultTokenFiles(ctx)
 		require.Equal(t, cfg, ctx.Config.EnvFiles.GitHubToken)
 	})
+	t.Run("templates", func(t *testing.T) {
+		ctx := context.New(config.Project{
+			ProjectName: "foobar",
+			Env: []string{
+				"FOO=FOO_{{ .Env.BAR }}",
+				"FOOBAR={{.ProjectName}}",
+			},
+		})
+		os.Setenv("BAR", "lebar")
+		os.Setenv("GITHUB_TOKEN", "fake")
+		require.NoError(t, Pipe{}.Run(ctx))
+		require.Equal(t, ctx.Config.Env, []string{"FOO=FOO_lebar", "FOOBAR=foobar"})
+	})
+
+	t.Run("template error", func(t *testing.T) {
+		ctx := context.New(config.Project{
+			Env: []string{
+				"FOO={{ .Asss }",
+			},
+		})
+		require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
+	})
 }
 
 func TestValidGithubEnv(t *testing.T) {