courgette/internal/commands/execute_workflow.go

57 lines
1.3 KiB
Go
Raw Permalink Normal View History

package commands
import (
driver "courgette/internal/driver"
2024-08-03 17:10:01 +00:00
logger "courgette/internal/logging"
2024-08-02 23:49:56 +00:00
runner "courgette/internal/runner"
workflow "courgette/internal/workflow"
"errors"
"fmt"
)
func ExecuteWorkflow(configuration Configuration, workflowFile string) error {
driver, err := driver.NewDriver(configuration.Containers.Driver)
if err != nil {
return err
}
runnerInstance := runner.NewRunner(
driver,
configuration.Runner.Labels,
configuration.GetCacheDir(),
)
workflow, err := workflow.FromYamlFile(workflowFile)
if err != nil {
2024-08-03 17:10:01 +00:00
logger.Error(logger.Red("Failed to read workflow (%s)"), workflowFile)
return err
}
validationErrors := workflow.Validate()
if len(validationErrors) > 0 {
for _, err := range validationErrors {
2024-08-03 17:10:01 +00:00
logger.Error(logger.Red("Validation error: %s"), err)
}
2024-08-03 17:10:01 +00:00
return errors.New("Workflow validation failed.")
}
taskResult := runnerInstance.RunWorkflow(*workflow)
for _, job := range taskResult.Children {
2024-08-03 17:10:01 +00:00
if job.Status == "success" {
logger.Info(logger.Green("Job %s: %s"), job.TaskId, job.Status)
2024-08-03 17:10:01 +00:00
} else if job.Status == "failed" {
logger.Error(logger.Red("Job %s: %s"), job.TaskId, job.Status)
2024-08-03 17:10:01 +00:00
}
}
if taskResult.Failed() {
return fmt.Errorf("Task %s failed with at least 1 error.", taskResult.TaskId)
}
return nil
}