Skip to content

Commit

Permalink
feat: replace logger with step logger
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
2 people authored and github-actions committed May 23, 2022
1 parent 4d8243d commit 5bc0031
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
55 changes: 36 additions & 19 deletions pkg/runner/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,27 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
return common.NewErrorExecutor(err)
}

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

stepExec := step.main()
steps = append(steps, func(ctx context.Context) error {
return (func(ctx context.Context) error {
logger := common.Logger(ctx)
err := stepExec(ctx)
if err != nil {
logger.Errorf("%v", err)
common.SetJobError(ctx, err)
} else if ctx.Err() != nil {
logger.Errorf("%v", ctx.Err())
common.SetJobError(ctx, ctx.Err())
}
return nil
})(withStepLogger(ctx, stepModel.ID, stepModel.String()))
})
steps = append(steps, useStepLogger(rc, stepModel, func(ctx context.Context) error {
logger := common.Logger(ctx)
err := stepExec(ctx)
if err != nil {
logger.Errorf("%v", err)
common.SetJobError(ctx, err)
} else if ctx.Err() != nil {
logger.Errorf("%v", ctx.Err())
common.SetJobError(ctx, ctx.Err())
}
return nil
}))

// run the post exector in reverse order
if postExecutor != nil {
postExecutor = step.post().Finally(postExecutor)
postExecutor = useStepLogger(rc, stepModel, step.post()).Finally(postExecutor)
} else {
postExecutor = step.post()
postExecutor = useStepLogger(rc, stepModel, step.post())
}
}

Expand Down Expand Up @@ -111,3 +107,24 @@ func newJobExecutor(info jobInfo, sf stepFactory, rc *RunContext) common.Executo
Finally(info.interpolateOutputs()).
Finally(info.closeContainer())
}

func useStepLogger(rc *RunContext, stepModel *model.Step, executor common.Executor) common.Executor {
return func(ctx context.Context) error {
ctx = withStepLogger(ctx, stepModel.ID, stepModel.String())

rawLogger := common.Logger(ctx).WithField("raw_output", true)
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
if rc.Config.LogOutput {
rawLogger.Infof("%s", s)
} else {
rawLogger.Debugf("%s", s)
}
return true
})

oldout, olderr := rc.JobContainer.ReplaceLogWriter(logWriter, logWriter)
defer rc.JobContainer.ReplaceLogWriter(oldout, olderr)

return executor(ctx)
}
}
14 changes: 13 additions & 1 deletion pkg/runner/job_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package runner
import (
"context"
"fmt"
"io"
"testing"

"github.com/nektos/act/pkg/common"
"github.com/nektos/act/pkg/container"
"github.com/nektos/act/pkg/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -75,6 +77,14 @@ func (jim *jobInfoMock) result(result string) {
jim.Called(result)
}

type jobContainerMock struct {
container.Container
}

func (jcm *jobContainerMock) ReplaceLogWriter(stdout, stderr io.Writer) (io.Writer, io.Writer) {
return nil, nil
}

type stepFactoryMock struct {
mock.Mock
}
Expand Down Expand Up @@ -236,7 +246,9 @@ func TestNewJobExecutor(t *testing.T) {
ctx := common.WithJobErrorContainer(context.Background())
jim := &jobInfoMock{}
sfm := &stepFactoryMock{}
rc := &RunContext{}
rc := &RunContext{
JobContainer: &jobContainerMock{},
}
executorOrder := make([]string, 0)

jim.On("steps").Return(tt.steps)
Expand Down

0 comments on commit 5bc0031

Please sign in to comment.