feat: add working-directory, defaults.run support

This commit is contained in:
Marc 2024-08-17 23:58:54 -04:00
parent 4ef95277e8
commit 673c20cc30
Signed by: marc
GPG key ID: 048E042F22B5DC79
5 changed files with 18 additions and 15 deletions

View file

@ -21,7 +21,9 @@ syntax](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for
- [ ] jobs.<job_id>.concurrency
- [ ] jobs.<job_id>.outputs
- [ ] jobs.<job_id>.env
- [ ] jobs.<job_id>.defaults
- [x] jobs.<job_id>.defaults
- [ ] jobs.<job_id>.run.shell
- [x] jobs.<job_id>.run.working-directory
- [ ] jobs.<job_id>.timeout-minutes
- [ ] jobs.<job_id>.strategy
- [ ] jobs.<job_id>.container
@ -37,7 +39,7 @@ syntax](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for
- [ ] jobs.<job_id>.steps[*].name
- [ ] jobs.<job_id>.steps[*].uses
- [x] jobs.<job_id>.steps[*].run
- [ ] jobs.<job_id>.steps[*].working-directory
- [x] jobs.<job_id>.steps[*].working-directory
- [ ] jobs.<job_id>.steps[*].shell
- [ ] jobs.<job_id>.steps[*].with
- [ ] jobs.<job_id>.steps[*].env

View file

@ -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.

View file

@ -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

View file

@ -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 {

View file

@ -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.")