mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-26 23:50:46 +00:00
This PR introduces a new `ActionRunAttempt` model and makes Actions
execution attempt-scoped.
**Main Changes**
- Each workflow run trigger generates a new `ActionRunAttempt`. The
triggered jobs are then associated with this new `ActionRunAttempt`
record.
- Each rerun now creates:
- a new `ActionRunAttempt` record for the workflow run
- a full new set of `ActionRunJob` records for the new
`ActionRunAttempt`
- For jobs that need to be rerun, the new job records are created as
runnable jobs in the new attempt.
- For jobs that do not need to be rerun, new job records are still
created in the new attempt, but they reuse the result of the previous
attempt instead of executing again.
- Introduce `rerunPlan` to manage each rerun and refactored rerun flow
into a two-phase plan-based model:
- `buildRerunPlan`
- `execRerunPlan`
- `RerunFailedWorkflowRun` and `RerunFailed` no longer directly derives
all jobs that need to be rerun; this step is now handled by
`buildRerunPlan`.
- Converted artifacts from run-scoped to attempt-scoped:
- uploads are now associated with `RunAttemptID`
- listing, download, and deletion resolve against the current attempt
- Added attempt-aware web Actions views:
- the default run page shows the latest attempt
(`/actions/runs/{run_id}`)
- previous attempt pages show jobs and artifacts for that attempt
(`/actions/runs/{run_id}/attempts/{attempt_num}`)
- New APIs:
- `/repos/{owner}/{repo}/actions/runs/{run}/attempts/{attempt}`
- `/repos/{owner}/{repo}/actions/runs/{run}/attempts/{attempt}/jobs`
- New configuration `MAX_RERUN_ATTEMPTS`
- https://gitea.com/gitea/docs/pulls/383
**Compatibility**
- Existing legacy runs use `LatestAttemptID = 0` and legacy jobs use
`RunAttemptID = 0`. Therefore, these fields can be used to identify
legacy runs and jobs and provide backward compatibility.
- If a legacy run is rerun, an `ActionRunAttempt` with `attempt=1` will
be created to represent the original execution. Then a new
`ActionRunAttempt` with `attempt=2` will be created for the real rerun.
- Existing artifact records are not backfilled; legacy artifacts
continue to use `RunAttemptID = 0`.
**Improvements**
- It is now easier to inspect and download logs from previous attempts.
-
[`run_attempt`](https://docs.github.com/en/actions/reference/workflows-and-actions/contexts#github-context)
semantics are now aligned with GitHub.
- > A unique number for each attempt of a particular workflow run in a
repository. This number begins at 1 for the workflow run's first
attempt, and increments with each re-run.
- Rerun behavior is now clearer and more explicit.
- Instead of mutating the status of previous jobs in place, each rerun
creates a new attempt with a full new set of job records.
- Artifacts produced by different reruns can now be listed separately.
Signed-off-by: Zettat123 <zettat123@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Giteabot <teabot@gitea.io>
145 lines
4.9 KiB
Go
145 lines
4.9 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"context"
|
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
|
"code.gitea.io/gitea/modules/log"
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
|
)
|
|
|
|
// NotifyWorkflowJobsAndRunsStatusUpdate notifies status changes for a batch of jobs and the runs they affect.
|
|
// Use it when a workflow operation updates multiple jobs and runs.
|
|
func NotifyWorkflowJobsAndRunsStatusUpdate(ctx context.Context, jobs []*actions_model.ActionRunJob) {
|
|
if len(jobs) == 0 {
|
|
return
|
|
}
|
|
|
|
// The input jobs may belong to different runs, so track each affected run.
|
|
runs := make(map[int64]*actions_model.ActionRun, len(jobs))
|
|
jobsByRunID := make(map[int64][]*actions_model.ActionRunJob)
|
|
|
|
for _, job := range jobs {
|
|
if err := job.LoadAttributes(ctx); err != nil {
|
|
log.Error("Failed to load job attributes: %v", err)
|
|
continue
|
|
}
|
|
CreateCommitStatusForRunJobs(ctx, job.Run, job)
|
|
|
|
if _, ok := runs[job.RunID]; !ok {
|
|
runs[job.RunID] = job.Run
|
|
}
|
|
if _, ok := jobsByRunID[job.RunID]; !ok {
|
|
jobsByRunID[job.RunID] = make([]*actions_model.ActionRunJob, 0)
|
|
}
|
|
jobsByRunID[job.RunID] = append(jobsByRunID[job.RunID], job)
|
|
}
|
|
|
|
for _, run := range runs {
|
|
NotifyWorkflowRunStatusUpdate(ctx, run)
|
|
}
|
|
|
|
for _, jobs := range jobsByRunID {
|
|
NotifyWorkflowJobsStatusUpdate(ctx, jobs...)
|
|
}
|
|
}
|
|
|
|
// NotifyWorkflowRunStatusUpdateWithReload reloads the run before notifying its status update.
|
|
// Use it when only repo/run IDs are available or when the in-memory run may be stale after job updates.
|
|
func NotifyWorkflowRunStatusUpdateWithReload(ctx context.Context, repoID, runID int64) {
|
|
run, err := actions_model.GetRunByRepoAndID(ctx, repoID, runID)
|
|
if err != nil {
|
|
log.Error("GetRunByRepoAndID: %v", err)
|
|
return
|
|
}
|
|
NotifyWorkflowRunStatusUpdate(ctx, run)
|
|
}
|
|
|
|
// NotifyWorkflowRunStatusUpdate notifies a run status update using the latest attempt trigger user when available.
|
|
// Use it for run-level notifications when the caller already has the run model loaded.
|
|
func NotifyWorkflowRunStatusUpdate(ctx context.Context, run *actions_model.ActionRun) {
|
|
if err := run.LoadAttributes(ctx); err != nil {
|
|
log.Error("run.LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
triggerUser := run.TriggerUser
|
|
if run.LatestAttemptID > 0 {
|
|
attempt, err := actions_model.GetRunAttemptByRepoAndID(ctx, run.RepoID, run.LatestAttemptID)
|
|
if err != nil {
|
|
log.Error("GetRunAttemptByRepoAndID: %v", err)
|
|
return
|
|
}
|
|
if err := attempt.LoadAttributes(ctx); err != nil {
|
|
log.Error("attempt.LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
triggerUser = attempt.TriggerUser
|
|
}
|
|
notify_service.WorkflowRunStatusUpdate(ctx, run.Repo, triggerUser, run)
|
|
}
|
|
|
|
// NotifyWorkflowJobsStatusUpdate notifies status updates for jobs without task.
|
|
// Use it for batch or single-job notifications after state changes.
|
|
func NotifyWorkflowJobsStatusUpdate(ctx context.Context, jobs ...*actions_model.ActionRunJob) {
|
|
jobsByAttempt := make(map[int64][]*actions_model.ActionRunJob)
|
|
for _, job := range jobs {
|
|
if _, ok := jobsByAttempt[job.RunAttemptID]; !ok {
|
|
jobsByAttempt[job.RunAttemptID] = make([]*actions_model.ActionRunJob, 0)
|
|
}
|
|
jobsByAttempt[job.RunAttemptID] = append(jobsByAttempt[job.RunAttemptID], job)
|
|
}
|
|
|
|
for attemptID, js := range jobsByAttempt {
|
|
if attemptID == 0 {
|
|
for _, job := range js {
|
|
if err := job.LoadAttributes(ctx); err != nil {
|
|
log.Error("job.LoadAttributes: %v", err)
|
|
continue
|
|
}
|
|
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
|
|
}
|
|
continue
|
|
}
|
|
|
|
attempt, err := actions_model.GetRunAttemptByRepoAndID(ctx, js[0].RepoID, attemptID)
|
|
if err != nil {
|
|
log.Error("GetRunAttemptByRepoAndID: %v", err)
|
|
continue
|
|
}
|
|
if err := attempt.LoadAttributes(ctx); err != nil {
|
|
log.Error("attempt.LoadAttributes: %v", err)
|
|
continue
|
|
}
|
|
for _, job := range js {
|
|
notify_service.WorkflowJobStatusUpdate(ctx, attempt.Run.Repo, attempt.TriggerUser, job, nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
// NotifyWorkflowJobStatusUpdateWithTask notifies a single job status update when a concrete task is available.
|
|
// Use it for runner/task lifecycle callbacks so the notification includes the originating task context.
|
|
func NotifyWorkflowJobStatusUpdateWithTask(ctx context.Context, job *actions_model.ActionRunJob, task *actions_model.ActionTask) {
|
|
if job.RunAttemptID == 0 {
|
|
if err := job.LoadAttributes(ctx); err != nil {
|
|
log.Error("job.LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, task)
|
|
return
|
|
}
|
|
|
|
attempt, err := actions_model.GetRunAttemptByRepoAndID(ctx, job.RepoID, job.RunAttemptID)
|
|
if err != nil {
|
|
log.Error("GetRunAttemptByRepoAndID: %v", err)
|
|
return
|
|
}
|
|
if err := attempt.LoadAttributes(ctx); err != nil {
|
|
log.Error("attempt.LoadAttributes: %v", err)
|
|
return
|
|
}
|
|
notify_service.WorkflowJobStatusUpdate(ctx, attempt.Run.Repo, attempt.TriggerUser, job, task)
|
|
}
|