diff --git a/pkg/skaffold/runner/runcontext/v2/context.go b/pkg/skaffold/runner/runcontext/v2/context.go index b80e88fa20c..b7aa5da8634 100644 --- a/pkg/skaffold/runner/runcontext/v2/context.go +++ b/pkg/skaffold/runner/runcontext/v2/context.go @@ -19,6 +19,7 @@ package v2 import ( "fmt" "os" + "path/filepath" "github.com/google/uuid" "github.com/sirupsen/logrus" @@ -212,10 +213,14 @@ func GetRunContext(opts config.SkaffoldOptions, configs []*latestV2.SkaffoldConf kubeContext := kubeConfig.CurrentContext logrus.Infof("Using kubectl context: %s", kubeContext) - // TODO(dgageot): this should be the folder containing skaffold.yaml. Should also be moved elsewhere. - cwd, err := os.Getwd() - if err != nil { - return nil, fmt.Errorf("finding current directory: %w", err) + var cwd string + if opts.ConfigurationFile == "" { + cwd, err = os.Getwd() + if err != nil { + return nil, fmt.Errorf("finding current directory: %w", err) + } + } else { + cwd = filepath.Dir(opts.ConfigurationFile) } namespaces, err := runnerutil.GetAllPodNamespaces(opts.Namespace, pipelines) diff --git a/pkg/skaffold/runner/runcontext/v2/context_test.go b/pkg/skaffold/runner/runcontext/v2/context_test.go index 53555a24848..936cfbc3f46 100644 --- a/pkg/skaffold/runner/runcontext/v2/context_test.go +++ b/pkg/skaffold/runner/runcontext/v2/context_test.go @@ -17,8 +17,14 @@ limitations under the License. package v2 import ( + "fmt" + "os" + "path/filepath" "testing" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" + latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" + latestV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v2" "github.com/GoogleContainerTools/skaffold/testutil" ) @@ -89,3 +95,25 @@ func TestRunContext_UpdateNamespaces(t *testing.T) { }) } } + +func TestGetRunContextDefaultWorkdir(t *testing.T) { + testutil.Run(t, "default workdir", func(t *testutil.T) { + rctx, err := GetRunContext(config.SkaffoldOptions{}, []*latestV2.SkaffoldConfig{}) + pwd, _ := os.Getwd() + t.CheckDeepEqual(pwd, rctx.WorkingDir) + t.CheckNoError(err) + }) +} + +func TestGetRunContextCustomWorkdir(t *testing.T) { + testutil.Run(t, "default workdir", func(t *testutil.T) { + tmpDir := t.NewTempDir() + tmpDir.Write("skaffold.yaml", fmt.Sprintf("apiVersion: %s\nkind: Config", latestV1.Version)). + Chdir() + rctx, err := GetRunContext(config.SkaffoldOptions{ + ConfigurationFile: filepath.Join(tmpDir.Root(), "skaffold.yaml"), + }, []*latestV2.SkaffoldConfig{}) + t.CheckDeepEqual(tmpDir.Root(), rctx.WorkingDir) + t.CheckNoError(err) + }) +}