From 247742274c190826c149c177a07680e4d8322779 Mon Sep 17 00:00:00 2001 From: Jan Baraniewski Date: Fri, 10 Jan 2025 14:19:35 +0100 Subject: [PATCH] Cleanup workspace directory if project clone failed --- cmd/agent/workspace/up.go | 2 +- pkg/agent/workspace.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/agent/workspace/up.go b/cmd/agent/workspace/up.go index 9dab9e938..db7da2f41 100644 --- a/cmd/agent/workspace/up.go +++ b/cmd/agent/workspace/up.go @@ -281,7 +281,7 @@ func prepareWorkspace(ctx context.Context, workspaceInfo *provider2.AgentWorkspa } } - if workspaceInfo.CLIOptions.Recreate && !workspaceInfo.CLIOptions.Reset { + if workspaceInfo.CLIOptions.Recreate && !workspaceInfo.CLIOptions.Reset && exists { log.Info("Rebuiling without resetting a git based workspace, keeping old content folder") return nil } diff --git a/pkg/agent/workspace.go b/pkg/agent/workspace.go index 5b15dd50c..cf9c7b987 100644 --- a/pkg/agent/workspace.go +++ b/pkg/agent/workspace.go @@ -299,6 +299,10 @@ func CloneRepositoryForWorkspace( gitInfo := git.NewGitInfo(source.GitRepository, source.GitBranch, source.GitCommit, source.GitPRReference, source.GitSubPath) err := git.CloneRepositoryWithEnv(ctx, gitInfo, extraEnv, workspaceDir, helper, cloner, log) if err != nil { + // cleanup workspace dir if clone failed, otherwise we won't try to clone again when rebuilding this workspace + if cleanupErr := cleanupWorkspaceDir(workspaceDir); cleanupErr != nil { + return fmt.Errorf("clone repository: %w, cleanup workspace: %w", err, cleanupErr) + } return fmt.Errorf("clone repository: %w", err) } @@ -331,6 +335,10 @@ func getGitOptions(options provider2.CLIOptions) []git.Option { return gitOpts } +func cleanupWorkspaceDir(workspaceDir string) error { + return os.RemoveAll(workspaceDir) +} + func setupSSHKey(key string, agentPath string) ([]string, func(), error) { keyFile, err := os.CreateTemp("", "") if err != nil {