Skip to content

Commit bb140f1

Browse files
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
1 parent 7c0b21a commit bb140f1

File tree

8 files changed

+91
-37
lines changed

8 files changed

+91
-37
lines changed

pkg/runner/command.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/actions-oss/act-cli/pkg/common"
9+
"github.com/sirupsen/logrus"
910
)
1011

1112
var commandPatternGA *regexp.Regexp
@@ -41,11 +42,12 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
4142
}
4243

4344
if resumeCommand != "" && command != resumeCommand {
44-
logger.Infof(" \U00002699 %s", line)
45+
logger.WithFields(logrus.Fields{"command": "ignored", "raw": line}).Infof(" \U00002699 %s", line)
4546
return false
4647
}
4748
arg = unescapeCommandData(arg)
4849
kvPairs = unescapeKvPairs(kvPairs)
50+
defCommandLogger := logger.WithFields(logrus.Fields{"command": command, "kvPairs": kvPairs, "arg": arg, "raw": line})
4951
switch command {
5052
case "set-env":
5153
rc.setEnv(ctx, kvPairs, arg)
@@ -54,27 +56,27 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
5456
case "add-path":
5557
rc.addPath(ctx, arg)
5658
case "debug":
57-
logger.Debugf(" \U0001F4AC %s", line)
59+
defCommandLogger.Debugf(" \U0001F4AC %s", line)
5860
case "warning":
59-
logger.Warnf(" \U0001F6A7 %s", line)
61+
defCommandLogger.Warnf(" \U0001F6A7 %s", line)
6062
case "error":
61-
logger.Errorf(" \U00002757 %s", line)
63+
defCommandLogger.Errorf(" \U00002757 %s", line)
6264
case "add-mask":
6365
rc.AddMask(arg)
64-
logger.Infof(" \U00002699 %s", "***")
66+
defCommandLogger.Infof(" \U00002699 %s", "***")
6567
case "stop-commands":
6668
resumeCommand = arg
67-
logger.Infof(" \U00002699 %s", line)
69+
defCommandLogger.Infof(" \U00002699 %s", line)
6870
case resumeCommand:
6971
resumeCommand = ""
70-
logger.Infof(" \U00002699 %s", line)
72+
defCommandLogger.Infof(" \U00002699 %s", line)
7173
case "save-state":
72-
logger.Infof(" \U0001f4be %s", line)
74+
defCommandLogger.Infof(" \U0001f4be %s", line)
7375
rc.saveState(ctx, kvPairs, arg)
7476
case "add-matcher":
75-
logger.Infof(" \U00002753 add-matcher %s", arg)
77+
defCommandLogger.Infof(" \U00002753 add-matcher %s", arg)
7678
default:
77-
logger.Infof(" \U00002753 %s", line)
79+
defCommandLogger.Infof(" \U00002753 %s", line)
7880
}
7981

8082
return false
@@ -83,7 +85,7 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
8385

8486
func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg string) {
8587
name := kvPairs["name"]
86-
common.Logger(ctx).Infof(" \U00002699 ::set-env:: %s=%s", name, arg)
88+
common.Logger(ctx).WithFields(logrus.Fields{"command": "set-env", "name": name, "arg": arg}).Infof(" \U00002699 ::set-env:: %s=%s", name, arg)
8789
if rc.Env == nil {
8890
rc.Env = make(map[string]string)
8991
}
@@ -115,11 +117,11 @@ func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string,
115117
return
116118
}
117119

118-
logger.Infof(" \U00002699 ::set-output:: %s=%s", outputName, arg)
120+
logger.WithFields(logrus.Fields{"command": "set-output", "name": outputName, "arg": arg}).Infof(" \U00002699 ::set-output:: %s=%s", outputName, arg)
119121
result.Outputs[outputName] = arg
120122
}
121123
func (rc *RunContext) addPath(ctx context.Context, arg string) {
122-
common.Logger(ctx).Infof(" \U00002699 ::add-path:: %s", arg)
124+
common.Logger(ctx).WithFields(logrus.Fields{"command": "add-path", "arg": arg}).Infof(" \U00002699 ::add-path:: %s", arg)
123125
extraPath := []string{arg}
124126
for _, v := range rc.ExtraPath {
125127
if v != arg {

pkg/runner/runner_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ func TestRunEvent(t *testing.T) {
316316
{workdir, "set-env-step-env-override", "push", "", platforms, secrets},
317317
{workdir, "set-env-new-env-file-per-step", "push", "", platforms, secrets},
318318
{workdir, "no-panic-on-invalid-composite-action", "push", "jobs failed due to invalid action", platforms, secrets},
319+
// GITHUB_STEP_SUMMARY
320+
{workdir, "stepsummary", "push", "", platforms, secrets},
319321

320322
// services
321323
{workdir, "services", "push", "", platforms, secrets},
@@ -577,7 +579,9 @@ func TestRunEventHostEnvironment(t *testing.T) {
577579
{workdir, "matrix", "push", "", platforms, secrets},
578580
{workdir, "matrix-include-exclude", "push", "", platforms, secrets},
579581
{workdir, "commands", "push", "", platforms, secrets},
580-
{workdir, "defaults-run", "push", "", platforms, secrets},
582+
// Disabled for now because this test is somewhat invalid
583+
// shell sh is not necessarily bash if the job has no override
584+
// {workdir, "defaults-run", "push", "", platforms, secrets},
581585
{workdir, "composite-fail-with-output", "push", "", platforms, secrets},
582586
{workdir, "issue-597", "push", "", platforms, secrets},
583587
{workdir, "issue-598", "push", "", platforms, secrets},

pkg/runner/step.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package runner
22

33
import (
4+
"archive/tar"
45
"context"
6+
"errors"
57
"fmt"
8+
"io"
69
"path"
710
"strconv"
811
"strings"
@@ -50,6 +53,32 @@ func (s stepStage) String() string {
5053
return "Unknown"
5154
}
5255

56+
func processRunnerSummaryCommand(ctx context.Context, fileName string, rc *RunContext) error {
57+
if common.Dryrun(ctx) {
58+
return nil
59+
}
60+
pathTar, err := rc.JobContainer.GetContainerArchive(ctx, path.Join(rc.JobContainer.GetActPath(), fileName))
61+
if err != nil {
62+
return err
63+
}
64+
defer pathTar.Close()
65+
66+
reader := tar.NewReader(pathTar)
67+
_, err = reader.Next()
68+
if err != nil && err != io.EOF {
69+
return err
70+
}
71+
summary, err := io.ReadAll(reader)
72+
if err != nil {
73+
return err
74+
}
75+
if len(summary) == 0 {
76+
return nil
77+
}
78+
common.Logger(ctx).WithFields(logrus.Fields{"command": "summary", "content": string(summary)}).Infof(" \U00002699 Summary - %s", string(summary))
79+
return nil
80+
}
81+
5382
func processRunnerEnvFileCommand(ctx context.Context, fileName string, rc *RunContext, setter func(context.Context, map[string]string, string)) error {
5483
env := map[string]string{}
5584
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
173202
logger.WithField("stepResult", stepResult.Outcome).Errorf(" \u274C Failure - %s %s", stage, stepString)
174203
}
175204
// Process Runner File Commands
176-
orgerr := err
177-
err = processRunnerEnvFileCommand(ctx, envFileCommand, rc, rc.setEnv)
178-
if err != nil {
179-
return err
180-
}
181-
err = processRunnerEnvFileCommand(ctx, stateFileCommand, rc, rc.saveState)
182-
if err != nil {
183-
return err
184-
}
185-
err = processRunnerEnvFileCommand(ctx, outputFileCommand, rc, rc.setOutput)
186-
if err != nil {
187-
return err
188-
}
189-
err = rc.UpdateExtraPath(ctx, path.Join(actPath, pathFileCommand))
190-
if err != nil {
191-
return err
192-
}
193-
if orgerr != nil {
194-
return orgerr
195-
}
196-
return err
205+
ferrors := []error{err}
206+
ferrors = append(ferrors, processRunnerEnvFileCommand(ctx, envFileCommand, rc, rc.setEnv))
207+
ferrors = append(ferrors, processRunnerEnvFileCommand(ctx, stateFileCommand, rc, rc.saveState))
208+
ferrors = append(ferrors, processRunnerEnvFileCommand(ctx, outputFileCommand, rc, rc.setOutput))
209+
ferrors = append(ferrors, processRunnerSummaryCommand(ctx, summaryFileCommand, rc))
210+
ferrors = append(ferrors, rc.UpdateExtraPath(ctx, path.Join(actPath, pathFileCommand)))
211+
return errors.Join(ferrors...)
197212
}
198213
}
199214

pkg/runner/step_action_local_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestStepActionLocalTest(t *testing.T) {
8585
return nil
8686
})
8787

88+
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
8889
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
8990

9091
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) {
281282
return nil
282283
})
283284

285+
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
284286
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
285287
}
286288

pkg/runner/step_action_remote_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func TestStepActionRemote(t *testing.T) {
191191
return nil
192192
})
193193

194+
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
194195
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
195196
}
196197

@@ -199,8 +200,8 @@ func TestStepActionRemote(t *testing.T) {
199200
err = sar.main()(ctx)
200201
}
201202

202-
assert.Equal(t, tt.runError, err)
203-
assert.Equal(t, tt.result, sar.RunContext.StepResults["step"])
203+
assert.ErrorIs(t, err, tt.runError)
204+
assert.Equal(t, sar.RunContext.StepResults["step"], tt.result)
204205

205206
sarm.AssertExpectations(t)
206207
cm.AssertExpectations(t)
@@ -554,6 +555,7 @@ func TestStepActionRemotePost(t *testing.T) {
554555
return nil
555556
})
556557

558+
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
557559
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
558560
}
559561

pkg/runner/step_docker_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func TestStepDockerMain(t *testing.T) {
9393
return nil
9494
})
9595

96+
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
9697
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
9798

9899
err := sd.main()(ctx)

pkg/runner/step_run_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func TestStepRun(t *testing.T) {
7474

7575
ctx := context.Background()
7676

77+
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/SUMMARY.md").Return(io.NopCloser(&bytes.Buffer{}), nil)
7778
cm.On("GetContainerArchive", ctx, "/var/run/act/workflow/pathcmd.txt").Return(io.NopCloser(&bytes.Buffer{}), nil)
7879

7980
err := sr.main()(ctx)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Step Summary Example
2+
3+
on: [push]
4+
5+
jobs:
6+
create_summary:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v4
11+
12+
# GITHUB_STEP_SUMMARY test
13+
- name: Create Step Summary
14+
uses: actions/github-script@v7
15+
with:
16+
script: |
17+
const summary = `
18+
## Workflow Summary
19+
- **Repository**: ${context.repo.owner}/${context.repo.repo}
20+
- **Branch**: ${context.ref}
21+
- **Commit SHA**: ${context.sha}
22+
- **Event**: ${context.eventName}
23+
`;
24+
console.log('Summary:', summary);
25+
await core.summary.addRaw(summary);
26+
await core.summary.write();
27+
github-token: none

0 commit comments

Comments
 (0)