enhance: report a GitHub-style "Set up job" section (#1089)

Reshapes the job log's "Set up job" section to mirror `actions/runner`:

- runner name/version, then `Runner Information` (labels, task, job, repository, event) and `Operating System` groups
- every required action downloaded up front under `Prepare all required actions`, each as `Download action repository '<action>@<ref>' (SHA:<sha>)`
- `Complete job name` closes the section

Downloading up front is the one behavioral change: the same set was already fetched during the pre stage regardless of a step's `if`, now just before the first pre step, so a download failure is reported against the job rather than a step.

---------

Co-authored-by: silverwind <me@silverwind.io>
Reviewed-on: https://gitea.com/gitea/runner/pulls/1089
Reviewed-by: silverwind <2021+silverwind@noreply.gitea.com>
This commit is contained in:
bircni
2026-07-22 14:58:23 +00:00
parent 46f22c78d2
commit 8af385d147
10 changed files with 612 additions and 4 deletions

View File

@@ -315,7 +315,7 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
}
}()
reporter.Logf("%s(version:%s) received task %v of job %v, be triggered by event: %s", r.name, ver.Version(), task.Id, task.Context.Fields["job"].GetStringValue(), task.Context.Fields["event_name"].GetStringValue())
r.reportSetup(reporter, task)
workflow, jobID, err := generateWorkflow(task)
if err != nil {

83
internal/app/run/setup.go Normal file
View File

@@ -0,0 +1,83 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package run
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"gitea.com/gitea/runner/internal/pkg/report"
"gitea.com/gitea/runner/internal/pkg/ver"
runnerv1 "gitea.dev/actions-proto-go/runner/v1"
)
// osReleasePath describes the host distribution on Linux; absent elsewhere, where the platform
// falls back to the Go runtime alone. A var so tests can point it at a fixture.
var osReleasePath = "/etc/os-release"
// reportSetup opens the job log the way actions/runner opens its "Set up job" step. The action
// downloads and the closing job name are written later, as the job starts.
func (r *Runner) reportSetup(reporter *report.Reporter, task *runnerv1.Task) {
for _, line := range r.setupLines(task) {
reporter.Logf("%s", line)
}
}
// setupLines names the runner, then reports what it was asked to run and the host it runs on, each
// in its own group.
func (r *Runner) setupLines(task *runnerv1.Task) []string {
fields := task.Context.Fields
lines := []string{
fmt.Sprintf("%s(version:%s)", r.name, ver.Version()),
"::group::Runner Information",
}
if names := r.labels.Names(); len(names) > 0 {
lines = append(lines, "Runner labels: "+strings.Join(names, ", "))
}
lines = append(lines,
// The task id correlates the job log with the runner's log and the server's task list.
fmt.Sprintf("Task: %d", task.Id),
"Job: "+fields["job"].GetStringValue(),
"Repository: "+fields["repository"].GetStringValue(),
"Triggered by event: "+fields["event_name"].GetStringValue(),
"::endgroup::",
"::group::Operating System",
)
lines = append(lines, osInfo()...)
return append(lines, "::endgroup::")
}
// osInfo describes the host the runner executes on.
func osInfo() []string {
lines := make([]string, 0, 2)
if name := prettyOSName(); name != "" {
lines = append(lines, name)
}
return append(lines, fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
}
// prettyOSName reads PRETTY_NAME (e.g. "Ubuntu 24.04.4 LTS") from os-release, or "" when absent.
func prettyOSName() string {
data, err := os.ReadFile(osReleasePath)
if err != nil {
return ""
}
for line := range strings.SplitSeq(string(data), "\n") {
key, value, ok := strings.Cut(strings.TrimSpace(line), "=")
if !ok || key != "PRETTY_NAME" {
continue
}
// Values are shell-quoted, but the quotes are optional.
if unquoted, err := strconv.Unquote(value); err == nil {
return unquoted
}
return value
}
return ""
}

View File

@@ -0,0 +1,103 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package run
import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"gitea.com/gitea/runner/internal/pkg/labels"
"gitea.com/gitea/runner/internal/pkg/ver"
runnerv1 "gitea.dev/actions-proto-go/runner/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/structpb"
)
func TestSetupLines(t *testing.T) {
original := osReleasePath
path := filepath.Join(t.TempDir(), "os-release")
require.NoError(t, os.WriteFile(path, []byte("PRETTY_NAME=\"Ubuntu 24.04.4 LTS\"\n"), 0o600))
osReleasePath = path
defer func() { osReleasePath = original }()
r := &Runner{
name: "gitea-com-gitea-0003",
labels: labels.Labels{
{Name: "ubuntu-latest", Schema: labels.SchemeDocker, Arg: "//node:20"},
{Name: "ubuntu-22.04", Schema: labels.SchemeDocker, Arg: "//node:20"},
},
}
taskCtx, err := structpb.NewStruct(map[string]any{
"job": "lint",
"repository": "gitea/runner",
"event_name": "pull_request",
})
require.NoError(t, err)
assert.Equal(t, []string{
"gitea-com-gitea-0003(version:" + ver.Version() + ")",
"::group::Runner Information",
"Runner labels: ubuntu-latest, ubuntu-22.04",
"Task: 268506",
"Job: lint",
"Repository: gitea/runner",
"Triggered by event: pull_request",
"::endgroup::",
"::group::Operating System",
"Ubuntu 24.04.4 LTS",
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
"::endgroup::",
}, r.setupLines(&runnerv1.Task{Id: 268506, Context: taskCtx}))
}
func TestPrettyOSName(t *testing.T) {
tests := map[string]struct {
osRelease string
want string
}{
"quoted value": {
osRelease: "NAME=\"Ubuntu\"\nVERSION_ID=\"24.04\"\nPRETTY_NAME=\"Ubuntu 24.04.4 LTS\"\n",
want: "Ubuntu 24.04.4 LTS",
},
"unquoted value": {
osRelease: "PRETTY_NAME=Alpine Linux v3.21\n",
want: "Alpine Linux v3.21",
},
"no pretty name": {
osRelease: "NAME=\"Ubuntu\"\nVERSION_ID=\"24.04\"\n",
want: "",
},
// A key that merely ends in PRETTY_NAME must not be mistaken for it.
"similar key": {
osRelease: "IMAGE_PRETTY_NAME=\"Ubuntu Core 24\"\n",
want: "",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "os-release")
require.NoError(t, os.WriteFile(path, []byte(tt.osRelease), 0o600))
original := osReleasePath
osReleasePath = path
defer func() { osReleasePath = original }()
assert.Equal(t, tt.want, prettyOSName())
})
}
t.Run("missing file", func(t *testing.T) {
original := osReleasePath
osReleasePath = filepath.Join(t.TempDir(), "absent")
defer func() { osReleasePath = original }()
assert.Empty(t, prettyOSName())
})
}