Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions agent/job_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,10 @@ func NewJobRunner(ctx context.Context, l logger.Logger, apiClient *api.Client, c
},
)

// TempDir is not guaranteed to exist
tempDir := os.TempDir()
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
// Actual file permissions will be reduced by umask, and won't be 0o777 unless the user has manually changed the umask to 000
if err = os.MkdirAll(tempDir, 0o777); err != nil {
return nil, err
}
}

// Prepare a file to receive the given job environment
file, err := os.CreateTemp(tempDir, fmt.Sprintf("job-env-%s", r.conf.Job.ID))
r.envShellFile, r.envJSONFile, err = createJobEnvFiles(r.agentLogger, r.conf.Job.ID, conf.KubernetesExec)
if err != nil {
return r, err
}
r.agentLogger.Debug("[JobRunner] Created env file (shell format): %s", file.Name())
r.envShellFile = file

file, err = os.CreateTemp(tempDir, fmt.Sprintf("job-env-json-%s", r.conf.Job.ID))
if err != nil {
return r, err
return nil, err
}
r.agentLogger.Debug("[JobRunner] Created env file (JSON format): %s", file.Name())
r.envJSONFile = file

env, err := r.createEnvironment(ctx)
if err != nil {
Expand Down Expand Up @@ -891,3 +872,35 @@ func (l jobLogger) Write(data []byte) (int, error) {
l.log.Info(msg)
return len(data), nil
}

func createJobEnvFiles(l logger.Logger, jobID string, kubernetesExec bool) (shellFile, jsonFile *os.File, err error) {
// Use /workspace in Kubernetes mode for shared volume access between containers
tempDir := os.TempDir()
if kubernetesExec {
tempDir = "/workspace"
}

// tempDir is not guaranteed to exist
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
// Actual file permissions will be reduced by umask, and won't be 0o777 unless the user has manually changed the umask to 000
if err = os.MkdirAll(tempDir, 0o777); err != nil {
return nil, nil, err
}
}

shellFile, err = os.CreateTemp(tempDir, fmt.Sprintf("job-env-%s", jobID))
if err != nil {
return nil, nil, err
}
l.Debug("[JobRunner] Created env file (shell format): %s", shellFile.Name())

jsonFile, err = os.CreateTemp(tempDir, fmt.Sprintf("job-env-json-%s", jobID))
if err != nil {
shellFile.Close()
os.Remove(shellFile.Name())
return nil, nil, err
}
l.Debug("[JobRunner] Created env file (JSON format): %s", jsonFile.Name())

return shellFile, jsonFile, nil
}