2022-02-08 11:22:41 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jobInfo interface {
|
|
|
|
matrix() map[string]interface{}
|
|
|
|
steps() []*model.Step
|
|
|
|
startContainer() common.Executor
|
|
|
|
stopContainer() common.Executor
|
|
|
|
closeContainer() common.Executor
|
|
|
|
interpolateOutputs() common.Executor
|
|
|
|
result(result string)
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executor {
|
2022-02-08 11:22:41 -06:00
|
|
|
steps := make([]common.Executor, 0)
|
2022-03-22 16:13:00 -05:00
|
|
|
preSteps := make([]common.Executor, 0)
|
2022-05-24 08:36:06 -05:00
|
|
|
var postExecutor common.Executor
|
2022-02-08 11:22:41 -06:00
|
|
|
|
|
|
|
steps = append(steps, func(ctx context.Context) error {
|
|
|
|
if len(info.matrix()) > 0 {
|
|
|
|
common.Logger(ctx).Infof("\U0001F9EA Matrix: %v", info.matrix())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
infoSteps := info.steps()
|
2022-02-08 11:22:41 -06:00
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
if len(infoSteps) == 0 {
|
|
|
|
return common.NewDebugExecutor("No steps found")
|
|
|
|
}
|
|
|
|
|
|
|
|
preSteps = append(preSteps, info.startContainer())
|
|
|
|
|
|
|
|
for i, stepModel := range infoSteps {
|
2022-05-12 14:23:34 -05:00
|
|
|
if stepModel == nil {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return fmt.Errorf("invalid Step %v: missing run or uses key", i)
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
if stepModel.ID == "" {
|
|
|
|
stepModel.ID = fmt.Sprintf("%d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
step, err := sf.newStep(stepModel, rc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return common.NewErrorExecutor(err)
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
preSteps = append(preSteps, step.pre())
|
|
|
|
|
|
|
|
stepExec := step.main()
|
2022-02-08 11:22:41 -06:00
|
|
|
steps = append(steps, func(ctx context.Context) error {
|
2022-03-22 16:13:00 -05:00
|
|
|
stepName := stepModel.String()
|
2022-03-14 12:38:30 -05:00
|
|
|
return (func(ctx context.Context) error {
|
|
|
|
err := stepExec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
common.Logger(ctx).Errorf("%v", err)
|
|
|
|
common.SetJobError(ctx, err)
|
|
|
|
} else if ctx.Err() != nil {
|
|
|
|
common.Logger(ctx).Errorf("%v", ctx.Err())
|
|
|
|
common.SetJobError(ctx, ctx.Err())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})(withStepLogger(ctx, stepName))
|
2022-02-08 11:22:41 -06:00
|
|
|
})
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
// run the post exector in reverse order
|
|
|
|
if postExecutor != nil {
|
|
|
|
postExecutor = step.post().Finally(postExecutor)
|
|
|
|
} else {
|
|
|
|
postExecutor = step.post()
|
|
|
|
}
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|
2022-02-10 10:54:58 -06:00
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
postExecutor = postExecutor.Finally(func(ctx context.Context) error {
|
2022-02-08 11:22:41 -06:00
|
|
|
jobError := common.JobError(ctx)
|
|
|
|
if jobError != nil {
|
|
|
|
info.result("failure")
|
|
|
|
} else {
|
2022-03-14 14:46:32 -05:00
|
|
|
err := info.stopContainer()(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-08 11:22:41 -06:00
|
|
|
info.result("success")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
pipeline := make([]common.Executor, 0)
|
|
|
|
pipeline = append(pipeline, preSteps...)
|
|
|
|
pipeline = append(pipeline, steps...)
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
return common.NewPipelineExecutor(pipeline...).
|
|
|
|
Finally(postExecutor).
|
|
|
|
Finally(info.interpolateOutputs()).
|
|
|
|
Finally(info.closeContainer())
|
2022-02-08 11:22:41 -06:00
|
|
|
}
|