Skip to content

Commit e100e30

Browse files
earl-warrenChristopherHX
andcommitted
feat: log parsed commands and step summary (#824)
Refs nektos/act#2761 --- * feat: log parsed command data in json logger * Could be used to upload the GITHUB_STEP_SUMMARY by downstream Projects * You can see the summary and other commands * Access the raw line of most commands * Update step.go * Update step.go * Update push.yml * . --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> (cherry picked from commit bb7db7b1c8a456d46907f3e65a6c4c2c1dcb6286) ``` Conflicts: act/runner/command.go act/runner/runner_test.go trivial context conflicts for both ``` <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - features - [PR](https://code.forgejo.org/forgejo/runner/pulls/824): <!--number 824 --><!--line 0 --><!--description ZmVhdDogbG9nIHBhcnNlZCBjb21tYW5kcyBhbmQgc3RlcCBzdW1tYXJ5-->feat: log parsed commands and step summary<!--description--> <!--end release-notes-assistant--> Co-authored-by: ChristopherHX <[email protected]> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/824 Reviewed-by: Gusted <[email protected]>
1 parent 5f1224b commit e100e30

File tree

8 files changed

+90
-36
lines changed

8 files changed

+90
-36
lines changed

act/runner/command.go

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

88
"code.forgejo.org/forgejo/runner/v9/act/common"
9+
10+
"github.com/sirupsen/logrus"
911
)
1012

1113
var (
@@ -43,11 +45,12 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
4345
}
4446

4547
if resumeCommand != "" && command != resumeCommand {
46-
logger.Infof(" \U00002699 %s", line)
48+
logger.WithFields(logrus.Fields{"command": "ignored", "raw": line}).Infof(" \U00002699 %s", line)
4749
return false
4850
}
4951
arg = unescapeCommandData(arg)
5052
kvPairs = unescapeKvPairs(kvPairs)
53+
defCommandLogger := logger.WithFields(logrus.Fields{"command": command, "kvPairs": kvPairs, "arg": arg, "raw": line})
5154
switch command {
5255
case "set-env":
5356
rc.setEnv(ctx, kvPairs, arg)
@@ -56,27 +59,27 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
5659
case "add-path":
5760
rc.addPath(ctx, arg)
5861
case "debug":
59-
logger.Infof(" \U0001F4AC %s", line)
62+
defCommandLogger.Debugf(" \U0001F4AC %s", line)
6063
case "warning":
61-
logger.Infof(" \U0001F6A7 %s", line)
64+
defCommandLogger.Warnf(" \U0001F6A7 %s", line)
6265
case "error":
63-
logger.Infof(" \U00002757 %s", line)
66+
defCommandLogger.Errorf(" \U00002757 %s", line)
6467
case "add-mask":
6568
rc.AddMask(arg)
66-
logger.Infof(" \U00002699 %s", "***")
69+
defCommandLogger.Infof(" \U00002699 %s", "***")
6770
case "stop-commands":
6871
resumeCommand = arg
69-
logger.Infof(" \U00002699 %s", line)
72+
defCommandLogger.Infof(" \U00002699 %s", line)
7073
case resumeCommand:
7174
resumeCommand = ""
72-
logger.Infof(" \U00002699 %s", line)
75+
defCommandLogger.Infof(" \U00002699 %s", line)
7376
case "save-state":
74-
logger.Infof(" \U0001f4be %s", line)
77+
defCommandLogger.Infof(" \U0001f4be %s", line)
7578
rc.saveState(ctx, kvPairs, arg)
7679
case "add-matcher":
77-
logger.Infof(" \U00002753 add-matcher %s", arg)
80+
defCommandLogger.Infof(" \U00002753 add-matcher %s", arg)
7881
default:
79-
logger.Infof(" \U00002753 %s", line)
82+
defCommandLogger.Infof(" \U00002753 %s", line)
8083
}
8184

8285
// return true to let gitea's logger handle these special outputs also
@@ -86,7 +89,7 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
8689

8790
func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg string) {
8891
name := kvPairs["name"]
89-
common.Logger(ctx).Infof(" \U00002699 ::set-env:: %s=%s", name, arg)
92+
common.Logger(ctx).WithFields(logrus.Fields{"command": "set-env", "name": name, "arg": arg}).Infof(" \U00002699 ::set-env:: %s=%s", name, arg)
9093
if rc.Env == nil {
9194
rc.Env = make(map[string]string)
9295
}
@@ -119,12 +122,12 @@ func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string,
119122
return
120123
}
121124

122-
logger.Infof(" \U00002699 ::set-output:: %s=%s", outputName, arg)
125+
logger.WithFields(logrus.Fields{"command": "set-output", "name": outputName, "arg": arg}).Infof(" \U00002699 ::set-output:: %s=%s", outputName, arg)
123126
result.Outputs[outputName] = arg
124127
}
125128

126129
func (rc *RunContext) addPath(ctx context.Context, arg string) {
127-
common.Logger(ctx).Infof(" \U00002699 ::add-path:: %s", arg)
130+
common.Logger(ctx).WithFields(logrus.Fields{"command": "add-path", "arg": arg}).Infof(" \U00002699 ::add-path:: %s", arg)
128131
extraPath := []string{arg}
129132
for _, v := range rc.ExtraPath {
130133
if v != arg {

act/runner/runner_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ func TestRunner_RunEvent(t *testing.T) {
317317
{workdir, "set-env-step-env-override", "push", "", platforms, secrets},
318318
{workdir, "set-env-new-env-file-per-step", "push", "", platforms, secrets},
319319
{workdir, "no-panic-on-invalid-composite-action", "push", "missing steps in composite action", platforms, secrets},
320+
{workdir, "stepsummary", "push", "", platforms, secrets},
320321
{workdir, "tool-cache", "push", "", platforms, secrets},
321322

322323
// services

act/runner/step.go

Lines changed: 38 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"
@@ -12,6 +15,8 @@ import (
1215
"code.forgejo.org/forgejo/runner/v9/act/container"
1316
"code.forgejo.org/forgejo/runner/v9/act/exprparser"
1417
"code.forgejo.org/forgejo/runner/v9/act/model"
18+
19+
"github.com/sirupsen/logrus"
1520
)
1621

1722
type step interface {
@@ -49,6 +54,32 @@ func (s stepStage) String() string {
4954
return "Unknown"
5055
}
5156

57+
func processRunnerSummaryCommand(ctx context.Context, fileName string, rc *RunContext) error {
58+
if common.Dryrun(ctx) {
59+
return nil
60+
}
61+
pathTar, err := rc.JobContainer.GetContainerArchive(ctx, path.Join(rc.JobContainer.GetActPath(), fileName))
62+
if err != nil {
63+
return err
64+
}
65+
defer pathTar.Close()
66+
67+
reader := tar.NewReader(pathTar)
68+
_, err = reader.Next()
69+
if err != nil && err != io.EOF {
70+
return err
71+
}
72+
summary, err := io.ReadAll(reader)
73+
if err != nil {
74+
return err
75+
}
76+
if len(summary) == 0 {
77+
return nil
78+
}
79+
common.Logger(ctx).WithFields(logrus.Fields{"command": "summary", "content": string(summary)}).Infof(" \U00002699 Summary - %s", string(summary))
80+
return nil
81+
}
82+
5283
func processRunnerEnvFileCommand(ctx context.Context, fileName string, rc *RunContext, setter func(context.Context, map[string]string, string)) error {
5384
env := map[string]string{}
5485
err := rc.JobContainer.UpdateFromEnv(path.Join(rc.JobContainer.GetActPath(), fileName), &env)(ctx)
@@ -171,27 +202,13 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
171202
logger.WithField("stepResult", stepResult.Outcome).Errorf(" \u274C Failure - %s %s", stage, stepString)
172203
}
173204
// Process Runner File Commands
174-
orgerr := err
175-
err = processRunnerEnvFileCommand(ctx, envFileCommand, rc, rc.setEnv)
176-
if err != nil {
177-
return err
178-
}
179-
err = processRunnerEnvFileCommand(ctx, stateFileCommand, rc, rc.saveState)
180-
if err != nil {
181-
return err
182-
}
183-
err = processRunnerEnvFileCommand(ctx, outputFileCommand, rc, rc.setOutput)
184-
if err != nil {
185-
return err
186-
}
187-
err = rc.UpdateExtraPath(ctx, path.Join(actPath, pathFileCommand))
188-
if err != nil {
189-
return err
190-
}
191-
if orgerr != nil {
192-
return orgerr
193-
}
194-
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...)
195212
}
196213
}
197214

act/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(ctx context.Context) error {
@@ -280,6 +281,7 @@ func TestStepActionLocalPost(t *testing.T) {
280281
return nil
281282
})
282283

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

act/runner/step_action_remote_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ func TestStepActionRemoteOK(t *testing.T) {
180180
return nil
181181
})
182182

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

@@ -188,9 +189,9 @@ func TestStepActionRemoteOK(t *testing.T) {
188189
err = sar.main()(ctx)
189190
}
190191

191-
assert.Equal(t, tt.runError, err)
192+
assert.ErrorIs(t, err, tt.runError)
192193
assert.Equal(t, tt.mocks.cloned, clonedAction)
193-
assert.Equal(t, tt.result, sar.RunContext.StepResults["step"])
194+
assert.Equal(t, sar.RunContext.StepResults["step"], tt.result)
194195

195196
sarm.AssertExpectations(t)
196197
cm.AssertExpectations(t)
@@ -508,6 +509,7 @@ func TestStepActionRemotePost(t *testing.T) {
508509
return nil
509510
})
510511

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

act/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)

act/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: https://data.forgejo.org/actions/checkout@v4
11+
12+
# GITHUB_STEP_SUMMARY test
13+
- name: Create Step Summary
14+
uses: https://github.com/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)