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

Commit f88afe3

Browse files
authored
Remove -clean-working-dir and introduce -pre-created-working-dir flag (#120)
1 parent 69c1fad commit f88afe3

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

cmd/agent/main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +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-
cleanWorkingDir := flag.Bool("clean-working-dir", false, "clean working directory before exit")
42+
preCreatedWorkingDir := flag.String("pre-created-working-dir", "",
43+
"working directory to use when spawned via Persistent Worker")
4344
flag.Parse()
4445

4546
if *help {
@@ -142,7 +143,8 @@ func main() {
142143

143144
go runHeartbeat(*taskIdPtr, *clientTokenPtr, conn)
144145

145-
buildExecutor := executor.NewExecutor(*taskIdPtr, *clientTokenPtr, *serverTokenPtr, *commandFromPtr, *commandToPtr, *cleanWorkingDir)
146+
buildExecutor := executor.NewExecutor(*taskIdPtr, *clientTokenPtr, *serverTokenPtr, *commandFromPtr, *commandToPtr,
147+
*preCreatedWorkingDir)
146148
buildExecutor.RunBuild()
147149

148150
logFile.Close()

internal/executor/executor.go

+32-24
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,38 @@ 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-
cleanWorkingDir bool
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
4646
}
4747

48-
func NewExecutor(taskId int64, clientToken, serverToken string, commandFrom string, commandTo string, cleanWorkingDir bool) *Executor {
48+
func NewExecutor(
49+
taskId int64,
50+
clientToken,
51+
serverToken string,
52+
commandFrom string,
53+
commandTo string,
54+
preCreatedWorkingDir string,
55+
) *Executor {
4956
taskIdentification := &api.TaskIdentification{
5057
TaskId: taskId,
5158
Secret: clientToken,
5259
}
5360
return &Executor{
54-
taskIdentification: taskIdentification,
55-
serverToken: serverToken,
56-
backgroundCommands: make([]CommandAndLogs, 0),
57-
httpCacheHost: "",
58-
sensitiveValues: make([]string, 0),
59-
commandFrom: commandFrom,
60-
commandTo: commandTo,
61-
cleanWorkingDir: cleanWorkingDir,
61+
taskIdentification: taskIdentification,
62+
serverToken: serverToken,
63+
backgroundCommands: make([]CommandAndLogs, 0),
64+
httpCacheHost: "",
65+
sensitiveValues: make([]string, 0),
66+
commandFrom: commandFrom,
67+
commandTo: commandTo,
68+
preCreatedWorkingDir: preCreatedWorkingDir,
6269
}
6370
}
6471

@@ -141,12 +148,6 @@ func (executor *Executor) RunBuild() {
141148
}
142149
backgroundCommand.Logs.Finalize()
143150
}
144-
if executor.cleanWorkingDir {
145-
err = os.RemoveAll(workingDir)
146-
if err != nil {
147-
log.Printf("Failed to clean working directory: %v", err)
148-
}
149-
}
150151
}
151152

152153
// BoundedCommands bounds a slice of commands with unique names to a half-open range [fromName, toName).
@@ -178,6 +179,13 @@ func getExpandedScriptEnvironment(executor *Executor, responseEnvironment map[st
178179
}
179180
responseEnvironment["CIRRUS_OS"] = runtime.GOOS
180181

182+
// Use directory created by the persistent worker if CIRRUS_WORKING_DIR
183+
// was not overridden in the task specification by the user
184+
_, needsWorkingDir := responseEnvironment["CIRRUS_WORKING_DIR"]
185+
if needsWorkingDir && executor.preCreatedWorkingDir != "" {
186+
responseEnvironment["CIRRUS_WORKING_DIR"] = executor.preCreatedWorkingDir
187+
}
188+
181189
if _, ok := responseEnvironment["CIRRUS_WORKING_DIR"]; !ok {
182190
defaultTempDirPath := filepath.Join(os.TempDir(), "cirrus-ci-build")
183191
if _, err := os.Stat(defaultTempDirPath); os.IsNotExist(err) {

0 commit comments

Comments
 (0)