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

Commit feae88c

Browse files
committed
Separate CIRRUS_CLONE_DIR from CIRRUS_WORKING_DIR
1 parent b348387 commit feae88c

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

cmd/agent/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func main() {
3939
stopHook := flag.Bool("stop-hook", false, "pre stop flag")
4040
commandFromPtr := flag.String("command-from", "", "Command to star execution from (inclusive)")
4141
commandToPtr := flag.String("command-to", "", "Command to stop execution at (exclusive)")
42-
preCreatedWorkingDir := flag.String("pre-created-working-dir", "",
43-
"working directory to use when spawned via Persistent Worker")
42+
preCreatedCloneDir := flag.String("pre-created-working-dir", "",
43+
"clone directory (previously working directory) to use when spawned via Persistent Worker")
4444
flag.Parse()
4545

4646
if *help {
@@ -144,7 +144,7 @@ func main() {
144144
go runHeartbeat(*taskIdPtr, *clientTokenPtr, conn)
145145

146146
buildExecutor := executor.NewExecutor(*taskIdPtr, *clientTokenPtr, *serverTokenPtr, *commandFromPtr, *commandToPtr,
147-
*preCreatedWorkingDir)
147+
*preCreatedCloneDir)
148148
buildExecutor.RunBuild()
149149

150150
logFile.Close()

internal/executor/executor.go

+37-31
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ type CommandAndLogs struct {
3434
}
3535

3636
type Executor struct {
37-
taskIdentification *api.TaskIdentification
38-
serverToken string
39-
backgroundCommands []CommandAndLogs
40-
httpCacheHost string
41-
timeout <-chan time.Time
42-
sensitiveValues []string
43-
commandFrom string
44-
commandTo string
45-
preCreatedWorkingDir string
37+
taskIdentification *api.TaskIdentification
38+
serverToken string
39+
backgroundCommands []CommandAndLogs
40+
httpCacheHost string
41+
timeout <-chan time.Time
42+
sensitiveValues []string
43+
commandFrom string
44+
commandTo string
45+
preCreatedCloneDir string
4646
}
4747

4848
var (
@@ -63,14 +63,14 @@ func NewExecutor(
6363
Secret: clientToken,
6464
}
6565
return &Executor{
66-
taskIdentification: taskIdentification,
67-
serverToken: serverToken,
68-
backgroundCommands: make([]CommandAndLogs, 0),
69-
httpCacheHost: "",
70-
sensitiveValues: make([]string, 0),
71-
commandFrom: commandFrom,
72-
commandTo: commandTo,
73-
preCreatedWorkingDir: preCreatedWorkingDir,
66+
taskIdentification: taskIdentification,
67+
serverToken: serverToken,
68+
backgroundCommands: make([]CommandAndLogs, 0),
69+
httpCacheHost: "",
70+
sensitiveValues: make([]string, 0),
71+
commandFrom: commandFrom,
72+
commandTo: commandTo,
73+
preCreatedCloneDir: preCreatedWorkingDir,
7474
}
7575
}
7676

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

193-
// Use directory created by the persistent worker if CIRRUS_WORKING_DIR
193+
// Use directory created by the persistent worker if CIRRUS_CLONE_DIR
194194
// was not overridden in the task specification by the user
195-
_, hasWorkingDir := responseEnvironment["CIRRUS_WORKING_DIR"]
196-
if !hasWorkingDir && executor.preCreatedWorkingDir != "" {
197-
responseEnvironment["CIRRUS_WORKING_DIR"] = executor.preCreatedWorkingDir
195+
_, hasWorkingDir := responseEnvironment["CIRRUS_CLONE_DIR"]
196+
if !hasWorkingDir && executor.preCreatedCloneDir != "" {
197+
responseEnvironment["CIRRUS_CLONE_DIR"] = executor.preCreatedCloneDir
198198
}
199199

200-
if _, ok := responseEnvironment["CIRRUS_WORKING_DIR"]; !ok {
200+
// Set CIRRUS_CLONE_DIR if not already set by the user (1) or by us above (2)
201+
if _, ok := responseEnvironment["CIRRUS_CLONE_DIR"]; !ok {
201202
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
202203
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {
203-
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(defaultTempDirPath)
204+
responseEnvironment["CIRRUS_CLONE_DIR"] = filepath.ToSlash(defaultTempDirPath)
204205
} else if executor.commandFrom != "" {
205206
// Default folder exists and we continue execution. Therefore we need to use it.
206-
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(defaultTempDirPath)
207+
responseEnvironment["CIRRUS_CLONE_DIR"] = filepath.ToSlash(defaultTempDirPath)
207208
} else {
208209
uniqueTempDirPath, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("cirrus-task-%d", executor.taskIdentification.TaskId))
209-
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(uniqueTempDirPath)
210+
responseEnvironment["CIRRUS_CLONE_DIR"] = filepath.ToSlash(uniqueTempDirPath)
210211
}
211212
}
212213

214+
// Set CIRRUS_WORKING_DIR if not already set by the user (1) or by us above (2)
215+
if _, ok := responseEnvironment["CIRRUS_WORKING_DIR"]; !ok {
216+
responseEnvironment["CIRRUS_WORKING_DIR"] = "${CIRRUS_CLONE_DIR}"
217+
}
218+
213219
result := expandEnvironmentRecursively(responseEnvironment)
214220

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

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

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

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

501-
repo, err = git.PlainClone(working_dir, false, &cloneOptions)
507+
repo, err = git.PlainClone(cloneDir, false, &cloneOptions)
502508

503509
if err != nil && retryableCloneError(err) {
504510
logUploader.Write([]byte(fmt.Sprintf("\nRetryable error '%s' while cloning! Trying again...", err)))
505-
os.RemoveAll(working_dir)
506-
EnsureFolderExists(working_dir)
507-
repo, err = git.PlainClone(working_dir, false, &cloneOptions)
511+
os.RemoveAll(cloneDir)
512+
EnsureFolderExists(cloneDir)
513+
repo, err = git.PlainClone(cloneDir, false, &cloneOptions)
508514
}
509515

510516
if err != nil {

0 commit comments

Comments
 (0)