courgette/internal/commands/execute_workflow.go

54 lines
1.2 KiB
Go

package commands
import (
logger "courgette/internal/logging"
runner "courgette/internal/runner"
workflow "courgette/internal/workflow"
"errors"
"fmt"
)
func ExecuteWorkflow(configuration Configuration, workflowFile string) error {
driver, err := runner.NewDriver(configuration.Containers.Driver)
if err != nil {
return err
}
runnerInstance := runner.NewRunner(
driver,
configuration.Runner.Labels,
)
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)
if !taskResult.HasError() {
return nil
}
for _, job := range taskResult.Context.Jobs {
if job.Status == "success" {
logger.Info(logger.Green("Job %s: %s"), job.Id, job.Status)
} else if job.Status == "failed" {
logger.Error(logger.Red("Job %s: %s"), job.Id, job.Status)
}
}
return fmt.Errorf("Task %s failed with at least 1 error.", taskResult.Id)
}