mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-06 23:30:29 +00:00
refactor: extract RunContext Executor in JobExecutor (#984)
This splits the executor from the RunContext into its own function called newJobExecutor. We defined an interface called jobInfo which is implemented by the RunContext. This enables better unit testing because only a small interface needs to be mocked. This is a preparation for implementing pre and post actions. Co-authored-by: Björn Brauer <[email protected]> Co-authored-by: Marcus Noll <[email protected]> Co-authored-by: Jonas Holland <[email protected]> Co-authored-by: Robert Kowalski <[email protected]> Co-authored-by: Markus Wolf <[email protected]> Co-authored-by: Björn Brauer <[email protected]> Co-authored-by: Marcus Noll <[email protected]> Co-authored-by: Jonas Holland <[email protected]> Co-authored-by: Robert Kowalski <[email protected]> Co-authored-by: Markus Wolf <[email protected]>
This commit is contained in:
4 files changed
+233
-45
No files matched your search
+26
-45
@@ -244,57 +244,38 @@ func (rc *RunContext) interpolateOutputs() common.Executor {
|
||||
}
|
||||
}
|
||||
|
||||
// Executor returns a pipeline executor for all the steps in the job
|
||||
func (rc *RunContext) Executor() common.Executor {
|
||||
steps := make([]common.Executor, 0)
|
||||
func (rc *RunContext) startContainer() common.Executor {
|
||||
return rc.startJobContainer()
|
||||
}
|
||||
|
||||
steps = append(steps, func(ctx context.Context) error {
|
||||
if len(rc.Matrix) > 0 {
|
||||
common.Logger(ctx).Infof("\U0001F9EA Matrix: %v", rc.Matrix)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
func (rc *RunContext) stopContainer() common.Executor {
|
||||
return rc.stopJobContainer()
|
||||
}
|
||||
|
||||
steps = append(steps, rc.startJobContainer())
|
||||
|
||||
for i, step := range rc.Run.Job().Steps {
|
||||
if step.ID == "" {
|
||||
step.ID = fmt.Sprintf("%d", i)
|
||||
}
|
||||
stepExec := rc.newStepExecutor(step)
|
||||
steps = append(steps, 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
|
||||
})
|
||||
}
|
||||
steps = append(steps, func(ctx context.Context) error {
|
||||
err := rc.stopJobContainer()(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rc.Run.Job().Result = "success"
|
||||
jobError := common.JobError(ctx)
|
||||
if jobError != nil {
|
||||
rc.Run.Job().Result = "failure"
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return common.NewPipelineExecutor(steps...).Finally(rc.interpolateOutputs()).Finally(func(ctx context.Context) error {
|
||||
func (rc *RunContext) closeContainer() common.Executor {
|
||||
return func(ctx context.Context) error {
|
||||
if rc.JobContainer != nil {
|
||||
return rc.JobContainer.Close()(ctx)
|
||||
}
|
||||
return nil
|
||||
}).If(rc.isEnabled)
|
||||
}
|
||||
}
|
||||
|
||||
func (rc *RunContext) matrix() map[string]interface{} {
|
||||
return rc.Matrix
|
||||
}
|
||||
|
||||
func (rc *RunContext) result(result string) {
|
||||
rc.Run.Job().Result = result
|
||||
}
|
||||
|
||||
func (rc *RunContext) steps() []*model.Step {
|
||||
return rc.Run.Job().Steps
|
||||
}
|
||||
|
||||
// Executor returns a pipeline executor for all the steps in the job
|
||||
func (rc *RunContext) Executor() common.Executor {
|
||||
return newJobExecutor(rc).If(rc.isEnabled)
|
||||
}
|
||||
|
||||
// Executor returns a pipeline executor for all the steps in the job
|
||||
|
||||
Reference in new issue
Block a user