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

Commit fc0f3b5

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

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

internal/executor/env.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ func ExpandTextOSFirst(text string, customEnv map[string]string) string {
2626
})
2727
}
2828

29+
func StartsWithVariable(s string, name string) bool {
30+
return variableIndex(s, name) == 0
31+
}
32+
33+
func ContainsVariable(s string, name string) bool {
34+
return variableIndex(s, name) >= 0
35+
}
36+
37+
func variableIndex(s string, name string) int {
38+
if index := strings.Index(s, "$"+name); index >= 0 {
39+
return index
40+
}
41+
42+
if index := strings.Index(s, "${"+name); index >= 0 {
43+
return index
44+
}
45+
46+
if index := strings.Index(s, "%"+name); index >= 0 {
47+
return index
48+
}
49+
50+
return -1
51+
}
52+
2953
func expandTextExtended(text string, lookup func(string) (string, bool)) string {
3054
var re = regexp.MustCompile(`%(\w+)%`)
3155
return os.Expand(re.ReplaceAllString(text, `${$1}`), func(text string) string {
@@ -56,10 +80,7 @@ func expandEnvironmentRecursively(environment map[string]string) map[string]stri
5680
originalValue := result[key]
5781
expandedValue := ExpandTextOSFirst(value, result)
5882

59-
selfRecursion := strings.Contains(expandedValue, "$"+key) ||
60-
strings.Contains(expandedValue, "${"+key) ||
61-
strings.Contains(expandedValue, "%"+key)
62-
if selfRecursion {
83+
if ContainsVariable(expandedValue, key) {
6384
// detected self-recursion
6485
continue
6586
}

internal/executor/executor.go

+33-20
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ func BoundedCommands(commands []*api.Command, fromName, toName string) []*api.Co
178178
return commands[left:right]
179179
}
180180

181+
func makeScratchDir(executor *Executor) string {
182+
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
183+
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {
184+
return filepath.ToSlash(defaultTempDirPath)
185+
} else if executor.commandFrom != "" {
186+
// Default folder exists and we continue execution. Therefore we need to use it.
187+
return filepath.ToSlash(defaultTempDirPath)
188+
} else {
189+
uniqueTempDirPath, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("cirrus-task-%d", executor.taskIdentification.TaskId))
190+
return filepath.ToSlash(uniqueTempDirPath)
191+
}
192+
}
193+
181194
func getExpandedScriptEnvironment(executor *Executor, responseEnvironment map[string]string) map[string]string {
182195
if responseEnvironment == nil {
183196
responseEnvironment = make(map[string]string)
@@ -190,23 +203,23 @@ func getExpandedScriptEnvironment(executor *Executor, responseEnvironment map[st
190203
}
191204
responseEnvironment["CIRRUS_OS"] = runtime.GOOS
192205

193-
// Use directory created by the persistent worker if CIRRUS_WORKING_DIR
194-
// 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
206+
if _, ok := responseEnvironment["CIRRUS_WORKING_DIR"]; !ok {
207+
if executor.preCreatedWorkingDir != "" {
208+
responseEnvironment["CIRRUS_WORKING_DIR"] = executor.preCreatedWorkingDir
209+
} else {
210+
responseEnvironment["CIRRUS_WORKING_DIR"] = makeScratchDir(executor)
211+
}
198212
}
199213

200-
if _, ok := responseEnvironment["CIRRUS_WORKING_DIR"]; !ok {
201-
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
202-
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {
203-
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(defaultTempDirPath)
204-
} else if executor.commandFrom != "" {
205-
// Default folder exists and we continue execution. Therefore we need to use it.
206-
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(defaultTempDirPath)
214+
if _, ok := responseEnvironment["CIRRUS_CLONE_DIR"]; !ok {
215+
// Get the working directory here again after we've dealt
216+
// with the potentially missing CIRRUS_WORKING_DIR above
217+
workingDir := responseEnvironment["CIRRUS_WORKING_DIR"]
218+
219+
if StartsWithVariable(workingDir, "CIRRUS_CLONE_DIR") {
220+
responseEnvironment["CIRRUS_CLONE_DIR"] = makeScratchDir(executor)
207221
} else {
208-
uniqueTempDirPath, _ := ioutil.TempDir(os.TempDir(), fmt.Sprintf("cirrus-task-%d", executor.taskIdentification.TaskId))
209-
responseEnvironment["CIRRUS_WORKING_DIR"] = filepath.ToSlash(uniqueTempDirPath)
222+
responseEnvironment["CIRRUS_CLONE_DIR"] = workingDir
210223
}
211224
}
212225

@@ -386,7 +399,7 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {
386399

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

389-
working_dir := env["CIRRUS_WORKING_DIR"]
402+
cloneDir := env["CIRRUS_CLONE_DIR"]
390403
change := env["CIRRUS_CHANGE_IN_REPO"]
391404
branch := env["CIRRUS_BRANCH"]
392405
pr_number, is_pr := env["CIRRUS_PR"]
@@ -426,7 +439,7 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {
426439
var repo *git.Repository
427440

428441
if is_pr {
429-
repo, err = git.PlainInit(working_dir, false)
442+
repo, err = git.PlainInit(cloneDir, false)
430443
if err != nil {
431444
logUploader.Write([]byte(fmt.Sprintf("\nFailed to init repository: %s!", err)))
432445
return false
@@ -498,13 +511,13 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {
498511
}
499512
logUploader.Write([]byte(fmt.Sprintf("\nCloning %s...\n", cloneOptions.ReferenceName)))
500513

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

503516
if err != nil && retryableCloneError(err) {
504517
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)
518+
os.RemoveAll(cloneDir)
519+
EnsureFolderExists(cloneDir)
520+
repo, err = git.PlainClone(cloneDir, false, &cloneOptions)
508521
}
509522

510523
if err != nil {

0 commit comments

Comments
 (0)