fix(actions): keep runner_name after ephemeral runner removal (#39725)

Jobs that ran on ephemeral runners lose `runner_name` in the
`workflow_job` completed webhook and the jobs API, because the runner
row is deleted when the task finishes. GitHub keeps both `runner_id` and
`runner_name`, and GARM ignores completed jobs without a name, holding
the runner slot until its periodic cleanup.

The name is now stored on the task when claimed, and a migration
backfills tasks whose runner row still exists.

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-11-13 01:18:57 +00:00
1 parent 82debf7008
commit dc9a2aefdc
9 files changed
+113 -43

No files matched your search

+1
View File
@@ -445,6 +445,7 @@ func prepareMigrationTasks() []*migration {
newMigration(366, "Drop user_open_id table", v29.DropUserOpenIDTable),
newMigration(367, "Add latest_release_id to repository table", v29.AddLatestReleaseIDToRepository),
newMigration(368, "Add unique index for team name", v29.AddUniqueTeamNameIndex),
newMigration(369, "Add runner_name to action_task", v29.AddRunnerNameToActionTask),
}
return preparedMigrations
}
+35
View File
@@ -0,0 +1,35 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v29
import (
"context"
"gitea.dev/modelmigration/base"
"xorm.io/xorm"
)
func AddRunnerNameToActionTask(_ context.Context, x base.EngineMigration) error {
type ActionTask struct {
RunnerName string `xorm:"VARCHAR(255) NOT NULL DEFAULT ''"`
}
if _, err := x.SyncWithOptions(xorm.SyncOptions{
IgnoreDropIndices: true,
IgnoreConstrains: true,
}, new(ActionTask)); err != nil {
return err
}
_, err := x.Exec(`
UPDATE action_task SET runner_name = (
SELECT action_runner.name FROM action_runner WHERE action_runner.id = action_task.runner_id
)
WHERE runner_name = ''
AND EXISTS (
SELECT 1 FROM action_runner WHERE action_runner.id = action_task.runner_id AND action_runner.name IS NOT NULL
);`) // soft-deleted runners are included to keep their names
return err
}
+47
View File
@@ -0,0 +1,47 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package v29
import (
"testing"
"gitea.dev/modelmigration/migrationtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAddRunnerNameToActionTask(t *testing.T) {
type ActionTask struct {
ID int64 `xorm:"pk"`
RunnerID int64 `xorm:"index"`
}
type ActionRunner struct {
ID int64 `xorm:"pk"`
Name string `xorm:"VARCHAR(255)"`
Deleted int64
}
x, deferrable := migrationtest.PrepareTestEnv(t, 0, new(ActionTask), new(ActionRunner))
defer deferrable()
if x == nil || t.Failed() {
return
}
_, err := x.Insert(
&ActionRunner{ID: 1, Name: "live-runner"},
&ActionRunner{ID: 2, Name: "deleted-runner", Deleted: 1},
&ActionTask{ID: 1, RunnerID: 1},
&ActionTask{ID: 2, RunnerID: 2},
&ActionTask{ID: 3, RunnerID: 3},
&ActionTask{ID: 4, RunnerID: 0},
)
require.NoError(t, err)
require.NoError(t, AddRunnerNameToActionTask(t.Context(), x))
var names []string
require.NoError(t, x.Table("action_task").Cols("runner_name").Asc("id").Find(&names))
assert.Equal(t, []string{"live-runner", "deleted-runner", "", ""}, names)
}
-8
View File
@@ -207,14 +207,6 @@ func init() {
db.RegisterModel(&ActionRunner{})
}
func GetRunnersMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionRunner, error) {
runners := make(map[int64]*ActionRunner, len(ids))
if len(ids) == 0 {
return runners, nil
}
return runners, db.GetEngine(ctx).In("id", ids).Find(&runners)
}
// FindRunnerOptions
// ownerID == 0 and repoID == 0 means any runner including global runners
// repoID != 0 and WithAvailable == false means any runner for the given repo
+11 -9
View File
@@ -27,15 +27,16 @@ import (
// ActionTask represents a distribution of job
type ActionTask struct {
ID int64
JobID int64
Job *ActionRunJob `xorm:"-"`
Steps []*ActionTaskStep `xorm:"-"`
Attempt int64
RunnerID int64 `xorm:"index"`
Status Status `xorm:"index"`
Started timeutil.TimeStamp `xorm:"index"`
Stopped timeutil.TimeStamp `xorm:"index(stopped_log_expired)"`
ID int64
JobID int64
Job *ActionRunJob `xorm:"-"`
Steps []*ActionTaskStep `xorm:"-"`
Attempt int64
RunnerID int64 `xorm:"index"`
RunnerName string `xorm:"VARCHAR(255) NOT NULL DEFAULT ''"`
Status Status `xorm:"index"`
Started timeutil.TimeStamp `xorm:"index"`
Stopped timeutil.TimeStamp `xorm:"index(stopped_log_expired)"`
RepoID int64 `xorm:"index"`
OwnerID int64 `xorm:"index"`
@@ -347,6 +348,7 @@ func claimJobForRunner(ctx context.Context, runner *ActionRunner, job *ActionRun
JobID: job.ID,
Attempt: job.Attempt,
RunnerID: runner.ID,
RunnerName: runner.Name,
Started: now,
Status: StatusRunning,
RepoID: job.RepoID,
+1
View File
@@ -333,6 +333,7 @@ func TestReleaseTaskForRunner(t *testing.T) {
} else {
require.Equal(t, StatusRunning, claimed.Status)
require.Equal(t, task.ID, claimed.TaskID)
assert.Equal(t, runner.Name, unittest.AssertExistsAndLoadBean(t, &ActionTask{ID: task.ID}).RunnerName)
require.NoError(t, ReleaseTaskForRunner(t.Context(), task))
+8 -6
View File
@@ -143,11 +143,12 @@ func TestToActionWorkflowJob_StepStatusAndBatchParity(t *testing.T) {
require.NoError(t, db.Insert(ctx, run))
task := &actions_model.ActionTask{
ID: 900102,
JobID: 9001,
RepoID: 2,
RunnerID: 34346,
Status: actions_model.StatusFailure,
ID: 900102,
JobID: 9001,
RepoID: 2,
RunnerID: 987654321,
RunnerName: "removed-runner",
Status: actions_model.StatusFailure,
}
require.NoError(t, db.Insert(ctx, task))
@@ -174,7 +175,8 @@ func TestToActionWorkflowJob_StepStatusAndBatchParity(t *testing.T) {
require.NoError(t, err)
require.Len(t, apiJob.Steps, 2)
assert.Equal(t, "runner_to_be_deleted-user", apiJob.RunnerName)
assert.EqualValues(t, 987654321, apiJob.RunnerID)
assert.Equal(t, "removed-runner", apiJob.RunnerName)
assert.Equal(t, "completed", apiJob.Steps[0].Status, "step 0 status")
assert.Equal(t, "success", apiJob.Steps[0].Conclusion, "step 0 conclusion (succeeded before the failure)")
assert.Equal(t, "completed", apiJob.Steps[1].Status, "step 1 status")
+4 -20
View File
@@ -491,7 +491,6 @@ func ToActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task
return nil, err
}
var runner *actions_model.ActionRunner
if effectiveTaskID := job.EffectiveTaskID(); effectiveTaskID != 0 {
if task == nil {
task, _, err = db.GetByID[actions_model.ActionTask](ctx, effectiveTaskID)
@@ -504,10 +503,9 @@ func ToActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task
if err := (actions_model.TaskList{task}).LoadSteps(ctx); err != nil {
return nil, err
}
runner, _, _ = db.GetByID[actions_model.ActionRunner](ctx, task.RunnerID)
}
}
return toActionWorkflowJob(ctx, repo, task, runner, job), nil
return toActionWorkflowJob(ctx, repo, task, job), nil
}
// ToActionWorkflowJobs requires job.Run.Repo to be loaded
@@ -523,26 +521,14 @@ func ToActionWorkflowJobs(ctx context.Context, jobs []*actions_model.ActionRunJo
if err := taskList.LoadSteps(ctx); err != nil {
return nil, err
}
runners, err := actions_model.GetRunnersMapByIDs(ctx, container.FilterSlice(taskList, func(task *actions_model.ActionTask) (int64, bool) {
return task.RunnerID, task.RunnerID != 0
}))
if err != nil {
return nil, err
}
result := make([]*api.ActionWorkflowJob, len(jobs))
for i, job := range jobs {
task := tasks[job.EffectiveTaskID()]
var runner *actions_model.ActionRunner
if task != nil {
runner = runners[task.RunnerID]
}
result[i] = toActionWorkflowJob(ctx, job.Run.Repo, task, runner, job)
result[i] = toActionWorkflowJob(ctx, job.Run.Repo, tasks[job.EffectiveTaskID()], job)
}
return result, nil
}
func toActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task *actions_model.ActionTask, runner *actions_model.ActionRunner, job *actions_model.ActionRunJob) *api.ActionWorkflowJob {
func toActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task *actions_model.ActionTask, job *actions_model.ActionRunJob) *api.ActionWorkflowJob {
status, conclusion := ToRunActionsStatus(job.Run, job.Status)
var runnerID int64
var runnerName string
@@ -550,9 +536,7 @@ func toActionWorkflowJob(ctx context.Context, repo *repo_model.Repository, task
if task != nil && job.EffectiveTaskID() != 0 {
runnerID = task.RunnerID
if runner != nil {
runnerName = runner.Name
}
runnerName = task.RunnerName
for i, step := range task.Steps {
stepStatus, stepConclusion := ToActionsStatus(step.Status)
steps = append(steps, &api.ActionWorkflowStep{
+6
View File
@@ -588,6 +588,12 @@ jobs:
})
assert.NoError(t, err)
unittest.AssertNotExistsBean(t, &actions_model.ActionRunner{ID: actionTask.RunnerID})
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/actions/jobs/%d", baseRepo.OwnerName, baseRepo.Name, actionRunJob.ID).AddTokenAuth(user2Token)
apiJob := DecodeJSON(t, MakeRequest(t, req, http.StatusOK), &api.ActionWorkflowJob{})
assert.Equal(t, actionTask.RunnerID, apiJob.RunnerID)
assert.Equal(t, "mock-runner", apiJob.RunnerName)
resp, err = runner.client.runnerServiceClient.FetchTask(t.Context(), &runnerv1.FetchTaskRequest{
TasksVersion: 0,
})