Skip to content

Commit

Permalink
Cleanup workspace directory if project clone failed
Browse files Browse the repository at this point in the history
  • Loading branch information
janekbaraniewski committed Jan 10, 2025
1 parent fe07d22 commit 2477422
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/agent/workspace/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/agent/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2477422

Please sign in to comment.