Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Ported simplified logic from #129
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev committed Apr 19, 2021
1 parent f82d56d commit ce68324
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions internal/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,48 +201,42 @@ func (executor *Executor) PopulateCloneAndWorkingDirEnvironmentVariables(environ
result[key] = value
}

_, hasCloneDir := result["CIRRUS_CLONE_DIR"]
_, hasWorkingDir := result["CIRRUS_WORKING_DIR"]

if hasCloneDir && !hasWorkingDir {
// Only clone was overridden. Make sure $CIRRUS_WORKING_DIR is set
result["CIRRUS_WORKING_DIR"] = "$CIRRUS_CLONE_DIR"
}
if !hasCloneDir && hasWorkingDir {
// User did override working dir
if !strings.Contains(result["CIRRUS_WORKING_DIR"], "CIRRUS_CLONE_DIR") {
// If working dir doesn't depend on clone dir then we can default clone dir to the one provided by user
result["CIRRUS_CLONE_DIR"] = "$CIRRUS_WORKING_DIR"
hasCloneDir = true
if _, ok := result["CIRRUS_WORKING_DIR"]; !ok {
if executor.preCreatedWorkingDir != "" {
result["CIRRUS_WORKING_DIR"] = executor.preCreatedWorkingDir
} else {
result["CIRRUS_WORKING_DIR"] = makeScratchDir(executor)
}
}

if !hasCloneDir && !hasWorkingDir {
// none of the dirs are explicitly set. Make sure they'll be the same
result["CIRRUS_WORKING_DIR"] = "$CIRRUS_CLONE_DIR"
}

if !hasCloneDir && executor.preCreatedWorkingDir != "" {
// none of the dirs are explicitly set. Make sure they'll be the same
result["CIRRUS_CLONE_DIR"] = executor.preCreatedWorkingDir
}

if _, ok := result["CIRRUS_CLONE_DIR"]; !ok {
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {
result["CIRRUS_CLONE_DIR"] = filepath.ToSlash(defaultTempDirPath)
} else if executor.commandFrom != "" {
// Default folder exists and we continue execution. Therefore we need to use it.
result["CIRRUS_CLONE_DIR"] = filepath.ToSlash(defaultTempDirPath)
// Get the working directory here again after we've
// dealt with it's potential absence above
workingDir := result["CIRRUS_WORKING_DIR"]

if strings.Contains(workingDir, "CIRRUS_CLONE_DIR") {
result["CIRRUS_CLONE_DIR"] = makeScratchDir(executor)
} else {
uniqueTempDirPath, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("cirrus-task-%d", executor.taskIdentification.TaskId))
result["CIRRUS_CLONE_DIR"] = filepath.ToSlash(uniqueTempDirPath)
result["CIRRUS_CLONE_DIR"] = workingDir
}
}

return result
}

func makeScratchDir(executor *Executor) string {
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {
return filepath.ToSlash(defaultTempDirPath)
} else if executor.commandFrom != "" {
// Default folder exists and we continue execution. Therefore we need to use it.
return filepath.ToSlash(defaultTempDirPath)
} else {
uniqueTempDirPath, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("cirrus-task-%d", executor.taskIdentification.TaskId))
return filepath.ToSlash(uniqueTempDirPath)
}
}

func (executor *Executor) performStep(env map[string]string, currentStep *api.Command) error {
success := false
signaledToExit := false
Expand Down

0 comments on commit ce68324

Please sign in to comment.