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

Commit

Permalink
Separate CIRRUS_CLONE_DIR from CIRRUS_WORKING_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev committed Apr 19, 2021
1 parent b348387 commit feae88c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
6 changes: 3 additions & 3 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func main() {
stopHook := flag.Bool("stop-hook", false, "pre stop flag")
commandFromPtr := flag.String("command-from", "", "Command to star execution from (inclusive)")
commandToPtr := flag.String("command-to", "", "Command to stop execution at (exclusive)")
preCreatedWorkingDir := flag.String("pre-created-working-dir", "",
"working directory to use when spawned via Persistent Worker")
preCreatedCloneDir := flag.String("pre-created-working-dir", "",
"clone directory (previously working directory) to use when spawned via Persistent Worker")
flag.Parse()

if *help {
Expand Down Expand Up @@ -144,7 +144,7 @@ func main() {
go runHeartbeat(*taskIdPtr, *clientTokenPtr, conn)

buildExecutor := executor.NewExecutor(*taskIdPtr, *clientTokenPtr, *serverTokenPtr, *commandFromPtr, *commandToPtr,
*preCreatedWorkingDir)
*preCreatedCloneDir)
buildExecutor.RunBuild()

logFile.Close()
Expand Down
68 changes: 37 additions & 31 deletions internal/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ type CommandAndLogs struct {
}

type Executor struct {
taskIdentification *api.TaskIdentification
serverToken string
backgroundCommands []CommandAndLogs
httpCacheHost string
timeout <-chan time.Time
sensitiveValues []string
commandFrom string
commandTo string
preCreatedWorkingDir string
taskIdentification *api.TaskIdentification
serverToken string
backgroundCommands []CommandAndLogs
httpCacheHost string
timeout <-chan time.Time
sensitiveValues []string
commandFrom string
commandTo string
preCreatedCloneDir string
}

var (
Expand All @@ -63,14 +63,14 @@ func NewExecutor(
Secret: clientToken,
}
return &Executor{
taskIdentification: taskIdentification,
serverToken: serverToken,
backgroundCommands: make([]CommandAndLogs, 0),
httpCacheHost: "",
sensitiveValues: make([]string, 0),
commandFrom: commandFrom,
commandTo: commandTo,
preCreatedWorkingDir: preCreatedWorkingDir,
taskIdentification: taskIdentification,
serverToken: serverToken,
backgroundCommands: make([]CommandAndLogs, 0),
httpCacheHost: "",
sensitiveValues: make([]string, 0),
commandFrom: commandFrom,
commandTo: commandTo,
preCreatedCloneDir: preCreatedWorkingDir,
}
}

Expand Down Expand Up @@ -190,26 +190,32 @@ func getExpandedScriptEnvironment(executor *Executor, responseEnvironment map[st
}
responseEnvironment["CIRRUS_OS"] = runtime.GOOS

// Use directory created by the persistent worker if CIRRUS_WORKING_DIR
// Use directory created by the persistent worker if CIRRUS_CLONE_DIR
// was not overridden in the task specification by the user
_, hasWorkingDir := responseEnvironment["CIRRUS_WORKING_DIR"]
if !hasWorkingDir && executor.preCreatedWorkingDir != "" {
responseEnvironment["CIRRUS_WORKING_DIR"] = executor.preCreatedWorkingDir
_, hasWorkingDir := responseEnvironment["CIRRUS_CLONE_DIR"]
if !hasWorkingDir && executor.preCreatedCloneDir != "" {
responseEnvironment["CIRRUS_CLONE_DIR"] = executor.preCreatedCloneDir
}

if _, ok := responseEnvironment["CIRRUS_WORKING_DIR"]; !ok {
// Set CIRRUS_CLONE_DIR if not already set by the user (1) or by us above (2)
if _, ok := responseEnvironment["CIRRUS_CLONE_DIR"]; !ok {
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(defaultTempDirPath)
responseEnvironment["CIRRUS_CLONE_DIR"] = filepath.ToSlash(defaultTempDirPath)
} else if executor.commandFrom != "" {
// Default folder exists and we continue execution. Therefore we need to use it.
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(defaultTempDirPath)
responseEnvironment["CIRRUS_CLONE_DIR"] = filepath.ToSlash(defaultTempDirPath)
} else {
uniqueTempDirPath, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("cirrus-task-%d", executor.taskIdentification.TaskId))
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(uniqueTempDirPath)
responseEnvironment["CIRRUS_CLONE_DIR"] = filepath.ToSlash(uniqueTempDirPath)
}
}

// Set CIRRUS_WORKING_DIR if not already set by the user (1) or by us above (2)
if _, ok := responseEnvironment["CIRRUS_WORKING_DIR"]; !ok {
responseEnvironment["CIRRUS_WORKING_DIR"] = "${CIRRUS_CLONE_DIR}"
}

result := expandEnvironmentRecursively(responseEnvironment)

return result
Expand Down Expand Up @@ -386,7 +392,7 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {

logUploader.Write([]byte("Using built-in Git...\n"))

working_dir := env["CIRRUS_WORKING_DIR"]
cloneDir := env["CIRRUS_CLONE_DIR"]
change := env["CIRRUS_CHANGE_IN_REPO"]
branch := env["CIRRUS_BRANCH"]
pr_number, is_pr := env["CIRRUS_PR"]
Expand Down Expand Up @@ -426,7 +432,7 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {
var repo *git.Repository

if is_pr {
repo, err = git.PlainInit(working_dir, false)
repo, err = git.PlainInit(cloneDir, false)
if err != nil {
logUploader.Write([]byte(fmt.Sprintf("\nFailed to init repository: %s!", err)))
return false
Expand Down Expand Up @@ -498,13 +504,13 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {
}
logUploader.Write([]byte(fmt.Sprintf("\nCloning %s...\n", cloneOptions.ReferenceName)))

repo, err = git.PlainClone(working_dir, false, &cloneOptions)
repo, err = git.PlainClone(cloneDir, false, &cloneOptions)

if err != nil && retryableCloneError(err) {
logUploader.Write([]byte(fmt.Sprintf("\nRetryable error '%s' while cloning! Trying again...", err)))
os.RemoveAll(working_dir)
EnsureFolderExists(working_dir)
repo, err = git.PlainClone(working_dir, false, &cloneOptions)
os.RemoveAll(cloneDir)
EnsureFolderExists(cloneDir)
repo, err = git.PlainClone(cloneDir, false, &cloneOptions)
}

if err != nil {
Expand Down

0 comments on commit feae88c

Please sign in to comment.