From 47a8b25cf6951d96e69d0ae299db1649b6fa4854 Mon Sep 17 00:00:00 2001 From: Fabian Kramm Date: Tue, 24 May 2022 16:18:55 +0200 Subject: [PATCH] feat: add devspace vars as environment variables --- e2e/tests/hooks/hooks.go | 3 +++ e2e/tests/hooks/testdata/once/devspace.yaml | 3 +++ pkg/devspace/hook/local_command.go | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/e2e/tests/hooks/hooks.go b/e2e/tests/hooks/hooks.go index cef9557b29..5238c05233 100644 --- a/e2e/tests/hooks/hooks.go +++ b/e2e/tests/hooks/hooks.go @@ -219,6 +219,9 @@ var _ = DevSpaceDescribe("hooks", func() { }) framework.ExpectNoError(err) + // check namespace hook + framework.ExpectLocalFileContentsImmediately("namespace.txt", ns) + // stop second command cancel2() diff --git a/e2e/tests/hooks/testdata/once/devspace.yaml b/e2e/tests/hooks/testdata/once/devspace.yaml index 3af1394bc9..991e4ec105 100644 --- a/e2e/tests/hooks/testdata/once/devspace.yaml +++ b/e2e/tests/hooks/testdata/once/devspace.yaml @@ -12,6 +12,9 @@ deployments: command: ["sleep"] args: ["999999999999"] hooks: + - command: | + echo -n $DEVSPACE_NAMESPACE > namespace.txt + events: ["after:deploy"] - command: | mkdir -p /app echo -n $RANDOM > /app/once.out diff --git a/pkg/devspace/hook/local_command.go b/pkg/devspace/hook/local_command.go index 78c2e17d46..8cdf1737ef 100644 --- a/pkg/devspace/hook/local_command.go +++ b/pkg/devspace/hook/local_command.go @@ -4,11 +4,13 @@ import ( "bytes" "context" "encoding/json" + "fmt" runtimevar "github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/runtime" devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context" "github.com/loft-sh/devspace/pkg/devspace/pipeline/engine" "io" "os" + "regexp" "strings" "github.com/loft-sh/devspace/pkg/devspace/config" @@ -30,6 +32,8 @@ type localCommandHook struct { Stderr io.Writer } +var EnvironmentVariableRegEx = regexp.MustCompile(`^[A-Za-z0-9_]+$`) + func (l *localCommandHook) Execute(ctx devspacecontext.Context, hook *latest.HookConfig, cmdExtraEnv map[string]string) error { // Create extra env variables osArgsBytes, err := json.Marshal(os.Args) @@ -46,6 +50,13 @@ func (l *localCommandHook) Execute(ctx devspacecontext.Context, hook *latest.Hoo for k, v := range cmdExtraEnv { extraEnv[k] = v } + for k, v := range ctx.Config().Variables() { + if !EnvironmentVariableRegEx.MatchString(k) { + continue + } + + extraEnv[k] = fmt.Sprintf("%v", v) + } // resolve hook command and args hookCommand, hookArgs, err := ResolveCommand(ctx.Context(), hook.Command, hook.Args, ctx.WorkingDir(), ctx.Config(), ctx.Dependencies())