diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go index 624b526fe..72472302c 100644 --- a/internal/tmpl/tmpl.go +++ b/internal/tmpl/tmpl.go @@ -230,6 +230,7 @@ func (t *Template) Apply(s string) (string, error) { "reverseFilter": filter(true), "mdv2escape": mdv2Escape, "envOrDefault": t.envOrDefault, + "isEnvSet": t.isEnvSet, "map": makemap, "indexOrDefault": indexOrDefault, }). @@ -256,6 +257,11 @@ func (t *Template) ApplyAll(sps ...*string) error { return nil } +func (t *Template) isEnvSet(name string) bool { + s, ok := t.fields[env].(context.Env)[name] + return ok && s != "" +} + func (t *Template) envOrDefault(name, value string) string { s, ok := t.fields[env].(context.Env)[name] if !ok { diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go index e1daa59a0..14171215d 100644 --- a/internal/tmpl/tmpl_test.go +++ b/internal/tmpl/tmpl_test.go @@ -96,6 +96,7 @@ func TestWithArtifact(t *testing.T) { "state dirty": `state {{.GitTreeState}}`, "env bar: barrrrr": `env bar: {{ envOrDefault "BAR" "barrrrr" }}`, "env foo: bar": `env foo: {{ envOrDefault "FOO" "barrrrr" }}`, + "env foo is set: true": `env foo is set: {{ isEnvSet "FOO" }}`, "remove this": "{{ filter .Env.MULTILINE \".*remove.*\" }}", "something with\nmultiple lines\nto test things": "{{ reverseFilter .Env.MULTILINE \".*remove.*\" }}", diff --git a/www/docs/customization/templates.md b/www/docs/customization/templates.md index eb7955ffe..fa86f16cf 100644 --- a/www/docs/customization/templates.md +++ b/www/docs/customization/templates.md @@ -160,6 +160,7 @@ On all fields, you have these available functions: | `title "foo"` | "titlenize" the string using english as language. See [Title](https://pkg.go.dev/golang.org/x/text/cases#Title). Since v1.14 | | `mdv2escape "foo"` | escape characters according to MarkdownV2, especially useful in the Telegram integration. Since v1.19 | | `envOrDefault "NAME" "value"` | either gets the value of the given environment variable, or the given default. Since v1.19 | +| `isEnvSet "NAME"` | returns true if the env is set and not empty, false otherwise. Since v1.26 | | `$m := map "KEY" "VALUE"` | creates a map from a list of key and value pairs. Both keys and values must be of type `string`. Since v1.21 | | `indexOrDefault $m "KEY" "value"` | either gets the value of the given key or the given default value from the given map. Since v1.21 |