mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-27 09:24:53 +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>
127 lines
5.0 KiB
Go
127 lines
5.0 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package convert
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/unit"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/git/gitcmd"
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// buildWorkflowTestRepo creates a temporary git repository for testing GetActionWorkflow.
|
|
// The default branch "main" has no workflow files; "feature" and "release-v1" each add their own workflow file.
|
|
func buildWorkflowTestRepo(t *testing.T) string {
|
|
t.Helper()
|
|
ctx := t.Context()
|
|
tmpDir := t.TempDir()
|
|
|
|
_, _, err := gitcmd.NewCommand("init").WithDir(tmpDir).RunStdString(ctx)
|
|
require.NoError(t, err)
|
|
|
|
readme := "readme"
|
|
featureWF := "on: [push]\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo test\n"
|
|
releaseWF := "on: [push]\njobs:\n release:\n runs-on: ubuntu-latest\n steps:\n - run: echo release\n"
|
|
|
|
// Build a git fast-import stream:
|
|
// :4 = initial commit on main (README.md only)
|
|
// :5 = feature branch commit (adds feature workflow)
|
|
// :6 = release commit from :4 (adds release workflow, tagged release-v1, not on main)
|
|
var sb strings.Builder
|
|
fmt.Fprintf(&sb, "blob\nmark :1\ndata %d\n%s\n", len(readme), readme)
|
|
fmt.Fprintf(&sb, "blob\nmark :2\ndata %d\n%s\n", len(featureWF), featureWF)
|
|
fmt.Fprintf(&sb, "blob\nmark :3\ndata %d\n%s\n", len(releaseWF), releaseWF)
|
|
fmt.Fprintf(&sb, "commit refs/heads/main\nmark :4\nauthor Test <test@gitea.com> 1000000000 +0000\ncommitter Test <test@gitea.com> 1000000000 +0000\ndata 14\ninitial commit\nM 100644 :1 README.md\n\n")
|
|
fmt.Fprintf(&sb, "commit refs/heads/feature\nmark :5\nauthor Test <test@gitea.com> 1000000001 +0000\ncommitter Test <test@gitea.com> 1000000001 +0000\ndata 12\nadd workflow\nfrom :4\nM 100644 :2 .gitea/workflows/my-workflow.yml\n\n")
|
|
fmt.Fprintf(&sb, "reset refs/pull/42/merge\nfrom :5\n\n")
|
|
fmt.Fprintf(&sb, "commit refs/heads/main\nmark :6\nauthor Test <test@gitea.com> 1000000002 +0000\ncommitter Test <test@gitea.com> 1000000002 +0000\ndata 16\nrelease workflow\nfrom :4\nM 100644 :3 .gitea/workflows/my-workflow.yml\n\n")
|
|
fmt.Fprintf(&sb, "reset refs/tags/release-v1\nfrom :6\n\n")
|
|
fmt.Fprintf(&sb, "reset refs/heads/main\nfrom :4\n\n")
|
|
fmt.Fprintf(&sb, "done\n")
|
|
|
|
_, _, err = gitcmd.NewCommand("fast-import").WithDir(tmpDir).WithStdinBytes([]byte(sb.String())).RunStdString(ctx)
|
|
require.NoError(t, err)
|
|
|
|
return tmpDir
|
|
}
|
|
|
|
func TestGetActionWorkflow_FallbackRef(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
repoDir := buildWorkflowTestRepo(t)
|
|
|
|
gitRepo, err := git.OpenRepository(ctx, repoDir)
|
|
require.NoError(t, err)
|
|
defer gitRepo.Close()
|
|
|
|
repo := &repo_model.Repository{
|
|
DefaultBranch: "main",
|
|
OwnerName: "test-owner",
|
|
Name: "test-repo",
|
|
Units: []*repo_model.RepoUnit{
|
|
{
|
|
Type: unit.TypeActions,
|
|
Config: &repo_model.ActionsConfig{},
|
|
},
|
|
},
|
|
}
|
|
|
|
t.Run("returns error when workflow only on non-default branch", func(t *testing.T) {
|
|
_, err := GetActionWorkflow(ctx, gitRepo, repo, "my-workflow.yml")
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, util.ErrNotExist)
|
|
})
|
|
|
|
t.Run("returns workflow when found via ref", func(t *testing.T) {
|
|
wf, err := GetActionWorkflowByRef(ctx, gitRepo, repo, "my-workflow.yml", git.RefName("refs/heads/feature"))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "my-workflow.yml", wf.ID)
|
|
})
|
|
|
|
t.Run("returns workflow when found via pull ref", func(t *testing.T) {
|
|
wf, err := GetActionWorkflowByRef(ctx, gitRepo, repo, "my-workflow.yml", git.RefName("refs/pull/42/merge"))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "my-workflow.yml", wf.ID)
|
|
assert.Contains(t, wf.HTMLURL, "/src/commit/")
|
|
})
|
|
|
|
t.Run("returns workflow with tag link when found via tag ref", func(t *testing.T) {
|
|
wf, err := GetActionWorkflowByRef(ctx, gitRepo, repo, "my-workflow.yml", git.RefName("refs/tags/release-v1"))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "my-workflow.yml", wf.ID)
|
|
assert.Contains(t, wf.HTMLURL, "/src/tag/release-v1/")
|
|
})
|
|
|
|
t.Run("returns error when workflow missing from ref", func(t *testing.T) {
|
|
_, err := GetActionWorkflowByRef(ctx, gitRepo, repo, "nonexistent.yml", git.RefName("refs/heads/feature"))
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, util.ErrNotExist)
|
|
})
|
|
}
|
|
|
|
func TestToActionWorkflowRun_UsesTriggerEvent(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
|
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: 803})
|
|
|
|
// Scheduled runs keep Event as the registration event (push) and use TriggerEvent as the real trigger.
|
|
run.Event = "push"
|
|
run.TriggerEvent = "schedule"
|
|
|
|
apiRun, err := ToActionWorkflowRun(t.Context(), repo, run, nil)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "schedule", apiRun.Event)
|
|
}
|