Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions e2e/tests/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ var _ = DevSpaceDescribe("hooks", func() {
})
framework.ExpectNoError(err)

// check namespace hook
framework.ExpectLocalFileContentsImmediately("namespace.txt", ns)

// stop second command
cancel2()

Expand Down
3 changes: 3 additions & 0 deletions e2e/tests/hooks/testdata/once/devspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pkg/devspace/hook/local_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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())
Expand Down