Improve logging (#1171)

* feat: use logger from context wherever possible

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

* feat: add step/job id and results to json logs

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

* test: value to be masked should not be hard-coded in the action

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

* fix: replace values following ::add-mask:: in evaluated strings

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

* feat: [DEBUG] identifier for debug logs to distinguish them

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

* feat: replace logger with step logger

The container gets injected a job logger, but during the time that steps
are run, we want to use the step logger.
This commit wraps pre/main/post steps in an executor that replaces the
job logger with a step logger.

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

* feat: add pre/post stage identifier fields to json log output

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

* feat: add job/step result status to skipped steps/jobs

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>

Co-authored-by: Markus Wolf <markus.wolf@new-work.se>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Björn Brauer
2022-06-17 17:55:21 +02:00
committed by GitHub
parent 52f5c4592c
commit 4391a10d5a
35 changed files with 363 additions and 292 deletions

View File

@@ -8,7 +8,6 @@ import (
"github.com/nektos/act/pkg/common"
"github.com/nektos/act/pkg/exprparser"
"github.com/nektos/act/pkg/model"
log "github.com/sirupsen/logrus"
)
type step interface {
@@ -19,7 +18,7 @@ type step interface {
getRunContext() *RunContext
getStepModel() *model.Step
getEnv() *map[string]string
getIfExpression(stage stepStage) string
getIfExpression(context context.Context, stage stepStage) string
}
type stepStage int
@@ -56,10 +55,11 @@ func (s stepStage) getStepName(stepModel *model.Step) string {
func runStepExecutor(step step, stage stepStage, executor common.Executor) common.Executor {
return func(ctx context.Context) error {
logger := common.Logger(ctx)
rc := step.getRunContext()
stepModel := step.getStepModel()
ifExpression := step.getIfExpression(stage)
ifExpression := step.getIfExpression(ctx, stage)
rc.CurrentStep = stage.getStepName(stepModel)
rc.StepResults[rc.CurrentStep] = &model.StepResult{
@@ -81,9 +81,9 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
}
if !runStep {
log.Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusSkipped
rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusSkipped
logger.WithField("stepResult", rc.StepResults[rc.CurrentStep].Outcome).Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
return nil
}
@@ -91,23 +91,23 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
if strings.Contains(stepString, "::add-mask::") {
stepString = "add-mask command"
}
common.Logger(ctx).Infof("\u2B50 Run %s %s", stage, stepString)
logger.Infof("\u2B50 Run %s %s", stage, stepString)
err = executor(ctx)
if err == nil {
common.Logger(ctx).Infof(" \u2705 Success - %s %s", stage, stepString)
logger.WithField("stepResult", rc.StepResults[rc.CurrentStep].Outcome).Infof(" \u2705 Success - %s %s", stage, stepString)
} else {
common.Logger(ctx).Errorf(" \u274C Failure - %s %s", stage, stepString)
rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusFailure
if stepModel.ContinueOnError {
common.Logger(ctx).Infof("Failed but continue next step")
logger.Infof("Failed but continue next step")
err = nil
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusSuccess
} else {
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusFailure
}
logger.WithField("stepResult", rc.StepResults[rc.CurrentStep].Outcome).Errorf(" \u274C Failure - %s %s", stage, stepString)
}
return err
}
@@ -116,7 +116,7 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
func setupEnv(ctx context.Context, step step) error {
rc := step.getRunContext()
mergeEnv(step)
mergeEnv(ctx, step)
err := rc.JobContainer.UpdateFromImageEnv(step.getEnv())(ctx)
if err != nil {
return err
@@ -131,9 +131,9 @@ func setupEnv(ctx context.Context, step step) error {
}
mergeIntoMap(step.getEnv(), step.getStepModel().GetEnv()) // step env should not be overwritten
exprEval := rc.NewStepExpressionEvaluator(step)
exprEval := rc.NewStepExpressionEvaluator(ctx, step)
for k, v := range *step.getEnv() {
(*step.getEnv())[k] = exprEval.Interpolate(v)
(*step.getEnv())[k] = exprEval.Interpolate(ctx, v)
}
common.Logger(ctx).Debugf("setupEnv => %v", *step.getEnv())
@@ -141,7 +141,7 @@ func setupEnv(ctx context.Context, step step) error {
return nil
}
func mergeEnv(step step) {
func mergeEnv(ctx context.Context, step step) {
env := step.getEnv()
rc := step.getRunContext()
job := rc.Run.Job()
@@ -162,7 +162,7 @@ func mergeEnv(step step) {
(*env)["PATH"] += `:` + p
}
mergeIntoMap(env, rc.withGithubEnv(*env))
mergeIntoMap(env, rc.withGithubEnv(ctx, *env))
}
func isStepEnabled(ctx context.Context, expr string, step step, stage stepStage) (bool, error) {
@@ -175,7 +175,7 @@ func isStepEnabled(ctx context.Context, expr string, step step, stage stepStage)
defaultStatusCheck = exprparser.DefaultStatusCheckSuccess
}
runStep, err := EvalBool(rc.NewStepExpressionEvaluator(step), expr, defaultStatusCheck)
runStep, err := EvalBool(ctx, rc.NewStepExpressionEvaluator(ctx, step), expr, defaultStatusCheck)
if err != nil {
return false, fmt.Errorf(" \u274C Error in if-expression: \"if: %s\" (%s)", expr, err)
}