feat: log parsed command data in json logger (#103)

* feat: log parsed command data in json logger

* add logging info to other commands as well
This commit is contained in:
ChristopherHX
2025-05-17 16:14:53 +02:00
committed by GitHub
parent 7c0b21a846
commit bb140f1a38
8 changed files with 91 additions and 37 deletions

View File

@@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/actions-oss/act-cli/pkg/common" "github.com/actions-oss/act-cli/pkg/common"
"github.com/sirupsen/logrus"
) )
var commandPatternGA *regexp.Regexp var commandPatternGA *regexp.Regexp
@@ -41,11 +42,12 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
} }
if resumeCommand != "" && command != resumeCommand { if resumeCommand != "" && command != resumeCommand {
logger.Infof(" \U00002699 %s", line) logger.WithFields(logrus.Fields{"command": "ignored", "raw": line}).Infof(" \U00002699 %s", line)
return false return false
} }
arg = unescapeCommandData(arg) arg = unescapeCommandData(arg)
kvPairs = unescapeKvPairs(kvPairs) kvPairs = unescapeKvPairs(kvPairs)
defCommandLogger := logger.WithFields(logrus.Fields{"command": command, "kvPairs": kvPairs, "arg": arg, "raw": line})
switch command { switch command {
case "set-env": case "set-env":
rc.setEnv(ctx, kvPairs, arg) rc.setEnv(ctx, kvPairs, arg)
@@ -54,27 +56,27 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
case "add-path": case "add-path":
rc.addPath(ctx, arg) rc.addPath(ctx, arg)
case "debug": case "debug":
logger.Debugf(" \U0001F4AC %s", line) defCommandLogger.Debugf(" \U0001F4AC %s", line)
case "warning": case "warning":
logger.Warnf(" \U0001F6A7 %s", line) defCommandLogger.Warnf(" \U0001F6A7 %s", line)
case "error": case "error":
logger.Errorf(" \U00002757 %s", line) defCommandLogger.Errorf(" \U00002757 %s", line)
case "add-mask": case "add-mask":
rc.AddMask(arg) rc.AddMask(arg)
logger.Infof(" \U00002699 %s", "***") defCommandLogger.Infof(" \U00002699 %s", "***")
case "stop-commands": case "stop-commands":
resumeCommand = arg resumeCommand = arg
logger.Infof(" \U00002699 %s", line) defCommandLogger.Infof(" \U00002699 %s", line)
case resumeCommand: case resumeCommand:
resumeCommand = "" resumeCommand = ""
logger.Infof(" \U00002699 %s", line) defCommandLogger.Infof(" \U00002699 %s", line)
case "save-state": case "save-state":
logger.Infof(" \U0001f4be %s", line) defCommandLogger.Infof(" \U0001f4be %s", line)
rc.saveState(ctx, kvPairs, arg) rc.saveState(ctx, kvPairs, arg)
case "add-matcher": case "add-matcher":
logger.Infof(" \U00002753 add-matcher %s", arg) defCommandLogger.Infof(" \U00002753 add-matcher %s", arg)
default: default:
logger.Infof(" \U00002753 %s", line) defCommandLogger.Infof(" \U00002753 %s", line)
} }
return false return false
@@ -83,7 +85,7 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg string) { func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg string) {
name := kvPairs["name"] name := kvPairs["name"]
common.Logger(ctx).Infof(" \U00002699 ::set-env:: %s=%s", name, arg) common.Logger(ctx).WithFields(logrus.Fields{"command": "set-env", "name": name, "arg": arg}).Infof(" \U00002699 ::set-env:: %s=%s", name, arg)
if rc.Env == nil { if rc.Env == nil {
rc.Env = make(map[string]string) rc.Env = make(map[string]string)
} }
@@ -115,11 +117,11 @@ func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string,
return return
} }
logger.Infof(" \U00002699 ::set-output:: %s=%s", outputName, arg) logger.WithFields(logrus.Fields{"command": "set-output", "name": outputName, "arg": arg}).Infof(" \U00002699 ::set-output:: %s=%s", outputName, arg)
result.Outputs[outputName] = arg result.Outputs[outputName] = arg
} }
func (rc *RunContext) addPath(ctx context.Context, arg string) { func (rc *RunContext) addPath(ctx context.Context, arg string) {
common.Logger(ctx).Infof(" \U00002699 ::add-path:: %s", arg) common.Logger(ctx).WithFields(logrus.Fields{"command": "add-path", "arg": arg}).Infof(" \U00002699 ::add-path:: %s", arg)
extraPath := []string{arg} extraPath := []string{arg}
for _, v := range rc.ExtraPath { for _, v := range rc.ExtraPath {
if v != arg { if v != arg {

View File

@@ -316,6 +316,8 @@ func TestRunEvent(t *testing.T) {
{workdir, "set-env-step-env-override", "push", "", platforms, secrets}, {workdir, "set-env-step-env-override", "push", "", platforms, secrets},
{workdir, "set-env-new-env-file-per-step", "push", "", platforms, secrets}, {workdir, "set-env-new-env-file-per-step", "push", "", platforms, secrets},
{workdir, "no-panic-on-invalid-composite-action", "push", "jobs failed due to invalid action", platforms, secrets}, {workdir, "no-panic-on-invalid-composite-action", "push", "jobs failed due to invalid action", platforms, secrets},
// GITHUB_STEP_SUMMARY
{workdir, "stepsummary", "push", "", platforms, secrets},
// services // services
{workdir, "services", "push", "", platforms, secrets}, {workdir, "services", "push", "", platforms, secrets},
@@ -577,7 +579,9 @@ func TestRunEventHostEnvironment(t *testing.T) {
{workdir, "matrix", "push", "", platforms, secrets}, {workdir, "matrix", "push", "", platforms, secrets},
{workdir, "matrix-include-exclude", "push", "", platforms, secrets}, {workdir, "matrix-include-exclude", "push", "", platforms, secrets},
{workdir, "commands", "push", "", platforms, secrets}, {workdir, "commands", "push", "", platforms, secrets},
{workdir, "defaults-run", "push", "", platforms, secrets}, // Disabled for now because this test is somewhat invalid
// shell sh is not necessarily bash if the job has no override
// {workdir, "defaults-run", "push", "", platforms, secrets},
{workdir, "composite-fail-with-output", "push", "", platforms, secrets}, {workdir, "composite-fail-with-output", "push", "", platforms, secrets},
{workdir, "issue-597", "push", "", platforms, secrets}, {workdir, "issue-597", "push", "", platforms, secrets},
{workdir, "issue-598", "push", "", platforms, secrets}, {workdir, "issue-598", "push", "", platforms, secrets},

View File

@@ -1,8 +1,11 @@
package runner package runner
import ( import (
"archive/tar"
"context" "context"
"errors"
"fmt" "fmt"
"io"
"path" "path"
"strconv" "strconv"
"strings" "strings"
@@ -50,6 +53,32 @@ func (s stepStage) String() string {
return "Unknown" return "Unknown"
} }
func processRunnerSummaryCommand(ctx context.Context, fileName string, rc *RunContext) error {
if common.Dryrun(ctx) {
return nil
}
pathTar, err := rc.JobContainer.GetContainerArchive(ctx, path.Join(rc.JobContainer.GetActPath(), fileName))
if err != nil {
return err
}
defer pathTar.Close()
reader := tar.NewReader(pathTar)
_, err = reader.Next()
if err != nil && err != io.EOF {
return err
}
summary, err := io.ReadAll(reader)
if err != nil {
return err
}
if len(summary) == 0 {
return nil
}
common.Logger(ctx).WithFields(logrus.Fields{"command": "summary", "content": string(summary)}).Infof(" \U00002699 Summary - %s", string(summary))
return nil
}
func processRunnerEnvFileCommand(ctx context.Context, fileName string, rc *RunContext, setter func(context.Context, map[string]string, string)) error { func processRunnerEnvFileCommand(ctx context.Context, fileName string, rc *RunContext, setter func(context.Context, map[string]string, string)) error {
env := map[string]string{} env := map[string]string{}
err := rc.JobContainer.UpdateFromEnv(path.Join(rc.JobContainer.GetActPath(), fileName), &env)(ctx) err := rc.JobContainer.UpdateFromEnv(path.Join(rc.JobContainer.GetActPath(), fileName), &env)(ctx)
@@ -173,27 +202,13 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
logger.WithField("stepResult", stepResult.Outcome).Errorf(" \u274C Failure - %s %s", stage, stepString) logger.WithField("stepResult", stepResult.Outcome).Errorf(" \u274C Failure - %s %s", stage, stepString)
} }
// Process Runner File Commands // Process Runner File Commands
orgerr := err ferrors := []error{err}
err = processRunnerEnvFileCommand(ctx, envFileCommand, rc, rc.setEnv) ferrors = append(ferrors, processRunnerEnvFileCommand(ctx, envFileCommand, rc, rc.setEnv))
if err != nil { ferrors = append(ferrors, processRunnerEnvFileCommand(ctx, stateFileCommand, rc, rc.saveState))
return err ferrors = append(ferrors, processRunnerEnvFileCommand(ctx, outputFileCommand, rc, rc.setOutput))
} ferrors = append(ferrors, processRunnerSummaryCommand(ctx, summaryFileCommand, rc))
err = processRunnerEnvFileCommand(ctx, stateFileCommand, rc, rc.saveState) ferrors = append(ferrors, rc.UpdateExtraPath(ctx, path.Join(actPath, pathFileCommand)))
if err != nil { return errors.Join(ferrors...)
return err
}
err = processRunnerEnvFileCommand(ctx, outputFileCommand, rc, rc.setOutput)
if err != nil {
return err
}
err = rc.UpdateExtraPath(ctx, path.Join(actPath, pathFileCommand))
if err != nil {
return err
}
if orgerr != nil {
return orgerr
}
return err
} }
} }

View File

@@ -85,6 +85,7 @@ func TestStepActionLocalTest(t *testing.T) {
return nil return nil
}) })
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil) cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
salm.On("runAction", sal, filepath.Clean("/tmp/path/to/action"), (*remoteAction)(nil)).Return(func(_ context.Context) error { salm.On("runAction", sal, filepath.Clean("/tmp/path/to/action"), (*remoteAction)(nil)).Return(func(_ context.Context) error {
@@ -281,6 +282,7 @@ func TestStepActionLocalPost(t *testing.T) {
return nil return nil
}) })
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil) cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
} }

View File

@@ -191,6 +191,7 @@ func TestStepActionRemote(t *testing.T) {
return nil return nil
}) })
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil) cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
} }
@@ -199,8 +200,8 @@ func TestStepActionRemote(t *testing.T) {
err = sar.main()(ctx) err = sar.main()(ctx)
} }
assert.Equal(t, tt.runError, err) assert.ErrorIs(t, err, tt.runError)
assert.Equal(t, tt.result, sar.RunContext.StepResults["step"]) assert.Equal(t, sar.RunContext.StepResults["step"], tt.result)
sarm.AssertExpectations(t) sarm.AssertExpectations(t)
cm.AssertExpectations(t) cm.AssertExpectations(t)
@@ -554,6 +555,7 @@ func TestStepActionRemotePost(t *testing.T) {
return nil return nil
}) })
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil) cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
} }

View File

@@ -93,6 +93,7 @@ func TestStepDockerMain(t *testing.T) {
return nil return nil
}) })
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil) cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
err := sd.main()(ctx) err := sd.main()(ctx)

View File

@@ -74,6 +74,7 @@ func TestStepRun(t *testing.T) {
ctx := context.Background() ctx := context.Background()
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil) cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
err := sr.main()(ctx) err := sr.main()(ctx)

View File

@@ -0,0 +1,27 @@
name: Step Summary Example
on: [push]
jobs:
create_summary:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
# GITHUB_STEP_SUMMARY test
- name: Create Step Summary
uses: actions/github-script@v7
with:
script: |
const summary = `
## Workflow Summary
- **Repository**: ${context.repo.owner}/${context.repo.repo}
- **Branch**: ${context.ref}
- **Commit SHA**: ${context.sha}
- **Event**: ${context.eventName}
`;
console.log('Summary:', summary);
await core.summary.addRaw(summary);
await core.summary.write();
github-token: none