Skip to content

Commit 1d04779

Browse files
authored
Merge pull request #2083 from FabianKramm/master
feat: add devspace vars as environment variables
2 parents 98c91ca + 47a8b25 commit 1d04779

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

e2e/tests/hooks/hooks.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ var _ = DevSpaceDescribe("hooks", func() {
219219
})
220220
framework.ExpectNoError(err)
221221

222+
// check namespace hook
223+
framework.ExpectLocalFileContentsImmediately("namespace.txt", ns)
224+
222225
// stop second command
223226
cancel2()
224227

e2e/tests/hooks/testdata/once/devspace.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ deployments:
1212
command: ["sleep"]
1313
args: ["999999999999"]
1414
hooks:
15+
- command: |
16+
echo -n $DEVSPACE_NAMESPACE > namespace.txt
17+
events: ["after:deploy"]
1518
- command: |
1619
mkdir -p /app
1720
echo -n $RANDOM > /app/once.out

pkg/devspace/hook/local_command.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
runtimevar "github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/runtime"
89
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
910
"github.com/loft-sh/devspace/pkg/devspace/pipeline/engine"
1011
"io"
1112
"os"
13+
"regexp"
1214
"strings"
1315

1416
"github.com/loft-sh/devspace/pkg/devspace/config"
@@ -30,6 +32,8 @@ type localCommandHook struct {
3032
Stderr io.Writer
3133
}
3234

35+
var EnvironmentVariableRegEx = regexp.MustCompile(`^[A-Za-z0-9_]+$`)
36+
3337
func (l *localCommandHook) Execute(ctx devspacecontext.Context, hook *latest.HookConfig, cmdExtraEnv map[string]string) error {
3438
// Create extra env variables
3539
osArgsBytes, err := json.Marshal(os.Args)
@@ -46,6 +50,13 @@ func (l *localCommandHook) Execute(ctx devspacecontext.Context, hook *latest.Hoo
4650
for k, v := range cmdExtraEnv {
4751
extraEnv[k] = v
4852
}
53+
for k, v := range ctx.Config().Variables() {
54+
if !EnvironmentVariableRegEx.MatchString(k) {
55+
continue
56+
}
57+
58+
extraEnv[k] = fmt.Sprintf("%v", v)
59+
}
4960

5061
// resolve hook command and args
5162
hookCommand, hookArgs, err := ResolveCommand(ctx.Context(), hook.Command, hook.Args, ctx.WorkingDir(), ctx.Config(), ctx.Dependencies())

0 commit comments

Comments
 (0)