@@ -178,6 +178,19 @@ func BoundedCommands(commands []*api.Command, fromName, toName string) []*api.Co
178
178
return commands [left :right ]
179
179
}
180
180
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
+
181
194
func getExpandedScriptEnvironment (executor * Executor , responseEnvironment map [string ]string ) map [string ]string {
182
195
if responseEnvironment == nil {
183
196
responseEnvironment = make (map [string ]string )
@@ -190,23 +203,23 @@ func getExpandedScriptEnvironment(executor *Executor, responseEnvironment map[st
190
203
}
191
204
responseEnvironment ["CIRRUS_OS" ] = runtime .GOOS
192
205
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
+ }
198
212
}
199
213
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 )
207
221
} 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
210
223
}
211
224
}
212
225
@@ -386,7 +399,7 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {
386
399
387
400
logUploader .Write ([]byte ("Using built-in Git...\n " ))
388
401
389
- working_dir := env ["CIRRUS_WORKING_DIR " ]
402
+ cloneDir := env ["CIRRUS_CLONE_DIR " ]
390
403
change := env ["CIRRUS_CHANGE_IN_REPO" ]
391
404
branch := env ["CIRRUS_BRANCH" ]
392
405
pr_number , is_pr := env ["CIRRUS_PR" ]
@@ -426,7 +439,7 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {
426
439
var repo * git.Repository
427
440
428
441
if is_pr {
429
- repo , err = git .PlainInit (working_dir , false )
442
+ repo , err = git .PlainInit (cloneDir , false )
430
443
if err != nil {
431
444
logUploader .Write ([]byte (fmt .Sprintf ("\n Failed to init repository: %s!" , err )))
432
445
return false
@@ -498,13 +511,13 @@ func (executor *Executor) CloneRepository(env map[string]string) bool {
498
511
}
499
512
logUploader .Write ([]byte (fmt .Sprintf ("\n Cloning %s...\n " , cloneOptions .ReferenceName )))
500
513
501
- repo , err = git .PlainClone (working_dir , false , & cloneOptions )
514
+ repo , err = git .PlainClone (cloneDir , false , & cloneOptions )
502
515
503
516
if err != nil && retryableCloneError (err ) {
504
517
logUploader .Write ([]byte (fmt .Sprintf ("\n Retryable 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 )
508
521
}
509
522
510
523
if err != nil {
0 commit comments