From 673c20cc303f0f83d9dd191ce8d5806d32dce563 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sat, 17 Aug 2024 23:58:54 -0400 Subject: [PATCH] feat: add working-directory, defaults.run support --- WORKFLOW_SUPPORT.md | 6 ++++-- internal/runner/driver.go | 2 +- internal/runner/podman_driver.go | 4 ++-- internal/runner/runner.go | 13 +++++++------ internal/runner/runner_flow_test.go | 8 ++++---- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/WORKFLOW_SUPPORT.md b/WORKFLOW_SUPPORT.md index a8eb50f..4a6e295 100644 --- a/WORKFLOW_SUPPORT.md +++ b/WORKFLOW_SUPPORT.md @@ -21,7 +21,9 @@ syntax](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for - [ ] jobs..concurrency - [ ] jobs..outputs - [ ] jobs..env - - [ ] jobs..defaults + - [x] jobs..defaults + - [ ] jobs..run.shell + - [x] jobs..run.working-directory - [ ] jobs..timeout-minutes - [ ] jobs..strategy - [ ] jobs..container @@ -37,7 +39,7 @@ syntax](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for - [ ] jobs..steps[*].name - [ ] jobs..steps[*].uses - [x] jobs..steps[*].run - - [ ] jobs..steps[*].working-directory + - [x] jobs..steps[*].working-directory - [ ] jobs..steps[*].shell - [ ] jobs..steps[*].with - [ ] jobs..steps[*].env diff --git a/internal/runner/driver.go b/internal/runner/driver.go index 4713075..f92af5c 100644 --- a/internal/runner/driver.go +++ b/internal/runner/driver.go @@ -8,7 +8,7 @@ type ContainerDriver interface { Pull(string) error Start(string, string) error Stop(string) error - Exec(string, string) CommandResult + Exec(containerId string, command string, cwd string) CommandResult } // Represents the outcome of a command call made by the driver. diff --git a/internal/runner/podman_driver.go b/internal/runner/podman_driver.go index 2269da8..bd9edff 100644 --- a/internal/runner/podman_driver.go +++ b/internal/runner/podman_driver.go @@ -79,8 +79,8 @@ func (d PodmanDriver) Stop(containerName string) error { return nil } -func (d PodmanDriver) Exec(containerId string, command string) CommandResult { - cmd := exec.Command("podman", "exec", containerId, "bash", "-c", command) +func (d PodmanDriver) Exec(containerId string, command string, cwd string) CommandResult { + cmd := exec.Command("podman", "exec", "--workdir", cwd, containerId, "bash", "-c", command) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr diff --git a/internal/runner/runner.go b/internal/runner/runner.go index db6384c..cf36cb6 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -52,8 +52,8 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker { jobContext := context.WithValue(workflowContext, "currentJob", job) jobContext = context.WithValue(jobContext, "runnerImageUri", runnerImage) - // Runs a given job (provided a runner to run it on, its context, a task tracker for progress monitoring and - // a WaitGroup to coordinate concurrent tasks) and updates the tracker with results. + // Runs a given job (provided a runner to run it on, its context, a task tracker for progress monitoring and + // a WaitGroup to coordinate concurrent tasks) and updates the tracker with results. runJob := func(runner *Runner, jobContext context.Context, jobTracker *TaskTracker, jobWaitGroup *sync.WaitGroup) { defer jobWaitGroup.Done() jobTracker.SetStatus("started") @@ -92,8 +92,8 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker { // // If the command raises an error while in the container or fails to run // the command at all, an error is returned, otherwise nil. -func (r *Runner) RunCommandInContainer(containerId string, command string) error { - result := r.Driver.Exec(containerId, command) +func (r *Runner) RunCommandInContainer(containerId string, command string, stepCwd string) error { + result := r.Driver.Exec(containerId, command, stepCwd) if result.Error != nil { return result.Error @@ -121,12 +121,13 @@ func (r *Runner) RunJobInContainer(imageUri string, containerId string, jobConte logger.Info("Started %s", containerId) for stepIndex, step := range job.Steps { + stepCwd := jobContext.Value("workflow").(workflow.Workflow).GetWorkingDirectory(job.Name, stepIndex) logger.Info("Run: %s", step.Run) - logger.Info("Using working directory %s", jobContext.Value("workflow").(workflow.Workflow).GetWorkingDirectory(job.Name, stepIndex)) + logger.Info("Using working directory %s", stepCwd) var stepError error if step.Run != "" { - stepError = r.RunCommandInContainer(containerId, step.Run) + stepError = r.RunCommandInContainer(containerId, step.Run, stepCwd) } if stepError != nil { diff --git a/internal/runner/runner_flow_test.go b/internal/runner/runner_flow_test.go index ec8d36d..d225bda 100644 --- a/internal/runner/runner_flow_test.go +++ b/internal/runner/runner_flow_test.go @@ -59,11 +59,11 @@ func (d *MockDriver) Stop(uri string) error { } -func (d *MockDriver) Exec(containerName string, command string) CommandResult { +func (d *MockDriver) Exec(containerName string, command string, cwd string) CommandResult { if _, init := d.calls["Exec"]; !init { d.calls["Exec"] = []MockCall{} } - d.calls["Exec"] = append(d.calls["Exec"], MockCall{fname: "Exec", args: []string{containerName, command}}) + d.calls["Exec"] = append(d.calls["Exec"], MockCall{fname: "Exec", args: []string{containerName, command, cwd}}) if d.mockResult != nil { return *d.mockResult @@ -80,7 +80,7 @@ func TestRunnerRunCommandInContainerReturnsErrorFromDriver(t *testing.T) { Driver: &mockDriver, } - err := runner.RunCommandInContainer("test-container", "test-command") + err := runner.RunCommandInContainer("test-container", "test-command", ".") if err == nil { t.Errorf("Expected error, got nil.") @@ -95,7 +95,7 @@ func TestRunnerRunCommandInContainerReturnsErrorIfCommandExitCodeNonzero(t *test Driver: &mockDriver, } - err := runner.RunCommandInContainer("test-container", "test-command") + err := runner.RunCommandInContainer("test-container", "test-command", ".") if err == nil { t.Errorf("Expected error, got nil.")