mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-27 18:34:25 +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>
184 lines
6.1 KiB
TypeScript
184 lines
6.1 KiB
TypeScript
import {createElementFromAttrs} from '../utils/dom.ts';
|
|
import {renderAnsi} from '../render/ansi.ts';
|
|
import {reactive} from 'vue';
|
|
import type {ActionsArtifact, ActionsJob, ActionsRun, ActionsRunStatus} from '../modules/gitea-actions.ts';
|
|
import type {IntervalId} from '../types.ts';
|
|
import {POST} from '../modules/fetch.ts';
|
|
|
|
// How GitHub Actions logs work:
|
|
// * Workflow command outputs log commands like "::group::the-title", "::add-matcher::...."
|
|
// * Workflow runner parses and processes the commands to "##[group]", apply "matchers", hide secrets, etc.
|
|
// * The reported logs are the processed logs.
|
|
// HOWEVER: Gitea runner does not completely process those commands. Many works are done by the frontend at the moment.
|
|
const LogLinePrefixCommandMap: Record<string, LogLineCommandName> = {
|
|
'::group::': 'group',
|
|
'##[group]': 'group',
|
|
'::endgroup::': 'endgroup',
|
|
'##[endgroup]': 'endgroup',
|
|
|
|
'##[error]': 'error',
|
|
'##[warning]': 'warning',
|
|
'##[notice]': 'notice',
|
|
'##[debug]': 'debug',
|
|
'[command]': 'command',
|
|
|
|
// https://github.com/actions/toolkit/blob/master/docs/commands.md
|
|
// https://github.com/actions/runner/blob/main/docs/adrs/0276-problem-matchers.md#registration
|
|
'::add-matcher::': 'hidden',
|
|
'##[add-matcher]': 'hidden',
|
|
'::remove-matcher': 'hidden', // it has arguments
|
|
};
|
|
|
|
// Pattern for ::cmd:: and ::cmd args:: format (args are stripped for display)
|
|
const LogLineCmdPattern = /^::(error|warning|notice|debug)(?:\s[^:]*)?::/;
|
|
|
|
export type LogLine = {
|
|
index: number;
|
|
timestamp: number;
|
|
message: string;
|
|
};
|
|
|
|
export type LogLineCommandName = 'group' | 'endgroup' | 'command' | 'error' | 'warning' | 'notice' | 'debug' | 'hidden';
|
|
export type LogLineCommand = {
|
|
name: LogLineCommandName,
|
|
prefix: string,
|
|
};
|
|
|
|
export function parseLogLineCommand(line: LogLine): LogLineCommand | null {
|
|
// TODO: in the future it can be refactored to be a general parser that can parse arguments, drop the "prefix match"
|
|
for (const prefix of Object.keys(LogLinePrefixCommandMap)) {
|
|
if (line.message.startsWith(prefix)) {
|
|
return {name: LogLinePrefixCommandMap[prefix], prefix};
|
|
}
|
|
}
|
|
// Handle ::cmd:: and ::cmd args:: format (runner may pass these through raw)
|
|
const match = LogLineCmdPattern.exec(line.message);
|
|
if (match) {
|
|
return {name: match[1] as LogLineCommandName, prefix: match[0]};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
const LogLineLabelMap: Partial<Record<LogLineCommandName, string>> = {
|
|
'error': 'Error',
|
|
'warning': 'Warning',
|
|
'notice': 'Notice',
|
|
'debug': 'Debug',
|
|
};
|
|
|
|
export function createLogLineMessage(line: LogLine, cmd: LogLineCommand | null) {
|
|
const logMsgAttrs = {class: 'log-msg'};
|
|
if (cmd?.name) logMsgAttrs.class += ` log-cmd-${cmd.name}`; // make it easier to add styles to some commands like "error"
|
|
|
|
// TODO: for some commands (::group::), the "prefix removal" works well, for some commands with "arguments" (::remove-matcher ...::),
|
|
// it needs to do further processing in the future (fortunately, at the moment we don't need to handle these commands)
|
|
const msgContent = cmd ? line.message.substring(cmd.prefix.length) : line.message;
|
|
|
|
const logMsg = createElementFromAttrs('span', logMsgAttrs);
|
|
const label = cmd ? LogLineLabelMap[cmd.name] : null;
|
|
if (label) {
|
|
logMsg.append(createElementFromAttrs('span', {class: 'log-msg-label'}, `${label}:`));
|
|
const msgSpan = document.createElement('span');
|
|
msgSpan.innerHTML = ` ${renderAnsi(msgContent.trimStart())}`;
|
|
logMsg.append(msgSpan);
|
|
} else {
|
|
logMsg.innerHTML = renderAnsi(msgContent);
|
|
}
|
|
return logMsg;
|
|
}
|
|
|
|
export function createEmptyActionsRun(): ActionsRun {
|
|
return {
|
|
repoId: 0,
|
|
link: '',
|
|
viewLink: '',
|
|
title: '',
|
|
titleHTML: '',
|
|
status: '' as ActionsRunStatus, // do not show the status before initialized, otherwise it would show an incorrect "error" icon
|
|
canCancel: false,
|
|
canApprove: false,
|
|
canRerun: false,
|
|
canRerunFailed: false,
|
|
canDeleteArtifact: false,
|
|
done: false,
|
|
workflowID: '',
|
|
workflowLink: '',
|
|
isSchedule: false,
|
|
runAttempt: 0,
|
|
attempts: [],
|
|
duration: '',
|
|
triggeredAt: 0,
|
|
triggerEvent: '',
|
|
jobs: [] as Array<ActionsJob>,
|
|
commit: {
|
|
localeCommit: '',
|
|
localePushedBy: '',
|
|
shortSHA: '',
|
|
link: '',
|
|
pusher: {
|
|
displayName: '',
|
|
link: '',
|
|
},
|
|
branch: {
|
|
name: '',
|
|
link: '',
|
|
isDeleted: false,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
export function createActionRunViewStore(viewUrl: string) {
|
|
let loadingAbortController: AbortController | null = null;
|
|
let intervalID: IntervalId | null = null;
|
|
const viewData = reactive({
|
|
currentRun: createEmptyActionsRun(),
|
|
runArtifacts: [] as Array<ActionsArtifact>,
|
|
});
|
|
const loadCurrentRun = async () => {
|
|
if (loadingAbortController) return;
|
|
const abortController = new AbortController();
|
|
loadingAbortController = abortController;
|
|
try {
|
|
const resp = await POST(viewUrl, {signal: abortController.signal, data: {}});
|
|
const runResp = await resp.json();
|
|
if (loadingAbortController !== abortController) return;
|
|
|
|
viewData.runArtifacts = runResp.artifacts || [];
|
|
viewData.currentRun = runResp.state.run;
|
|
// clear the interval timer if the job is done
|
|
if (viewData.currentRun.done && intervalID) {
|
|
clearInterval(intervalID);
|
|
intervalID = null;
|
|
}
|
|
} catch (e) {
|
|
// avoid network error while unloading page, and ignore "abort" error
|
|
if (e instanceof TypeError || abortController.signal.aborted) return;
|
|
throw e;
|
|
} finally {
|
|
if (loadingAbortController === abortController) loadingAbortController = null;
|
|
}
|
|
};
|
|
|
|
return reactive({
|
|
viewData,
|
|
|
|
async startPollingCurrentRun() {
|
|
await loadCurrentRun();
|
|
intervalID = setInterval(() => loadCurrentRun(), 1000);
|
|
},
|
|
async forceReloadCurrentRun() {
|
|
loadingAbortController?.abort();
|
|
loadingAbortController = null;
|
|
await loadCurrentRun();
|
|
},
|
|
stopPollingCurrentRun() {
|
|
if (!intervalID) return;
|
|
clearInterval(intervalID);
|
|
intervalID = null;
|
|
},
|
|
});
|
|
}
|
|
|
|
export type ActionRunViewStore = ReturnType<typeof createActionRunViewStore>;
|