2022-02-09 01:22:41 +08:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-05-24 22:52:25 +08:00
|
|
|
"time"
|
2022-02-09 01:22:41 +08:00
|
|
|
|
|
|
|
"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-23 05:13:00 +08:00
|
|
|
func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executor {
|
2022-02-09 01:22:41 +08:00
|
|
|
steps := make([]common.Executor, 0)
|
2022-03-23 05:13:00 +08:00
|
|
|
preSteps := make([]common.Executor, 0)
|
2022-05-24 21:36:06 +08:00
|
|
|
var postExecutor common.Executor
|
2022-02-09 01:22:41 +08:00
|
|
|
|
|
|
|
steps = append(steps, func(ctx context.Context) error {
|
2022-06-17 23:55:21 +08:00
|
|
|
logger := common.Logger(ctx)
|
2022-02-09 01:22:41 +08:00
|
|
|
if len(info.matrix()) > 0 {
|
2022-06-17 23:55:21 +08:00
|
|
|
logger.Infof("\U0001F9EA Matrix: %v", info.matrix())
|
2022-02-09 01:22:41 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-03-23 05:13:00 +08:00
|
|
|
infoSteps := info.steps()
|
2022-02-09 01:22:41 +08:00
|
|
|
|
2022-03-23 05:13:00 +08:00
|
|
|
if len(infoSteps) == 0 {
|
|
|
|
return common.NewDebugExecutor("No steps found")
|
|
|
|
}
|
|
|
|
|
|
|
|
preSteps = append(preSteps, info.startContainer())
|
|
|
|
|
|
|
|
for i, stepModel := range infoSteps {
|
2022-06-17 23:55:21 +08:00
|
|
|
stepModel := stepModel
|
2022-05-13 03:23:34 +08:00
|
|
|
if stepModel == nil {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return fmt.Errorf("invalid Step %v: missing run or uses key", i)
|
|
|
|
}
|
|
|
|
}
|
2022-03-23 05:13:00 +08: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-09 01:22:41 +08:00
|
|
|
}
|
2022-03-23 05:13:00 +08:00
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
preSteps = append(preSteps, useStepLogger(rc, stepModel, stepStagePre, step.pre()))
|
2022-03-23 05:13:00 +08:00
|
|
|
|
|
|
|
stepExec := step.main()
|
2022-06-17 23:55:21 +08:00
|
|
|
steps = append(steps, useStepLogger(rc, stepModel, stepStageMain, func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
err := stepExec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("%v", err)
|
|
|
|
common.SetJobError(ctx, err)
|
|
|
|
} else if ctx.Err() != nil {
|
|
|
|
logger.Errorf("%v", ctx.Err())
|
|
|
|
common.SetJobError(ctx, ctx.Err())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}))
|
2022-03-23 05:13:00 +08:00
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
postExec := useStepLogger(rc, stepModel, stepStagePost, step.post())
|
2022-05-24 21:36:06 +08:00
|
|
|
if postExecutor != nil {
|
2022-06-17 23:55:21 +08:00
|
|
|
// run the post exector in reverse order
|
|
|
|
postExecutor = postExec.Finally(postExecutor)
|
2022-05-24 21:36:06 +08:00
|
|
|
} else {
|
2022-06-17 23:55:21 +08:00
|
|
|
postExecutor = postExec
|
2022-05-24 21:36:06 +08:00
|
|
|
}
|
2022-02-09 01:22:41 +08:00
|
|
|
}
|
2022-02-11 00:54:58 +08:00
|
|
|
|
2022-05-24 21:36:06 +08:00
|
|
|
postExecutor = postExecutor.Finally(func(ctx context.Context) error {
|
2022-06-17 23:55:21 +08:00
|
|
|
logger := common.Logger(ctx)
|
2022-02-09 01:22:41 +08:00
|
|
|
jobError := common.JobError(ctx)
|
|
|
|
if jobError != nil {
|
|
|
|
info.result("failure")
|
2022-06-17 23:55:21 +08:00
|
|
|
logger.WithField("jobResult", "failure").Infof("\U0001F3C1 Job failed")
|
2022-02-09 01:22:41 +08:00
|
|
|
} else {
|
2022-03-15 03:46:32 +08:00
|
|
|
err := info.stopContainer()(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-09 01:22:41 +08:00
|
|
|
info.result("success")
|
2022-06-17 23:55:21 +08:00
|
|
|
logger.WithField("jobResult", "success").Infof("\U0001F3C1 Job succeeded")
|
2022-02-09 01:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-03-23 05:13:00 +08:00
|
|
|
pipeline := make([]common.Executor, 0)
|
|
|
|
pipeline = append(pipeline, preSteps...)
|
|
|
|
pipeline = append(pipeline, steps...)
|
|
|
|
|
2022-05-24 21:36:06 +08:00
|
|
|
return common.NewPipelineExecutor(pipeline...).
|
2022-05-24 22:52:25 +08:00
|
|
|
Finally(func(ctx context.Context) error {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
if ctx.Err() == context.Canceled {
|
|
|
|
// in case of an aborted run, we still should execute the
|
|
|
|
// post steps to allow cleanup.
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
return postExecutor(ctx)
|
|
|
|
}).
|
2022-05-24 21:36:06 +08:00
|
|
|
Finally(info.interpolateOutputs()).
|
|
|
|
Finally(info.closeContainer())
|
2022-02-09 01:22:41 +08:00
|
|
|
}
|
2022-06-17 23:55:21 +08:00
|
|
|
|
|
|
|
func useStepLogger(rc *RunContext, stepModel *model.Step, stage stepStage, executor common.Executor) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
ctx = withStepLogger(ctx, stepModel.ID, stepModel.String(), stage.String())
|
|
|
|
|
|
|
|
rawLogger := common.Logger(ctx).WithField("raw_output", true)
|
|
|
|
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
|
|
|
|
if rc.Config.LogOutput {
|
|
|
|
rawLogger.Infof("%s", s)
|
|
|
|
} else {
|
|
|
|
rawLogger.Debugf("%s", s)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
oldout, olderr := rc.JobContainer.ReplaceLogWriter(logWriter, logWriter)
|
|
|
|
defer rc.JobContainer.ReplaceLogWriter(oldout, olderr)
|
|
|
|
|
|
|
|
return executor(ctx)
|
|
|
|
}
|
|
|
|
}
|