2024-08-01 22:43:18 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
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-01 22:43:18 +00:00
|
|
|
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 {
|
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-02 20:57:56 +00:00
|
|
|
if !taskResult.HasError() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-02 02:48:54 +00:00
|
|
|
for _, job := range taskResult.Context.Jobs {
|
2024-08-03 17:10:01 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 20:57:56 +00:00
|
|
|
return fmt.Errorf("Task %s failed with at least 1 error.", taskResult.Id)
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|