mirror of
https://github.com/go-gitea/gitea.git
synced 2026-06-14 03:29:55 +00:00
0359746abe
## Summary This PR improves reusable workflow support for Gitea Actions. The parsing of the called workflow now happens on Gitea side, not on the runner. When the caller becomes ready, Gitea fetches the called workflow source, parses it, and inserts each child job into the database as a `ActionRunJob` linked to the caller via `ParentCallJobID`. As a result, every callee job is dispatched as its own task and its logs surface as an independent job entry in the UI, rather than being inlined into the caller's "Set up job" step. This PR supports two kinds of `uses` : - same-repo call: `uses: ./.gitea/workflows/foo.yaml` - cross-repo call: `uses: OWNER/REPO/.gitea/workflows/foo.yaml@REF` ## **⚠️ BREAKING ⚠️** External reusable workflows (`uses: https://other-gitea-instance/OWNER/REPO/.gitea/workflows/test.yaml@REF`) are no longer supported. To keep using them, clone the repositories to the local instance. ## Main changes ### Execution model - Each caller job carries `IsReusableCaller=true` and won't be fetched by runners. - `ParentCallJobID` can link a called job to its caller. - Caller status is derived from its direct children. ### Workflow syntax - `jobparser` now supports parsing `on: workflow_call` trigger with `inputs:`, `outputs:`, and `secrets:` declarations. - **Max nesting depth**: capped at `MaxReusableCallLevels = 9`, which means a top-level caller may have at most 9 nested callers below it. - **Cycle prevention**: at expansion time, `checkCallerChain` walks the caller's ancestor chain via `ParentCallJobID` and rejects if the same `uses:` string appears anywhere upstream (`reusable workflow call cycle detected`). This catches both direct (`A -> A`) and indirect (`A -> B -> A`) cycles. ### Cross-repo access - To share reusable workflows from private repos, use `Collaborative Owners` introduced by #32562 ### Rerun semantics - `expandRerunJobIDs` partitions the latest attempt's jobs into: - a **rerun set**: jobs being rerun + downstream siblings within the same scope. - an **ancestor set**: reusable callers whose only *some* descendants are being rerun (the caller itself is not). - Cloning behavior for callers in `execRerunPlan`: - **Caller is fully rerun** (caller's `AttemptJobID` in `rerunSet`): none of its descendants are cloned. The caller is cloned with `IsCallerExpanded=false`, and re-expansion (which reinserts the children fresh) happens later when the resolver brings the caller to `Waiting` again. - **Caller is in ancestor set** (only some descendants rerun): the caller is pass-through (`Status` will be updated by its fresh children). Its non-rerun descendants are also pass-through clones (point `SourceTaskID` at the original task). Their `ParentCallJobID` is remapped to the new attempt's caller row. ### UI - Job list in `RepoActionView.vue` is now tree-shaped: callers indent their children. Callers default to collapsed. - New caller detail page using `WorkflowGraph` to show direct children only; the run summary's `WorkflowGraph` shows top-level callers and their immediate descendants. ### Known trade-offs - **Caller expansion runs inside the enclosing write transaction.** `expandReusableWorkflowCaller` performs a git read of the called workflow while holding the row locks that update the caller and insert its children. This is intentional: the caller-row update and child-row inserts must commit atomically. None of the call sites is hot (each caller is expanded once per attempt), so the trade-off is acceptable. - **A malformed `if:` expression on a job leaves it `Blocked` silently.** `evaluateJobIf` now runs server-side as part of resolver passes; deterministic expression errors (typos, undefined context fields) are logged but do not surface in the UI. This is the same behavior the resolver already had for concurrency-expression errors. Distinguishing transient DB errors from user-authored expression errors and writing the latter back as `StatusFailure` is a follow-up. #### Screenshots <img width="1600" alt="image" src="https://github.com/user-attachments/assets/bfaa9b7a-07e9-4127-8de9-a81f86e82828" /> <img width="1600" alt="image" src="https://github.com/user-attachments/assets/8af109b3-ef28-4b53-aaad-d4632b923224" /> ## References - https://docs.github.com/en/actions/how-tos/reuse-automations/reuse-workflows - https://docs.github.com/en/actions/reference/workflows-and-actions/reusing-workflow-configurations --- Replace #36388 --------- Signed-off-by: Zettat123 <zettat123@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com>
134 lines
4.5 KiB
Go
134 lines
4.5 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gitea.dev/models/db"
|
|
"gitea.dev/models/unittest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetPriorAttemptChildrenByParent(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
ctx := t.Context()
|
|
|
|
// 3 attempts of one run:
|
|
// 1: caller expanded with 3 matrix instances of "work" + non-matrix sibling "summary".
|
|
// 2: caller skipped, no children rows.
|
|
// 3: placeholder "current" attempt for the walkback subtest.
|
|
|
|
run := &ActionRun{
|
|
Title: "prior-children-test",
|
|
RepoID: 4,
|
|
Index: 9501,
|
|
OwnerID: 1,
|
|
WorkflowID: "matrix.yaml",
|
|
TriggerUserID: 1,
|
|
Ref: "refs/heads/master",
|
|
CommitSHA: "c2d72f548424103f01ee1dc02889c1e2bff816b0",
|
|
Event: "push",
|
|
TriggerEvent: "push",
|
|
EventPayload: "{}",
|
|
Status: StatusSuccess,
|
|
}
|
|
require.NoError(t, db.Insert(ctx, run))
|
|
|
|
const callerAttemptJobID int64 = 9001
|
|
insertAttempt := func(t *testing.T, num int64, status Status) *ActionRunAttempt {
|
|
t.Helper()
|
|
a := &ActionRunAttempt{
|
|
RepoID: run.RepoID,
|
|
RunID: run.ID,
|
|
Attempt: num,
|
|
TriggerUserID: 1,
|
|
Status: status,
|
|
}
|
|
require.NoError(t, db.Insert(ctx, a))
|
|
return a
|
|
}
|
|
insertCaller := func(t *testing.T, attemptID int64, status Status, expanded bool) *ActionRunJob {
|
|
t.Helper()
|
|
caller := &ActionRunJob{
|
|
RunID: run.ID,
|
|
RunAttemptID: attemptID,
|
|
RepoID: run.RepoID,
|
|
OwnerID: run.OwnerID,
|
|
CommitSHA: run.CommitSHA,
|
|
Name: "caller",
|
|
JobID: "caller",
|
|
Attempt: 1,
|
|
Status: status,
|
|
AttemptJobID: callerAttemptJobID,
|
|
IsReusableCaller: true,
|
|
IsExpanded: expanded,
|
|
}
|
|
require.NoError(t, db.Insert(ctx, caller))
|
|
return caller
|
|
}
|
|
insertChild := func(t *testing.T, attemptID, parentID, attemptJobID int64, name, jobID string) {
|
|
t.Helper()
|
|
require.NoError(t, db.Insert(ctx, &ActionRunJob{
|
|
RunID: run.ID,
|
|
RunAttemptID: attemptID,
|
|
RepoID: run.RepoID,
|
|
OwnerID: run.OwnerID,
|
|
CommitSHA: run.CommitSHA,
|
|
Name: name,
|
|
JobID: jobID,
|
|
Attempt: 1,
|
|
Status: StatusSuccess,
|
|
AttemptJobID: attemptJobID,
|
|
ParentJobID: parentID,
|
|
}))
|
|
}
|
|
|
|
attempt1 := insertAttempt(t, 1, StatusSuccess)
|
|
caller1 := insertCaller(t, attempt1.ID, StatusSuccess, true)
|
|
insertChild(t, attempt1.ID, caller1.ID, 101, "work (alpha)", "work")
|
|
insertChild(t, attempt1.ID, caller1.ID, 102, "work (beta)", "work")
|
|
insertChild(t, attempt1.ID, caller1.ID, 103, "work (gamma)", "work")
|
|
insertChild(t, attempt1.ID, caller1.ID, 104, "summary", "summary")
|
|
|
|
attempt2 := insertAttempt(t, 2, StatusSkipped)
|
|
insertCaller(t, attempt2.ID, StatusSkipped, false) // no children intentionally
|
|
|
|
// both subtests expect attempt 1's expansion, differing only in the "current" attempt id
|
|
assertAttempt1Children := func(t *testing.T, out map[string]map[string]*ActionRunJob) {
|
|
t.Helper()
|
|
// outer map keyed by JobID: "work" has 3 matrix instances, "summary" 1
|
|
assert.Len(t, out, 2)
|
|
assert.Len(t, out["work"], 3, "matrix instances must each get their own inner-map entry")
|
|
assert.Len(t, out["summary"], 1)
|
|
|
|
require.NotNil(t, out["work"]["work (alpha)"])
|
|
require.NotNil(t, out["work"]["work (beta)"])
|
|
require.NotNil(t, out["work"]["work (gamma)"])
|
|
require.NotNil(t, out["summary"]["summary"])
|
|
|
|
assert.Equal(t, int64(101), out["work"]["work (alpha)"].AttemptJobID)
|
|
assert.Equal(t, int64(102), out["work"]["work (beta)"].AttemptJobID)
|
|
assert.Equal(t, int64(103), out["work"]["work (gamma)"].AttemptJobID)
|
|
assert.Equal(t, int64(104), out["summary"]["summary"].AttemptJobID)
|
|
}
|
|
|
|
t.Run("matrix instances and non-matrix sibling are indexed by (JobID, Name)", func(t *testing.T) {
|
|
// "current" = attempt 2; prior = attempt 1, which is the immediately preceding attempt.
|
|
out, err := GetPriorAttemptChildrenByParent(ctx, run.ID, attempt2.ID, callerAttemptJobID)
|
|
require.NoError(t, err)
|
|
assertAttempt1Children(t, out)
|
|
})
|
|
|
|
t.Run("walkback past an attempt where the caller had no children", func(t *testing.T) {
|
|
attempt3 := insertAttempt(t, 3, StatusRunning)
|
|
// "current" = attempt 3; the immediately preceding attempt 2 has no children, so the lookup must walk further back to attempt 1.
|
|
out, err := GetPriorAttemptChildrenByParent(ctx, run.ID, attempt3.ID, callerAttemptJobID)
|
|
require.NoError(t, err)
|
|
assertAttempt1Children(t, out)
|
|
})
|
|
}
|