mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-18 22:03:46 +00:00
enhance: report the steps of a task as they run (#1293)
Gitea derives the steps of a job from the workflow file, so the `pre` and `post` steps of actions only show up inside "Set up job" and "Complete job", see https://github.com/go-gitea/gitea/issues/24604. The runner now also reports the steps as it runs them (https://gitea.com/gitea/actions-proto-def/pulls/19): "Set up job", a "Pre" and "Post" step for each action that has one, the workflow steps with their evaluated names, and "Complete job", as actions/runner shows them. The per-step state Gitea reads today is unchanged, and the job log only moves "Complete job name" and "Matrix:" ahead of the pre steps, so older Gitea versions keep showing everything as before. Job hooks and containers stay part of "Set up job" and "Complete job". The `replace` of `gitea.dev/actionslib` is swapped for the release once the proto change is merged. Reviewed-on: https://gitea.com/gitea/runner/pulls/1293 Reviewed-by: Zettat123 <[email protected]> Co-authored-by: bircni <[email protected]>
This commit is contained in:
1 parent
20c8b4abc4
commit
160e18e705
11 files changed
+497
-47
No files matched your search
@@ -4,6 +4,7 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -21,6 +22,7 @@ import (
|
||||
"gitea.com/gitea/runner/internal/pkg/metrics"
|
||||
|
||||
"connectrpc.com/connect"
|
||||
"gitea.dev/actionslib/pkg/model"
|
||||
runnerv1 "gitea.dev/actionslib/runner/v1"
|
||||
"github.com/avast/retry-go/v5"
|
||||
"google.golang.org/protobuf/proto"
|
||||
@@ -62,6 +64,11 @@ type Reporter struct {
|
||||
|
||||
state *runnerv1.TaskState
|
||||
stateChanged bool
|
||||
// The steps in state.ReportedSteps by stage and workflow step number, the step the next log row
|
||||
// without a step of its own belongs to, and the workflow steps. Guarded by stateMu.
|
||||
reportedSteps map[reportedStepKey]*runnerv1.StepState
|
||||
currentReportedStep *runnerv1.StepState
|
||||
stepModels []*model.Step
|
||||
// reportFailing keeps an outage to one log line at each end.
|
||||
reportFailing map[string]bool
|
||||
// serverResult is what the server decided, e.g. the zombie reaper failing
|
||||
@@ -157,14 +164,152 @@ func (r *Reporter) Result() runnerv1.Result {
|
||||
return r.state.Result
|
||||
}
|
||||
|
||||
func (r *Reporter) ResetSteps(l int) {
|
||||
// ResetSteps sets the steps of the workflow job. They are reported both by their index in the
|
||||
// workflow file, for Gitea versions deriving the steps from the file, and as the steps the runner
|
||||
// runs, together with the setup, pre, post and cleanup steps, for Gitea versions that show those.
|
||||
func (r *Reporter) ResetSteps(steps []*model.Step) {
|
||||
r.stateMu.Lock()
|
||||
defer r.stateMu.Unlock()
|
||||
for i := range l {
|
||||
for i := range steps {
|
||||
r.state.Steps = append(r.state.Steps, &runnerv1.StepState{
|
||||
Id: int64(i),
|
||||
})
|
||||
}
|
||||
if len(steps) == 0 {
|
||||
return // a reusable workflow caller, whose called jobs log their own steps through it
|
||||
}
|
||||
r.stepModels = steps
|
||||
r.reportedSteps = map[reportedStepKey]*runnerv1.StepState{}
|
||||
setup := r.addReportedStep(len(r.state.ReportedSteps), runnerv1.StepStage_STEP_STAGE_SETUP, 0, "Set up job")
|
||||
setup.StartedAt = cmp.Or(r.state.StartedAt, timestamppb.Now())
|
||||
setup.LogLength = int64(r.logOffset + len(r.logRows)) // the runner's own lines logged before the job was parsed
|
||||
r.currentReportedStep = setup
|
||||
for i, step := range steps {
|
||||
r.addReportedStep(len(r.state.ReportedSteps), runnerv1.StepStage_STEP_STAGE_MAIN, i, r.mask(stepDisplayName(step, "")))
|
||||
}
|
||||
r.addReportedStep(len(r.state.ReportedSteps), runnerv1.StepStage_STEP_STAGE_CLEANUP, 0, "Complete job")
|
||||
}
|
||||
|
||||
type reportedStepKey struct {
|
||||
stage runnerv1.StepStage
|
||||
number int
|
||||
}
|
||||
|
||||
// stepDisplayName names a workflow step like Gitea and GitHub do, prefixing an unnamed one with "Run".
|
||||
// evaluated is step.String() with its expressions evaluated, empty while the step has not run yet.
|
||||
func stepDisplayName(step *model.Step, evaluated string) string {
|
||||
if step.Name != "" {
|
||||
return cmp.Or(evaluated, step.Name)
|
||||
}
|
||||
first, _, _ := strings.Cut(strings.TrimSpace(cmp.Or(evaluated, step.String())), "\n")
|
||||
return "Run " + cmp.Or(strings.TrimSpace(first), step.String())
|
||||
}
|
||||
|
||||
// addReportedStep inserts a step at pos of the reported steps. Caller holds stateMu.
|
||||
func (r *Reporter) addReportedStep(pos int, stage runnerv1.StepStage, number int, name string) *runnerv1.StepState {
|
||||
step := &runnerv1.StepState{Name: name, Stage: stage, Number: int64(number)}
|
||||
r.state.ReportedSteps = slices.Insert(r.state.ReportedSteps, pos, step)
|
||||
for i, v := range r.state.ReportedSteps[pos:] {
|
||||
v.Id = int64(pos + i)
|
||||
}
|
||||
r.reportedSteps[reportedStepKey{stage, number}] = step
|
||||
return step
|
||||
}
|
||||
|
||||
// reportedStepOf returns the reported step an entry belongs to, adding a pre or post step the
|
||||
// first time it runs, and whether the entry names that step itself. Caller holds stateMu.
|
||||
func (r *Reporter) reportedStepOf(entry logging.Record) (*runnerv1.StepState, bool) {
|
||||
if r.reportedSteps == nil {
|
||||
return nil, false
|
||||
}
|
||||
stage, _ := recordAttr[string](entry, "stage")
|
||||
number, ok := recordAttr[int](entry, "stepNumber")
|
||||
if stage == runner.StageComplete {
|
||||
return r.reportedSteps[reportedStepKey{stage: runnerv1.StepStage_STEP_STAGE_CLEANUP}], false
|
||||
}
|
||||
if !ok || number < 0 || number >= len(r.stepModels) {
|
||||
return r.currentReportedStep, false
|
||||
}
|
||||
var key reportedStepKey
|
||||
switch stage {
|
||||
case runner.StagePre:
|
||||
key = reportedStepKey{runnerv1.StepStage_STEP_STAGE_PRE, number}
|
||||
case runner.StageMain:
|
||||
key = reportedStepKey{runnerv1.StepStage_STEP_STAGE_MAIN, number}
|
||||
case runner.StagePost:
|
||||
key = reportedStepKey{runnerv1.StepStage_STEP_STAGE_POST, number}
|
||||
default:
|
||||
return r.currentReportedStep, false
|
||||
}
|
||||
main, ok := r.reportedSteps[reportedStepKey{runnerv1.StepStage_STEP_STAGE_MAIN, number}]
|
||||
if !ok {
|
||||
return r.currentReportedStep, false
|
||||
}
|
||||
if evaluated, _ := recordAttr[string](entry, "step"); evaluated != "" {
|
||||
// evaluated as GitHub shows it, and masked as an expression can name a secret
|
||||
main.Name = r.mask(stepDisplayName(r.stepModels[number], evaluated))
|
||||
}
|
||||
if step, ok := r.reportedSteps[key]; ok {
|
||||
return step, true
|
||||
}
|
||||
if started, _ := recordAttr[bool](entry, runner.StepStartedField); !started {
|
||||
return r.currentReportedStep, false // a pre or post step that does not run is not shown
|
||||
}
|
||||
mainName := main.Name
|
||||
if stage == runner.StagePre {
|
||||
// pre steps run in the order of the workflow steps, so each goes right before the main steps
|
||||
pos := slices.IndexFunc(r.state.ReportedSteps, func(v *runnerv1.StepState) bool { return v.Stage == runnerv1.StepStage_STEP_STAGE_MAIN })
|
||||
return r.addReportedStep(pos, key.stage, number, "Pre "+mainName), true
|
||||
}
|
||||
return r.addReportedStep(len(r.state.ReportedSteps)-1, key.stage, number, "Post "+mainName), true
|
||||
}
|
||||
|
||||
// recordAttr returns the attribute key of rec if it is a T.
|
||||
func recordAttr[T any](rec logging.Record, key string) (T, bool) {
|
||||
v, _ := rec.Attr(key)
|
||||
t, ok := v.(T)
|
||||
return t, ok
|
||||
}
|
||||
|
||||
// enterReportedStep makes step the one log rows without a step of their own belong to, finishing
|
||||
// the setup step once another step starts. It reports whether the step started. Caller holds stateMu.
|
||||
func (r *Reporter) enterReportedStep(step *runnerv1.StepState, timestamp time.Time) bool {
|
||||
if prev := r.currentReportedStep; step != prev && prev.Stage == runnerv1.StepStage_STEP_STAGE_SETUP && prev.StoppedAt == nil {
|
||||
if prev.Result == runnerv1.Result_RESULT_UNSPECIFIED {
|
||||
prev.Result = runnerv1.Result_RESULT_SUCCESS
|
||||
}
|
||||
prev.StoppedAt = timestamppb.New(timestamp)
|
||||
}
|
||||
r.currentReportedStep = step
|
||||
if step.StartedAt != nil {
|
||||
return false
|
||||
}
|
||||
step.StartedAt = timestamppb.New(timestamp)
|
||||
return true
|
||||
}
|
||||
|
||||
// finishReportedSteps gives a result to the reported steps that have none when the job ends:
|
||||
// the job's result to a step that was running, except the cleanup that ran in full. A running
|
||||
// cleanup is left running unless closing, as it still logs after the job result. Caller holds stateMu.
|
||||
func (r *Reporter) finishReportedSteps(jobResult runnerv1.Result, timestamp time.Time, closing bool) {
|
||||
for _, v := range r.state.ReportedSteps {
|
||||
if v.Result != runnerv1.Result_RESULT_UNSPECIFIED || !closing && v.StartedAt != nil && v.Stage == runnerv1.StepStage_STEP_STAGE_CLEANUP {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case v.StartedAt == nil && jobResult == runnerv1.Result_RESULT_CANCELLED:
|
||||
v.Result = runnerv1.Result_RESULT_CANCELLED
|
||||
case v.StartedAt == nil:
|
||||
v.Result = runnerv1.Result_RESULT_SKIPPED // never reached, such as the steps after a failed setup
|
||||
case v.Stage == runnerv1.StepStage_STEP_STAGE_CLEANUP:
|
||||
v.Result = runnerv1.Result_RESULT_SUCCESS
|
||||
default:
|
||||
v.Result = jobResult
|
||||
}
|
||||
if v.StartedAt != nil {
|
||||
v.StoppedAt = timestamppb.New(timestamp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Enabled makes the Reporter see every record, the job logger's level filter runs before it.
|
||||
@@ -200,6 +345,12 @@ func (r *Reporter) appendLogRow(row *runnerv1.LogRow) {
|
||||
return
|
||||
}
|
||||
row.Content = r.mask(row.Content)
|
||||
if step := r.currentReportedStep; step != nil {
|
||||
if step.LogLength == 0 {
|
||||
step.LogIndex = int64(r.logOffset + len(r.logRows))
|
||||
}
|
||||
step.LogLength++
|
||||
}
|
||||
r.logRows = append(r.logRows, row)
|
||||
r.jobLog.write(row.Time.AsTime(), row.Content)
|
||||
}
|
||||
@@ -266,9 +417,26 @@ func (r *Reporter) Handle(_ context.Context, entry logging.Record) error {
|
||||
r.state.StartedAt = timestamppb.New(timestamp)
|
||||
}
|
||||
|
||||
if reported, own := r.reportedStepOf(entry); reported != nil {
|
||||
if r.enterReportedStep(reported, timestamp) {
|
||||
urgentState = true
|
||||
}
|
||||
if !own && entry.Level >= logging.LevelError && reported.Result == runnerv1.Result_RESULT_UNSPECIFIED {
|
||||
reported.Result = runnerv1.Result_RESULT_FAILURE // a job level error, such as an action that cannot be downloaded
|
||||
urgentState = true
|
||||
}
|
||||
if v, ok := entry.Attr("stepResult"); ok && own && isJobStepEntry(entry) {
|
||||
if stepResult, ok := r.parseResult(v); ok {
|
||||
reported.Result = stepResult
|
||||
reported.StoppedAt = timestamppb.New(timestamp)
|
||||
urgentState = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage, _ := entry.Attr("stage")
|
||||
|
||||
if stage != "Main" {
|
||||
if stage != runner.StageMain {
|
||||
if v, ok := entry.Attr("jobResult"); ok {
|
||||
if jobResult, ok := r.parseResult(v); ok {
|
||||
// We need to ensure log upload before this upload
|
||||
@@ -282,6 +450,7 @@ func (r *Reporter) Handle(_ context.Context, entry logging.Record) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
r.finishReportedSteps(jobResult, timestamp, false)
|
||||
urgentState = true
|
||||
}
|
||||
}
|
||||
@@ -549,6 +718,7 @@ func (r *Reporter) Close(lastWords string) error {
|
||||
Content: lastWords,
|
||||
})
|
||||
}
|
||||
r.finishReportedSteps(r.state.Result, time.Now(), true)
|
||||
r.stateMu.Unlock()
|
||||
|
||||
// Wake up the daemon loop so it detects closed promptly.
|
||||
|
||||
Reference in new issue
Block a user