Skip to content

Commit

Permalink
feat: Allow escaping dollar in Envsubst (argoproj#7961)
Browse files Browse the repository at this point in the history
* feat: allow escaping dollar in Envsubst

Signed-off-by: Thomas Dy <[email protected]>

* docs: add variable escape documentation

Signed-off-by: Thomas Dy <[email protected]>
  • Loading branch information
thatsmydoing authored Jan 12, 2022
1 parent 4b5df69 commit c8bcabe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 11 additions & 1 deletion docs/user-guide/build-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@
* `ARGOCD_APP_SOURCE_REPO_URL` the repo's URL
* `ARGOCD_APP_SOURCE_TARGET_REVISION` - the target revision from the spec, e.g. `master`.
* `KUBE_VERSION` - the version of kubernetes
* `KUBE_API_VERSIONS` = the version of kubernetes API
* `KUBE_API_VERSIONS` = the version of kubernetes API

In case you don't want a variable to be interpolated, `$` can be escaped via `$$`.

```
command:
- sh
- -c
- |
echo $$FOO
```
7 changes: 6 additions & 1 deletion pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ func (e Env) Envsubst(s string) string {
valByEnv[item.Name] = item.Value
}
return os.Expand(s, func(s string) string {
return valByEnv[s]
// allow escaping $ with $$
if s == "$" {
return "$"
} else {
return valByEnv[s]
}
})
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2587,3 +2587,12 @@ func Test_validatePolicy_ValidResource(t *testing.T) {
err = validatePolicy("some-project", "org-admin", "p, proj:some-project:org-admin, clusters, *, some-project/*, allow")
assert.NoError(t, err)
}

func TestEnvsubst(t *testing.T) {
env := Env{
&EnvEntry{"foo", "bar"},
}

assert.Equal(t, "bar", env.Envsubst("$foo"))
assert.Equal(t, "$foo", env.Envsubst("$$foo"))
}

0 comments on commit c8bcabe

Please sign in to comment.