Skip to content

Commit

Permalink
feat: collect pre and post steps for composite actions
Browse files Browse the repository at this point in the history
  • Loading branch information
KnisterPeter authored and github-actions committed May 13, 2022
1 parent 7a4eead commit 15875da
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
42 changes: 35 additions & 7 deletions pkg/runner/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ func readActionImpl(step *model.Step, actionDir string, actionPath string, readF
func runActionImpl(step actionStep, actionDir string, remoteAction *remoteAction) common.Executor {
rc := step.getRunContext()
stepModel := step.getStepModel()

return func(ctx context.Context) error {
actionPath := ""
if remoteAction != nil && remoteAction.Path != "" {
actionPath = remoteAction.Path
}

action := step.getActionModel()
log.Debugf("About to run action %v", action)

Expand Down Expand Up @@ -441,7 +443,7 @@ func execAsComposite(step actionStep, containerActionDir string) common.Executor
rc.JobContainer.ReplaceLogWriter(oldout, olderr)
})()

err := compositerc.compositeExecutor(action)(ctx)
err := runCompositeSteps(ctx, action, compositerc)

// Map outputs from composite RunContext to job RunContext
eval = compositerc.NewExpressionEvaluator()
Expand All @@ -459,8 +461,10 @@ func execAsComposite(step actionStep, containerActionDir string) common.Executor
}

// Executor returns a pipeline executor for all the steps in the job
func (rc *RunContext) compositeExecutor(action *model.Action) common.Executor {
func (rc *RunContext) compositeExecutor(action *model.Action) *compositeSteps {
steps := make([]common.Executor, 0)
preSteps := make([]common.Executor, 0)
postSteps := make([]common.Executor, 0)

sf := &stepFactoryImpl{}

Expand All @@ -475,12 +479,15 @@ func (rc *RunContext) compositeExecutor(action *model.Action) common.Executor {

step, err := sf.newStep(&stepcopy, rc)
if err != nil {
return common.NewErrorExecutor(err)
return &compositeSteps{
main: common.NewErrorExecutor(err),
}
}
stepExec := common.NewPipelineExecutor(step.pre(), step.main(), step.post())

preSteps = append(preSteps, step.pre())

steps = append(steps, func(ctx context.Context) error {
err := stepExec(ctx)
err := step.main()(ctx)
if err != nil {
common.Logger(ctx).Errorf("%v", err)
common.SetJobError(ctx, err)
Expand All @@ -490,12 +497,33 @@ func (rc *RunContext) compositeExecutor(action *model.Action) common.Executor {
}
return nil
})

postSteps = append([]common.Executor{step.post()}, postSteps...)
}

steps = append(steps, common.JobError)
return func(ctx context.Context) error {
return common.NewPipelineExecutor(steps...)(common.WithJobErrorContainer(ctx))
return &compositeSteps{
pre: common.NewPipelineExecutor(preSteps...),
main: func(ctx context.Context) error {
return common.NewPipelineExecutor(steps...)(common.WithJobErrorContainer(ctx))
},
post: common.NewPipelineExecutor(postSteps...),
}
}

func runCompositeSteps(ctx context.Context, action *model.Action, compositerc *RunContext) error {
steps := compositerc.compositeExecutor(action)
var err error
if steps.pre != nil {
err = steps.pre(ctx)
}
if err == nil {
err = steps.main(ctx)
}
if err == nil && steps.post != nil {
err = steps.post(ctx)
}
return err
}

func populateEnvsFromSavedState(env *map[string]string, step actionStep, rc *RunContext) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/runner/action_composite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package runner

import "github.com/nektos/act/pkg/common"

type compositeSteps struct {
pre common.Executor
main common.Executor
post common.Executor
}
6 changes: 3 additions & 3 deletions pkg/runner/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestActionRunner(t *testing.T) {
name: "with-input",
step: &stepActionRemote{
Step: &model.Step{
Uses: "repo@ref",
Uses: "org/repo/path@ref",
},
RunContext: &RunContext{
Config: &Config{},
Expand Down Expand Up @@ -180,10 +180,10 @@ func TestActionRunner(t *testing.T) {
step: &stepActionRemote{
Step: &model.Step{
ID: "step",
Uses: "repo@ref",
Uses: "org/repo/path@ref",
},
RunContext: &RunContext{
ActionRepository: "repo",
ActionRepository: "org/repo",
ActionPath: "path",
ActionRef: "ref",
Config: &Config{},
Expand Down

0 comments on commit 15875da

Please sign in to comment.