implement pre and post steps (#1089)
* feat: add post step to actions and add state command This commit includes requried changes for running post steps for local and remote actions. This allows general cleanup work to be done after executing an action. Communication is allowed between this steps, by using the action state. * feat: collect pre and post steps for composite actions * refactor: move composite action logic into own file * refactor: restructure composite handling * feat: run composite post steps during post step lifecycle * refactor: remove duplicate log output * feat: run all composite post actions in a step Since composite actions could have multiple pre/post steps inside, we need to run all of them in a single top-level pre/post step. This PR includes a test case for this and the correct order of steps to be executed. * refactor: remove unused lines of code * refactor: simplify test expression * fix: use composite job logger * fix: make step output more readable * fix: enforce running all post executor To make sure every post executor/step is executed, it is chained with it's own Finally executor. * fix: do not run post step if no step result is available Having no step result means we do not run any step (neither pre nor main) and we do not need to run post. * fix: setup defaults If no pre-if or post-if is given, it should default to 'always()'. This could be set even if there is no pre or post step. In fact this is required for composite actions and included post steps to run. * fix: output step related if expression * test: update expectation * feat: run pre step from actions (#1110) This PR implements running pre steps for remote actions. This includes remote actions using inside local composite actions. * fix: set correct expr default status checks For post-if conditions the default status check should be always(), while for all other if expression the default status check is success() References: https://docs.github.com/en/actions/learn-github-actions/expressions#status-check-functions https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runspost-if * fix: remove code added during rebase
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/nektos/act/pkg/common"
|
||||
"github.com/nektos/act/pkg/exprparser"
|
||||
"github.com/nektos/act/pkg/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -18,14 +19,49 @@ type step interface {
|
||||
getRunContext() *RunContext
|
||||
getStepModel() *model.Step
|
||||
getEnv() *map[string]string
|
||||
getIfExpression(stage stepStage) string
|
||||
}
|
||||
|
||||
func runStepExecutor(step step, executor common.Executor) common.Executor {
|
||||
type stepStage int
|
||||
|
||||
const (
|
||||
stepStagePre stepStage = iota
|
||||
stepStageMain
|
||||
stepStagePost
|
||||
)
|
||||
|
||||
func (s stepStage) String() string {
|
||||
switch s {
|
||||
case stepStagePre:
|
||||
return "Pre"
|
||||
case stepStageMain:
|
||||
return "Main"
|
||||
case stepStagePost:
|
||||
return "Post"
|
||||
}
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
func (s stepStage) getStepName(stepModel *model.Step) string {
|
||||
switch s {
|
||||
case stepStagePre:
|
||||
return fmt.Sprintf("pre-%s", stepModel.ID)
|
||||
case stepStageMain:
|
||||
return stepModel.ID
|
||||
case stepStagePost:
|
||||
return fmt.Sprintf("post-%s", stepModel.ID)
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func runStepExecutor(step step, stage stepStage, executor common.Executor) common.Executor {
|
||||
return func(ctx context.Context) error {
|
||||
rc := step.getRunContext()
|
||||
stepModel := step.getStepModel()
|
||||
|
||||
rc.CurrentStep = stepModel.ID
|
||||
ifExpression := step.getIfExpression(stage)
|
||||
rc.CurrentStep = stage.getStepName(stepModel)
|
||||
|
||||
rc.StepResults[rc.CurrentStep] = &model.StepResult{
|
||||
Outcome: model.StepStatusSuccess,
|
||||
Conclusion: model.StepStatusSuccess,
|
||||
@@ -37,7 +73,7 @@ func runStepExecutor(step step, executor common.Executor) common.Executor {
|
||||
return err
|
||||
}
|
||||
|
||||
runStep, err := isStepEnabled(ctx, step)
|
||||
runStep, err := isStepEnabled(ctx, ifExpression, step, stage)
|
||||
if err != nil {
|
||||
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusFailure
|
||||
rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusFailure
|
||||
@@ -45,7 +81,7 @@ func runStepExecutor(step step, executor common.Executor) common.Executor {
|
||||
}
|
||||
|
||||
if !runStep {
|
||||
log.Debugf("Skipping step '%s' due to '%s'", stepModel.String(), stepModel.If.Value)
|
||||
log.Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
|
||||
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusSkipped
|
||||
rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusSkipped
|
||||
return nil
|
||||
@@ -55,14 +91,14 @@ func runStepExecutor(step step, executor common.Executor) common.Executor {
|
||||
if strings.Contains(stepString, "::add-mask::") {
|
||||
stepString = "add-mask command"
|
||||
}
|
||||
common.Logger(ctx).Infof("\u2B50 Run %s", stepString)
|
||||
common.Logger(ctx).Infof("\u2B50 Run %s %s", stage, stepString)
|
||||
|
||||
err = executor(ctx)
|
||||
|
||||
if err == nil {
|
||||
common.Logger(ctx).Infof(" \u2705 Success - %s", stepString)
|
||||
common.Logger(ctx).Infof(" \u2705 Success - %s %s", stage, stepString)
|
||||
} else {
|
||||
common.Logger(ctx).Errorf(" \u274C Failure - %s", stepString)
|
||||
common.Logger(ctx).Errorf(" \u274C Failure - %s %s", stage, stepString)
|
||||
|
||||
rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusFailure
|
||||
if stepModel.ContinueOnError {
|
||||
@@ -129,12 +165,19 @@ func mergeEnv(step step) {
|
||||
mergeIntoMap(env, rc.withGithubEnv(*env))
|
||||
}
|
||||
|
||||
func isStepEnabled(ctx context.Context, step step) (bool, error) {
|
||||
func isStepEnabled(ctx context.Context, expr string, step step, stage stepStage) (bool, error) {
|
||||
rc := step.getRunContext()
|
||||
|
||||
runStep, err := EvalBool(rc.NewStepExpressionEvaluator(step), step.getStepModel().If.Value)
|
||||
var defaultStatusCheck exprparser.DefaultStatusCheck
|
||||
if stage == stepStagePost {
|
||||
defaultStatusCheck = exprparser.DefaultStatusCheckAlways
|
||||
} else {
|
||||
defaultStatusCheck = exprparser.DefaultStatusCheckSuccess
|
||||
}
|
||||
|
||||
runStep, err := EvalBool(rc.NewStepExpressionEvaluator(step), expr, defaultStatusCheck)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf(" \u274C Error in if-expression: \"if: %s\" (%s)", step.getStepModel().If.Value, err)
|
||||
return false, fmt.Errorf(" \u274C Error in if-expression: \"if: %s\" (%s)", expr, err)
|
||||
}
|
||||
|
||||
return runStep, nil
|
||||
|
Reference in New Issue
Block a user