courgette/internal/commands/execute_workflow.go

56 lines
1.3 KiB
Go

package commands
import (
driver "courgette/internal/driver"
logger "courgette/internal/logging"
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 {
logger.Error(logger.Red("Failed to read workflow (%s)"), workflowFile)
return err
}
validationErrors := workflow.Validate()
if len(validationErrors) > 0 {
for _, err := range validationErrors {
logger.Error(logger.Red("Validation error: %s"), err)
}
return errors.New("Workflow validation failed.")
}
taskResult := runnerInstance.RunWorkflow(*workflow)
for _, job := range taskResult.Children {
if job.Status == "success" {
logger.Info(logger.Green("Job %s: %s"), job.TaskId, job.Status)
} else if job.Status == "failed" {
logger.Error(logger.Red("Job %s: %s"), job.TaskId, job.Status)
}
}
if taskResult.Failed() {
return fmt.Errorf("Task %s failed with at least 1 error.", taskResult.TaskId)
}
return nil
}