2022-03-22 16:13:00 -05:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-11-16 15:42:57 -06:00
|
|
|
"path"
|
2022-03-22 16:13:00 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
2022-11-16 15:42:57 -06:00
|
|
|
"github.com/nektos/act/pkg/container"
|
2022-05-24 08:36:06 -05:00
|
|
|
"github.com/nektos/act/pkg/exprparser"
|
2022-03-22 16:13:00 -05:00
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type step interface {
|
|
|
|
pre() common.Executor
|
|
|
|
main() common.Executor
|
|
|
|
post() common.Executor
|
|
|
|
|
|
|
|
getRunContext() *RunContext
|
2022-10-06 16:58:16 -05:00
|
|
|
getGithubContext(ctx context.Context) *model.GithubContext
|
2022-03-22 16:13:00 -05:00
|
|
|
getStepModel() *model.Step
|
|
|
|
getEnv() *map[string]string
|
2022-06-17 10:55:21 -05:00
|
|
|
getIfExpression(context context.Context, stage stepStage) string
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2023-02-04 07:35:13 -06:00
|
|
|
func processRunnerEnvFileCommand(ctx context.Context, fileName string, rc *RunContext, setter func(context.Context, map[string]string, string)) error {
|
|
|
|
env := map[string]string{}
|
|
|
|
err := rc.JobContainer.UpdateFromEnv(path.Join(rc.JobContainer.GetActPath(), fileName), &env)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range env {
|
|
|
|
setter(ctx, map[string]string{"name": k}, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
func runStepExecutor(step step, stage stepStage, executor common.Executor) common.Executor {
|
2022-03-22 16:13:00 -05:00
|
|
|
return func(ctx context.Context) error {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
2022-03-22 16:13:00 -05:00
|
|
|
rc := step.getRunContext()
|
|
|
|
stepModel := step.getStepModel()
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
ifExpression := step.getIfExpression(ctx, stage)
|
2022-12-15 11:08:31 -06:00
|
|
|
rc.CurrentStep = stepModel.ID
|
2022-05-24 08:36:06 -05:00
|
|
|
|
2022-12-15 11:08:31 -06:00
|
|
|
stepResult := &model.StepResult{
|
2022-03-22 16:13:00 -05:00
|
|
|
Outcome: model.StepStatusSuccess,
|
|
|
|
Conclusion: model.StepStatusSuccess,
|
|
|
|
Outputs: make(map[string]string),
|
|
|
|
}
|
2022-12-15 11:08:31 -06:00
|
|
|
if stage == stepStageMain {
|
|
|
|
rc.StepResults[rc.CurrentStep] = stepResult
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
|
|
|
|
err := setupEnv(ctx, step)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
runStep, err := isStepEnabled(ctx, ifExpression, step, stage)
|
2022-03-22 16:13:00 -05:00
|
|
|
if err != nil {
|
2022-12-15 11:08:31 -06:00
|
|
|
stepResult.Conclusion = model.StepStatusFailure
|
|
|
|
stepResult.Outcome = model.StepStatusFailure
|
2022-03-22 16:13:00 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !runStep {
|
2022-12-15 11:08:31 -06:00
|
|
|
stepResult.Conclusion = model.StepStatusSkipped
|
|
|
|
stepResult.Outcome = model.StepStatusSkipped
|
|
|
|
logger.WithField("stepResult", stepResult.Outcome).Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
|
2022-03-22 16:13:00 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-16 15:55:23 -06:00
|
|
|
stepString := rc.ExprEval.Interpolate(ctx, stepModel.String())
|
2022-05-11 14:06:05 -05:00
|
|
|
if strings.Contains(stepString, "::add-mask::") {
|
|
|
|
stepString = "add-mask command"
|
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Infof("\u2B50 Run %s %s", stage, stepString)
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-11-16 15:42:57 -06:00
|
|
|
// Prepare and clean Runner File Commands
|
|
|
|
actPath := rc.JobContainer.GetActPath()
|
|
|
|
outputFileCommand := path.Join("workflow", "outputcmd.txt")
|
|
|
|
stateFileCommand := path.Join("workflow", "statecmd.txt")
|
2022-12-09 05:16:15 -06:00
|
|
|
pathFileCommand := path.Join("workflow", "pathcmd.txt")
|
2023-02-04 07:35:13 -06:00
|
|
|
envFileCommand := path.Join("workflow", "envs.txt")
|
2022-11-16 15:42:57 -06:00
|
|
|
(*step.getEnv())["GITHUB_OUTPUT"] = path.Join(actPath, outputFileCommand)
|
|
|
|
(*step.getEnv())["GITHUB_STATE"] = path.Join(actPath, stateFileCommand)
|
2022-12-09 05:16:15 -06:00
|
|
|
(*step.getEnv())["GITHUB_PATH"] = path.Join(actPath, pathFileCommand)
|
2023-02-04 07:35:13 -06:00
|
|
|
(*step.getEnv())["GITHUB_ENV"] = path.Join(actPath, envFileCommand)
|
2022-11-16 15:42:57 -06:00
|
|
|
_ = rc.JobContainer.Copy(actPath, &container.FileEntry{
|
|
|
|
Name: outputFileCommand,
|
|
|
|
Mode: 0666,
|
|
|
|
}, &container.FileEntry{
|
|
|
|
Name: stateFileCommand,
|
|
|
|
Mode: 0666,
|
2022-12-09 05:16:15 -06:00
|
|
|
}, &container.FileEntry{
|
|
|
|
Name: pathFileCommand,
|
|
|
|
Mode: 0666,
|
2023-02-04 07:35:13 -06:00
|
|
|
}, &container.FileEntry{
|
|
|
|
Name: envFileCommand,
|
|
|
|
Mode: 0666,
|
2022-11-16 15:42:57 -06:00
|
|
|
})(ctx)
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
err = executor(ctx)
|
|
|
|
|
|
|
|
if err == nil {
|
2022-12-15 11:08:31 -06:00
|
|
|
logger.WithField("stepResult", stepResult.Outcome).Infof(" \u2705 Success - %s %s", stage, stepString)
|
2022-03-22 16:13:00 -05:00
|
|
|
} else {
|
2022-12-15 11:08:31 -06:00
|
|
|
stepResult.Outcome = model.StepStatusFailure
|
2022-09-08 09:20:39 -05:00
|
|
|
|
|
|
|
continueOnError, parseErr := isContinueOnError(ctx, stepModel.RawContinueOnError, step, stage)
|
|
|
|
if parseErr != nil {
|
2022-12-15 11:08:31 -06:00
|
|
|
stepResult.Conclusion = model.StepStatusFailure
|
2022-09-08 09:20:39 -05:00
|
|
|
return parseErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if continueOnError {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Infof("Failed but continue next step")
|
2022-03-22 16:13:00 -05:00
|
|
|
err = nil
|
2022-12-15 11:08:31 -06:00
|
|
|
stepResult.Conclusion = model.StepStatusSuccess
|
2022-03-22 16:13:00 -05:00
|
|
|
} else {
|
2022-12-15 11:08:31 -06:00
|
|
|
stepResult.Conclusion = model.StepStatusFailure
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
|
2022-12-15 11:08:31 -06:00
|
|
|
logger.WithField("stepResult", stepResult.Outcome).Errorf(" \u274C Failure - %s %s", stage, stepString)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
2022-11-16 15:42:57 -06:00
|
|
|
// Process Runner File Commands
|
|
|
|
orgerr := err
|
2023-02-04 07:35:13 -06:00
|
|
|
err = processRunnerEnvFileCommand(ctx, envFileCommand, rc, rc.setEnv)
|
2022-11-16 15:42:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-04 07:35:13 -06:00
|
|
|
err = processRunnerEnvFileCommand(ctx, stateFileCommand, rc, rc.saveState)
|
2022-11-16 15:42:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-04 07:35:13 -06:00
|
|
|
err = processRunnerEnvFileCommand(ctx, outputFileCommand, rc, rc.setOutput)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-16 15:42:57 -06:00
|
|
|
}
|
2022-12-09 05:16:15 -06:00
|
|
|
err = rc.UpdateExtraPath(ctx, path.Join(actPath, pathFileCommand))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-16 15:42:57 -06:00
|
|
|
if orgerr != nil {
|
|
|
|
return orgerr
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupEnv(ctx context.Context, step step) error {
|
|
|
|
rc := step.getRunContext()
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
mergeEnv(ctx, step)
|
2022-10-06 16:58:16 -05:00
|
|
|
// merge step env last, since it should not be overwritten
|
|
|
|
mergeIntoMap(step.getEnv(), step.getStepModel().GetEnv())
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
exprEval := rc.NewExpressionEvaluator(ctx)
|
2022-03-22 16:13:00 -05:00
|
|
|
for k, v := range *step.getEnv() {
|
2022-12-06 10:46:20 -06:00
|
|
|
if !strings.HasPrefix(k, "INPUT_") {
|
|
|
|
(*step.getEnv())[k] = exprEval.Interpolate(ctx, v)
|
|
|
|
}
|
|
|
|
}
|
2022-12-07 09:31:33 -06:00
|
|
|
// after we have an evaluated step context, update the expressions evaluator with a new env context
|
2022-12-06 10:46:20 -06:00
|
|
|
// you can use step level env in the with property of a uses construct
|
|
|
|
exprEval = rc.NewExpressionEvaluatorWithEnv(ctx, *step.getEnv())
|
|
|
|
for k, v := range *step.getEnv() {
|
|
|
|
if strings.HasPrefix(k, "INPUT_") {
|
|
|
|
(*step.getEnv())[k] = exprEval.Interpolate(ctx, v)
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
common.Logger(ctx).Debugf("setupEnv => %v", *step.getEnv())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func mergeEnv(ctx context.Context, step step) {
|
2022-03-22 16:13:00 -05:00
|
|
|
env := step.getEnv()
|
|
|
|
rc := step.getRunContext()
|
|
|
|
job := rc.Run.Job()
|
|
|
|
|
|
|
|
c := job.Container()
|
|
|
|
if c != nil {
|
|
|
|
mergeIntoMap(env, rc.GetEnv(), c.Env)
|
|
|
|
} else {
|
|
|
|
mergeIntoMap(env, rc.GetEnv())
|
|
|
|
}
|
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
rc.withGithubEnv(ctx, step.getGithubContext(ctx), *env)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
func isStepEnabled(ctx context.Context, expr string, step step, stage stepStage) (bool, error) {
|
2022-03-22 16:13:00 -05:00
|
|
|
rc := step.getRunContext()
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
var defaultStatusCheck exprparser.DefaultStatusCheck
|
|
|
|
if stage == stepStagePost {
|
|
|
|
defaultStatusCheck = exprparser.DefaultStatusCheckAlways
|
|
|
|
} else {
|
|
|
|
defaultStatusCheck = exprparser.DefaultStatusCheckSuccess
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
runStep, err := EvalBool(ctx, rc.NewStepExpressionEvaluator(ctx, step), expr, defaultStatusCheck)
|
2022-03-22 16:13:00 -05:00
|
|
|
if err != nil {
|
2022-05-24 08:36:06 -05:00
|
|
|
return false, fmt.Errorf(" \u274C Error in if-expression: \"if: %s\" (%s)", expr, err)
|
2022-03-22 16:13:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return runStep, nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 09:20:39 -05:00
|
|
|
func isContinueOnError(ctx context.Context, expr string, step step, stage stepStage) (bool, error) {
|
|
|
|
// https://github.com/github/docs/blob/3ae84420bd10997bb5f35f629ebb7160fe776eae/content/actions/reference/workflow-syntax-for-github-actions.md?plain=true#L962
|
|
|
|
if len(strings.TrimSpace(expr)) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rc := step.getRunContext()
|
|
|
|
|
|
|
|
continueOnError, err := EvalBool(ctx, rc.NewStepExpressionEvaluator(ctx, step), expr, exprparser.DefaultStatusCheckNone)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf(" \u274C Error in continue-on-error-expression: \"continue-on-error: %s\" (%s)", expr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return continueOnError, nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
func mergeIntoMap(target *map[string]string, maps ...map[string]string) {
|
|
|
|
for _, m := range maps {
|
|
|
|
for k, v := range m {
|
|
|
|
(*target)[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|