Repository: goreleaser/goreleaser
Pull request #2667 (2021-11-14 01:25:23)

feat: templates on sign env
allow templating signs.env

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

> :exclamation: Current head bae1f05 differs from pull request most recent head 953eca0. Consider uploading reports for the commit 953eca0 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2667/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/2667?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##             main    #2667      +/-   ##
==========================================
+ Coverage   84.65%   84.68%   +0.02%     
==========================================
  Files         102      102              
  Lines        7761     7775      +14     
==========================================
+ Hits         6570     6584      +14     
  Misses        977      977              
  Partials      214      214              
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2667?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/2667?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [0d0b7aa...953eca0](https://codecov.io/gh/goreleaser/goreleaser/pull/2667?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/sign/sign.go b/internal/pipe/sign/sign.go
index fcf58536..9a5e04ea 100644
--- a/internal/pipe/sign/sign.go
+++ b/internal/pipe/sign/sign.go
@@ -117,7 +117,13 @@ func signone(ctx *context.Context, cfg config.Sign, art *artifact.Artifact) ([]*
 	env["artifactName"] = art.Name
 	env["artifact"] = art.Path
 	env["artifactID"] = art.ID()
-	for k, v := range context.ToEnv(cfg.Env) {
+
+	tmplEnv, err := templateEnvS(ctx, cfg.Env)
+	if err != nil {
+		return nil, fmt.Errorf("sign failed: %s: %w", art.Name, err)
+	}
+
+	for k, v := range context.ToEnv(tmplEnv) {
 		env[k] = v
 	}
 
@@ -174,7 +180,7 @@ func signone(ctx *context.Context, cfg config.Sign, art *artifact.Artifact) ([]*
 	if stdin != nil {
 		cmd.Stdin = stdin
 	}
-	cmd.Env = append(env.Strings(), cfg.Env...)
+	cmd.Env = env.Strings()
 	log.WithFields(fields).Info("signing")
 	if err := cmd.Run(); err != nil {
 		return nil, fmt.Errorf("sign: %s failed: %w: %s", cfg.Cmd, err, b.String())
@@ -222,3 +228,15 @@ func expand(s string, env map[string]string) string {
 		return env[key]
 	})
 }
+
+func templateEnvS(ctx *context.Context, s []string) ([]string, error) {
+	var out []string
+	for _, s := range s {
+		ts, err := tmpl.New(ctx).WithEnvS(out).Apply(s)
+		if err != nil {
+			return nil, err
+		}
+		out = append(out, ts)
+	}
+	return out, nil
+}


Test patch:
diff --git a/internal/pipe/sign/sign_test.go b/internal/pipe/sign/sign_test.go
index e31c222e..a2b2ee3c 100644
--- a/internal/pipe/sign/sign_test.go
+++ b/internal/pipe/sign/sign_test.go
@@ -154,6 +154,21 @@ func TestSignArtifacts(t *testing.T) {
 				},
 			),
 		},
+		{
+			desc:           "invalid env template",
+			expectedErrMsg: `sign failed: artifact1: template: tmpl:1:5: executing "tmpl" at <.blah>: map has no entry for key "blah"`,
+			ctx: context.New(
+				config.Project{
+					Signs: []config.Sign{
+						{
+							Artifacts: "all",
+							Cmd:       "exit",
+							Env:       []string{"A={{ .blah }}"},
+						},
+					},
+				},
+			),
+		},
 		{
 			desc: "sign single",
 			ctx: context.New(
@@ -509,7 +524,7 @@ func TestSignArtifacts(t *testing.T) {
 				config.Project{
 					Signs: []config.Sign{
 						{
-							Env:         []string{"HONK=honk"},
+							Env:         []string{"NOT_HONK=honk", "HONK={{ .Env.NOT_HONK }}"},
 							Certificate: `{{ trimsuffix (trimsuffix .Env.artifactName ".tar.gz") ".deb" }}_${HONK}.pem`,
 							Artifacts:   "all",
 						},