mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-08 14:34:49 +09: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>
146 lines
4.2 KiB
Go
146 lines
4.2 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
// ActionRunAttempt represents a single execution attempt of an ActionRun.
|
|
type ActionRunAttempt struct {
|
|
ID int64
|
|
RepoID int64 `xorm:"index(repo_concurrency_status)"`
|
|
RunID int64 `xorm:"UNIQUE(run_attempt)"`
|
|
Run *ActionRun `xorm:"-"`
|
|
Attempt int64 `xorm:"UNIQUE(run_attempt)"`
|
|
|
|
TriggerUserID int64
|
|
TriggerUser *user_model.User `xorm:"-"`
|
|
|
|
ConcurrencyGroup string `xorm:"index(repo_concurrency_status) NOT NULL DEFAULT ''"`
|
|
ConcurrencyCancel bool `xorm:"NOT NULL DEFAULT FALSE"`
|
|
|
|
Status Status `xorm:"index(repo_concurrency_status)"`
|
|
Started timeutil.TimeStamp
|
|
Stopped timeutil.TimeStamp
|
|
|
|
Created timeutil.TimeStamp `xorm:"created"`
|
|
Updated timeutil.TimeStamp `xorm:"updated"`
|
|
}
|
|
|
|
func (*ActionRunAttempt) TableName() string {
|
|
return "action_run_attempt"
|
|
}
|
|
|
|
func init() {
|
|
db.RegisterModel(new(ActionRunAttempt))
|
|
}
|
|
|
|
func (attempt *ActionRunAttempt) Duration() time.Duration {
|
|
return calculateDuration(attempt.Started, attempt.Stopped, attempt.Status, attempt.Updated)
|
|
}
|
|
|
|
func (attempt *ActionRunAttempt) LoadAttributes(ctx context.Context) error {
|
|
if attempt == nil {
|
|
return nil
|
|
}
|
|
|
|
if attempt.Run == nil {
|
|
run, err := GetRunByRepoAndID(ctx, attempt.RepoID, attempt.RunID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := run.LoadAttributes(ctx); err != nil {
|
|
return err
|
|
}
|
|
attempt.Run = run
|
|
}
|
|
|
|
if attempt.TriggerUser == nil {
|
|
u, err := user_model.GetPossibleUserByID(ctx, attempt.TriggerUserID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
attempt.TriggerUser = u
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetRunAttemptByRepoAndID(ctx context.Context, repoID, attemptID int64) (*ActionRunAttempt, error) {
|
|
var attempt ActionRunAttempt
|
|
has, err := db.GetEngine(ctx).Where("repo_id=? AND id=?", repoID, attemptID).Get(&attempt)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, fmt.Errorf("run attempt %d in repo %d: %w", attemptID, repoID, util.ErrNotExist)
|
|
}
|
|
return &attempt, nil
|
|
}
|
|
|
|
func GetRunAttemptByRunIDAndAttemptNum(ctx context.Context, runID, attemptNum int64) (*ActionRunAttempt, error) {
|
|
var attempt ActionRunAttempt
|
|
has, err := db.GetEngine(ctx).Where("run_id=? AND attempt=?", runID, attemptNum).Get(&attempt)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, fmt.Errorf("run attempt %d for run %d: %w", attemptNum, runID, util.ErrNotExist)
|
|
}
|
|
return &attempt, nil
|
|
}
|
|
|
|
// FindConcurrentRunAttempts returns attempts in the given concurrency group and status set.
|
|
// Results are unordered; callers must not depend on any particular row order.
|
|
func FindConcurrentRunAttempts(ctx context.Context, repoID int64, concurrencyGroup string, statuses []Status) ([]*ActionRunAttempt, error) {
|
|
attempts := make([]*ActionRunAttempt, 0)
|
|
sess := db.GetEngine(ctx).Where("repo_id=? AND concurrency_group=?", repoID, concurrencyGroup)
|
|
if len(statuses) > 0 {
|
|
sess = sess.In("status", statuses)
|
|
}
|
|
return attempts, sess.Find(&attempts)
|
|
}
|
|
|
|
func UpdateRunAttempt(ctx context.Context, attempt *ActionRunAttempt, cols ...string) error {
|
|
if slices.Contains(cols, "status") && attempt.Started.IsZero() && attempt.Status.IsRunning() {
|
|
attempt.Started = timeutil.TimeStampNow()
|
|
cols = append(cols, "started")
|
|
}
|
|
|
|
sess := db.GetEngine(ctx).ID(attempt.ID)
|
|
if len(cols) > 0 {
|
|
sess.Cols(cols...)
|
|
}
|
|
if _, err := sess.Update(attempt); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Only status/timing changes on an attempt need to update the latest run.
|
|
if len(cols) > 0 && !slices.Contains(cols, "status") && !slices.Contains(cols, "started") && !slices.Contains(cols, "stopped") {
|
|
return nil
|
|
}
|
|
|
|
run, err := GetRunByRepoAndID(ctx, attempt.RepoID, attempt.RunID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if run.LatestAttemptID != attempt.ID {
|
|
log.Warn("run %d cannot be updated by an old attempt %d", run.LatestAttemptID, attempt.ID)
|
|
return nil
|
|
}
|
|
|
|
run.Status = attempt.Status
|
|
run.Started = attempt.Started
|
|
run.Stopped = attempt.Stopped
|
|
return UpdateRun(ctx, run, "status", "started", "stopped")
|
|
}
|