Skip to content

Commit

Permalink
feat: add step/job id and results to json logs
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Wolf <markus.wolf@new-work.se>
  • Loading branch information
2 people authored and github-actions committed May 18, 2022
1 parent 6a2197f commit 9287ec5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/runner/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestAddmaskUsemask(t *testing.T) {

re := captureOutput(t, func() {
ctx := context.Background()
ctx = WithJobLogger(ctx, "testjob", config, &rc.Masks)
ctx = WithJobLogger(ctx, "0", "testjob", config, &rc.Masks)

handler := rc.commandHandler(ctx)
handler("::add-mask::secret\n")
Expand Down
11 changes: 8 additions & 3 deletions pkg/runner/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
preSteps = append(preSteps, info.startContainer())

for i, stepModel := range infoSteps {
stepModel := stepModel
if stepModel == nil {
return func(ctx context.Context) error {
return fmt.Errorf("invalid Step %v: missing run or uses key", i)
Expand All @@ -55,11 +56,12 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
return common.NewErrorExecutor(err)
}

preSteps = append(preSteps, step.pre())
preSteps = append(preSteps, func(ctx context.Context) error {
return step.pre()(withStepLogger(ctx, stepModel.ID, stepModel.String()))
})

stepExec := step.main()
steps = append(steps, func(ctx context.Context) error {
stepName := stepModel.String()
return (func(ctx context.Context) error {
logger := common.Logger(ctx)
err := stepExec(ctx)
Expand All @@ -71,7 +73,7 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
common.SetJobError(ctx, ctx.Err())
}
return nil
})(withStepLogger(ctx, stepName))
})(withStepLogger(ctx, stepModel.ID, stepModel.String()))
})

// run the post exector in reverse order
Expand All @@ -83,15 +85,18 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
}

postExecutor = postExecutor.Finally(func(ctx context.Context) error {
logger := common.Logger(ctx)
jobError := common.JobError(ctx)
if jobError != nil {
info.result("failure")
logger.WithField("jobResult", "failure").Infof("\U0001F3C1 Job failed")
} else {
err := info.stopContainer()(ctx)
if err != nil {
return err
}
info.result("success")
logger.WithField("jobResult", "success").Infof("\U0001F3C1 Job succeeded")
}

return nil
Expand Down
15 changes: 11 additions & 4 deletions pkg/runner/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func WithMasks(ctx context.Context, masks *[]string) context.Context {
}

// WithJobLogger attaches a new logger to context that is aware of steps
func WithJobLogger(ctx context.Context, jobName string, config *Config, masks *[]string) context.Context {
func WithJobLogger(ctx context.Context, jobID string, jobName string, config *Config, masks *[]string) context.Context {
mux.Lock()
defer mux.Unlock()

Expand All @@ -82,7 +82,11 @@ func WithJobLogger(ctx context.Context, jobName string, config *Config, masks *[
logger.SetFormatter(formatter)
logger.SetOutput(os.Stdout)
logger.SetLevel(logrus.GetLevel())
rtn := logger.WithFields(logrus.Fields{"job": jobName, "dryrun": common.Dryrun(ctx)}).WithContext(ctx)
rtn := logger.WithFields(logrus.Fields{
"job": jobName,
"jobID": jobID,
"dryrun": common.Dryrun(ctx),
}).WithContext(ctx)

return common.WithLogger(ctx, rtn)
}
Expand All @@ -92,8 +96,11 @@ func WithCompositeLogger(ctx context.Context, masks *[]string) context.Context {
return common.WithLogger(ctx, common.Logger(ctx).WithFields(logrus.Fields{}).WithContext(ctx))
}

func withStepLogger(ctx context.Context, stepName string) context.Context {
rtn := common.Logger(ctx).WithFields(logrus.Fields{"step": stepName})
func withStepLogger(ctx context.Context, stepID string, stepName string) context.Context {
rtn := common.Logger(ctx).WithFields(logrus.Fields{
"step": stepName,
"stepID": stepID,
})
return common.WithLogger(ctx, rtn)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
}

return nil
})(common.WithJobErrorContainer(WithJobLogger(ctx, jobName, rc.Config, &rc.Masks)))
})(common.WithJobErrorContainer(WithJobLogger(ctx, rc.Run.JobID, jobName, rc.Config, &rc.Masks)))
})
}
pipeline = append(pipeline, common.NewParallelExecutor(maxParallel, stageExecutor...))
Expand Down
8 changes: 4 additions & 4 deletions pkg/runner/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
}

if !runStep {
logger.Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusSkipped
rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusSkipped
logger.Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
return nil
}

Expand All @@ -96,10 +96,8 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
err = executor(ctx)

if err == nil {
logger.Infof(" \u2705 Success - %s %s", stage, stepString)
logger.WithField("stepResult", rc.StepResults[rc.CurrentStep].Outcome).Infof(" \u2705 Success - %s %s", stage, stepString)
} else {
logger.Errorf(" \u274C Failure - %s %s", stage, stepString)

rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusFailure
if stepModel.ContinueOnError {
logger.Infof("Failed but continue next step")
Expand All @@ -108,6 +106,8 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
} 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
}
Expand Down

0 comments on commit 9287ec5

Please sign in to comment.