2024-08-01 22:43:18 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-08-23 03:11:00 +00:00
|
|
|
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"
|
2024-08-01 22:43:18 +00:00
|
|
|
"errors"
|
2024-08-02 20:57:56 +00:00
|
|
|
"fmt"
|
2024-08-01 22:43:18 +00:00
|
|
|
)
|
|
|
|
|
2024-08-02 21:32:41 +00:00
|
|
|
func ExecuteWorkflow(configuration Configuration, workflowFile string) error {
|
2024-08-23 03:11:00 +00:00
|
|
|
driver, err := driver.NewDriver(configuration.Containers.Driver)
|
2024-08-01 22:43:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
runnerInstance := runner.NewRunner(
|
|
|
|
driver,
|
|
|
|
configuration.Runner.Labels,
|
2024-08-31 03:09:31 +00:00
|
|
|
configuration.GetCacheDir(),
|
2024-08-01 22:43:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-01 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-03 17:10:01 +00:00
|
|
|
return errors.New("Workflow validation failed.")
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|
2024-08-02 23:40:06 +00:00
|
|
|
taskResult := runnerInstance.RunWorkflow(*workflow)
|
2024-08-01 22:43:18 +00:00
|
|
|
|
2024-08-17 04:18:53 +00:00
|
|
|
for _, job := range taskResult.Children {
|
2024-08-03 17:10:01 +00:00
|
|
|
if job.Status == "success" {
|
2024-08-17 04:18:53 +00:00
|
|
|
logger.Info(logger.Green("Job %s: %s"), job.TaskId, job.Status)
|
2024-08-03 17:10:01 +00:00
|
|
|
} else if job.Status == "failed" {
|
2024-08-17 04:18:53 +00:00
|
|
|
logger.Error(logger.Red("Job %s: %s"), job.TaskId, job.Status)
|
2024-08-03 17:10:01 +00:00
|
|
|
}
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 03:35:33 +00:00
|
|
|
if taskResult.Failed() {
|
|
|
|
return fmt.Errorf("Task %s failed with at least 1 error.", taskResult.TaskId)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|