2025-02-12 00:09:43 +08:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
package actions
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-05-29 17:34:29 +02:00
|
|
|
"errors"
|
2025-02-12 00:09:43 +08:00
|
|
|
"fmt"
|
|
|
|
|
|
2025-03-27 19:40:14 +00:00
|
|
|
actions_model "forgejo.org/models/actions"
|
|
|
|
|
"forgejo.org/models/db"
|
2026-05-03 15:46:58 +02:00
|
|
|
repo_model "forgejo.org/models/repo"
|
|
|
|
|
"forgejo.org/models/unit"
|
2026-01-15 03:39:00 +01:00
|
|
|
actions_module "forgejo.org/modules/actions"
|
|
|
|
|
"forgejo.org/modules/setting"
|
2025-04-08 14:32:16 +02:00
|
|
|
"forgejo.org/modules/timeutil"
|
|
|
|
|
"forgejo.org/modules/util"
|
2025-02-12 00:09:43 +08:00
|
|
|
|
2025-11-05 16:10:52 +01:00
|
|
|
runnerv1 "code.forgejo.org/forgejo/actions-proto/runner/v1"
|
2025-02-12 00:09:43 +08:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2025-04-08 17:41:13 +02:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2025-02-12 00:09:43 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-25 17:27:05 +01:00
|
|
|
func PickTask(ctx context.Context, runner *actions_model.ActionRunner, requestKey, handle *string) (*runnerv1.Task, bool, error) {
|
2025-02-12 00:09:43 +08:00
|
|
|
var (
|
|
|
|
|
task *runnerv1.Task
|
|
|
|
|
job *actions_model.ActionRunJob
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-16 18:56:56 +01:00
|
|
|
if runner.Ephemeral {
|
|
|
|
|
hasRunnerAssignedTask, err := actions_model.HasTaskForRunner(ctx, runner.ID)
|
|
|
|
|
// Let the runner retry the request, do not allow to proceed
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if runner has task, dont assign new task
|
|
|
|
|
if hasRunnerAssignedTask {
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 00:09:43 +08:00
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
2026-03-25 17:27:05 +01:00
|
|
|
t, ok, err := actions_model.CreateTaskForRunner(ctx, runner, requestKey, handle)
|
2025-02-12 00:09:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("CreateTaskForRunner: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := t.LoadAttributes(ctx); err != nil {
|
|
|
|
|
return fmt.Errorf("task LoadAttributes: %w", err)
|
|
|
|
|
}
|
|
|
|
|
job = t.Job
|
|
|
|
|
|
2025-12-30 17:33:21 +01:00
|
|
|
secrets, err := getSecretsOfTask(ctx, t)
|
2025-02-12 00:09:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("GetSecretsOfTask: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vars, err := actions_model.GetVariablesOfRun(ctx, t.Job.Run)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("GetVariablesOfRun: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
needs, err := findTaskNeeds(ctx, job)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("findTaskNeeds: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:46:58 +02:00
|
|
|
unit, err := t.Job.Run.Repo.GetUnit(ctx, unit.TypeActions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("GetUnit: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskContext, err := generateTaskContext(t, unit.ActionsConfig())
|
2025-02-12 00:09:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("generateTaskContext: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
task = &runnerv1.Task{
|
|
|
|
|
Id: t.ID,
|
|
|
|
|
WorkflowPayload: t.Job.WorkflowPayload,
|
|
|
|
|
Context: taskContext,
|
|
|
|
|
Secrets: secrets,
|
|
|
|
|
Vars: vars,
|
|
|
|
|
Needs: needs,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}); err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if task == nil {
|
|
|
|
|
return nil, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreateCommitStatus(ctx, job)
|
|
|
|
|
|
|
|
|
|
return task, true, nil
|
|
|
|
|
}
|
|
|
|
|
|
fix: allow Actions runner to recover tasks lost during fetching from intermittent errors (#11401)
Probably fixes (or improves, at least) https://code.forgejo.org/forgejo/runner/issues/1391, paired with the runner implementation https://code.forgejo.org/forgejo/runner/pulls/1393.
When the FetchTask() API is invoked to create a task, unpreventable environmental errors may occur; for example, network disconnects and timeouts. It's possible that these errors occur after the server-side has assigned a task to the runner during the API call, in which case the error would cause that task to be lost between the two systems -- the server will think it's assigned to the runner, and the runner never received it. This can cause jobs to appear stuck at "Set up job".
The solution implemented here is idempotency in the FetchTask() API call, which means that the "same" FetchTask() API call is expected to return the same values. Specifically, the runner creates a unique identifier which is transmitted to the server as a header `x-runner-request-key` with each FetchTask() invocation which defines the sameness of the call, and the runner retains the value until the API call receives a successful response. The server implementation returns the same tasks back if a second (or Nth) call is received with the same `x-runner-request-key` header. In order to accomplish this is records the `x-runner-request-key` value that is used with each request that assigns tasks.
As a complication, the Forgejo server is unable to return the same `${{ secrets.forgejo_token }}` for the task because the server stores that value in a one-way hash in the database. To resolve this, the server regenerates the token when retrieving tasks for a second time.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).
### Tests for Go changes
(can be removed for JavaScript changes)
- I added test coverage for Go changes...
- [x] in their respective `*_test.go` for unit tests.
- [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
- [x] `make pr-go` before pushing
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.
*The decision if the pull request will be shown in the release notes is up to the mergers / release team.*
The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11401
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2026-02-22 23:24:38 +01:00
|
|
|
func RecoverTasks(ctx context.Context, tasks []*actions_model.ActionTask) ([]*runnerv1.Task, error) {
|
|
|
|
|
retval := make([]*runnerv1.Task, len(tasks))
|
|
|
|
|
|
|
|
|
|
err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
|
for i, t := range tasks {
|
|
|
|
|
// `Token` is stored in the database w/ a one-way hash, so we can't recover it from the original. Instead
|
|
|
|
|
// we generate a new token to create usable runnerv1.Task objects.
|
|
|
|
|
t.GenerateToken()
|
|
|
|
|
if err := t.UpdateToken(ctx); err != nil {
|
|
|
|
|
return fmt.Errorf("UpdateTask failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := t.LoadAttributes(ctx); err != nil {
|
|
|
|
|
return fmt.Errorf("task LoadAttributes: %w", err)
|
|
|
|
|
}
|
|
|
|
|
job := t.Job
|
|
|
|
|
|
|
|
|
|
secrets, err := getSecretsOfTask(ctx, t)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("GetSecretsOfTask: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vars, err := actions_model.GetVariablesOfRun(ctx, t.Job.Run)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("GetVariablesOfRun: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
needs, err := findTaskNeeds(ctx, job)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("findTaskNeeds: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:46:58 +02:00
|
|
|
unit, err := t.Job.Run.Repo.GetUnit(ctx, unit.TypeActions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("GetUnit: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskContext, err := generateTaskContext(t, unit.ActionsConfig())
|
fix: allow Actions runner to recover tasks lost during fetching from intermittent errors (#11401)
Probably fixes (or improves, at least) https://code.forgejo.org/forgejo/runner/issues/1391, paired with the runner implementation https://code.forgejo.org/forgejo/runner/pulls/1393.
When the FetchTask() API is invoked to create a task, unpreventable environmental errors may occur; for example, network disconnects and timeouts. It's possible that these errors occur after the server-side has assigned a task to the runner during the API call, in which case the error would cause that task to be lost between the two systems -- the server will think it's assigned to the runner, and the runner never received it. This can cause jobs to appear stuck at "Set up job".
The solution implemented here is idempotency in the FetchTask() API call, which means that the "same" FetchTask() API call is expected to return the same values. Specifically, the runner creates a unique identifier which is transmitted to the server as a header `x-runner-request-key` with each FetchTask() invocation which defines the sameness of the call, and the runner retains the value until the API call receives a successful response. The server implementation returns the same tasks back if a second (or Nth) call is received with the same `x-runner-request-key` header. In order to accomplish this is records the `x-runner-request-key` value that is used with each request that assigns tasks.
As a complication, the Forgejo server is unable to return the same `${{ secrets.forgejo_token }}` for the task because the server stores that value in a one-way hash in the database. To resolve this, the server regenerates the token when retrieving tasks for a second time.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).
### Tests for Go changes
(can be removed for JavaScript changes)
- I added test coverage for Go changes...
- [x] in their respective `*_test.go` for unit tests.
- [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
- [x] `make pr-go` before pushing
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.
*The decision if the pull request will be shown in the release notes is up to the mergers / release team.*
The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11401
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2026-02-22 23:24:38 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("generateTaskContext: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retval[i] = &runnerv1.Task{
|
|
|
|
|
Id: t.ID,
|
|
|
|
|
WorkflowPayload: t.Job.WorkflowPayload,
|
|
|
|
|
Context: taskContext,
|
|
|
|
|
Secrets: secrets,
|
|
|
|
|
Vars: vars,
|
|
|
|
|
Needs: needs,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retval, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:46:58 +02:00
|
|
|
func generateTaskContext(t *actions_model.ActionTask, ac *repo_model.ActionsConfig) (*structpb.Struct, error) {
|
2026-01-15 03:39:00 +01:00
|
|
|
run := t.Job.Run
|
|
|
|
|
gitCtx, err := GenerateGiteaContext(run, t.Job)
|
2025-02-12 00:09:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-01-15 03:39:00 +01:00
|
|
|
gitCtx["token"] = t.Token
|
2025-02-12 00:09:43 +08:00
|
|
|
|
2026-01-15 03:39:00 +01:00
|
|
|
enableOpenIDConnect, err := t.Job.EnableOpenIDConnect()
|
2025-12-29 15:37:44 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-01-15 03:39:00 +01:00
|
|
|
|
|
|
|
|
// Override the setting from the workflow is this is coming from a fork pull request
|
|
|
|
|
// and this isn't a pull_request_target event.
|
|
|
|
|
if run.IsForkPullRequest && run.TriggerEvent != actions_module.GithubEventPullRequestTarget {
|
|
|
|
|
enableOpenIDConnect = false
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:46:58 +02:00
|
|
|
giteaRuntimeToken, err := CreateAuthorizationToken(t, gitCtx, enableOpenIDConnect, ac)
|
2026-01-15 03:39:00 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 00:09:43 +08:00
|
|
|
gitCtx["gitea_runtime_token"] = giteaRuntimeToken
|
|
|
|
|
|
2026-01-15 03:39:00 +01:00
|
|
|
if enableOpenIDConnect {
|
|
|
|
|
gitCtx["forgejo_actions_id_token_request_token"] = giteaRuntimeToken
|
|
|
|
|
// The "placeholder=true" at the end of the URL is meaningless, but we need a param
|
|
|
|
|
// here if we want to match the format used in GitHub actions examples (e.g., to ensure
|
|
|
|
|
// that "ACTIONS_ID_TOKEN_REQUEST_URL&audience=..." will work as expected).
|
|
|
|
|
gitCtx["forgejo_actions_id_token_request_url"] = setting.AppURL + setting.AppSubURL + fmt.Sprintf("api/actions/_apis/pipelines/workflows/%d/idtoken?placeholder=true", t.Job.RunID)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-12 00:09:43 +08:00
|
|
|
return structpb.NewStruct(gitCtx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func findTaskNeeds(ctx context.Context, taskJob *actions_model.ActionRunJob) (map[string]*runnerv1.TaskNeed, error) {
|
|
|
|
|
taskNeeds, err := FindTaskNeeds(ctx, taskJob)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ret := make(map[string]*runnerv1.TaskNeed, len(taskNeeds))
|
|
|
|
|
for jobID, taskNeed := range taskNeeds {
|
|
|
|
|
ret[jobID] = &runnerv1.TaskNeed{
|
|
|
|
|
Outputs: taskNeed.Outputs,
|
|
|
|
|
Result: runnerv1.Result(taskNeed.Result),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
2025-04-08 14:32:16 +02:00
|
|
|
|
|
|
|
|
func StopTask(ctx context.Context, taskID int64, status actions_model.Status) error {
|
|
|
|
|
if !status.IsDone() {
|
|
|
|
|
return fmt.Errorf("cannot stop task with status %v", status)
|
|
|
|
|
}
|
|
|
|
|
e := db.GetEngine(ctx)
|
|
|
|
|
|
|
|
|
|
task := &actions_model.ActionTask{}
|
|
|
|
|
if has, err := e.ID(taskID).Get(task); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if !has {
|
|
|
|
|
return util.ErrNotExist
|
|
|
|
|
}
|
|
|
|
|
if task.Status.IsDone() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := timeutil.TimeStampNow()
|
|
|
|
|
task.Status = status
|
|
|
|
|
task.Stopped = now
|
2025-04-24 15:15:24 +00:00
|
|
|
if _, err := UpdateRunJob(ctx, &actions_model.ActionRunJob{
|
2025-04-08 14:32:16 +02:00
|
|
|
ID: task.JobID,
|
|
|
|
|
Status: task.Status,
|
|
|
|
|
Stopped: task.Stopped,
|
|
|
|
|
}, nil); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := actions_model.UpdateTask(ctx, task, "status", "stopped"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 18:56:56 +01:00
|
|
|
runner := &actions_model.ActionRunner{}
|
|
|
|
|
if _, err := e.ID(task.RunnerID).Get(runner); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to find runner assigned to task")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if runner.Ephemeral {
|
|
|
|
|
err := actions_model.DeleteRunner(ctx, runner)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to remove ephemeral runner from stopped task: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 14:32:16 +02:00
|
|
|
if err := task.LoadAttributes(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, step := range task.Steps {
|
|
|
|
|
if !step.Status.IsDone() {
|
|
|
|
|
step.Status = status
|
|
|
|
|
if step.Started == 0 {
|
|
|
|
|
step.Started = now
|
|
|
|
|
}
|
|
|
|
|
step.Stopped = now
|
|
|
|
|
}
|
|
|
|
|
if _, err := e.ID(step.ID).Update(step); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Revert "fix: ensure actions logs are transferred when a task is done (#10008)" (#11462)
This reverts commit d4951968f0abbee8c614acec648240c4b25e7ec7, #10008.
When Forgejo cancels a job server-side, for example due to an additional push to an open PR, it immediately archives the logs from DBFS to disk due to the changes in #10008. Then, the runner recognizes that the job status is cancelled and it attempts to flush its pending logs to Forgejo, resulting in warnings being logged:
```
forgejo-runner.log:time="2026-02-23T01:32:11+01:00" level=warning msg="uploading final logs failed, but will be retried: already_exists: log file has been archived" task_id=51
forgejo-runner.log:time="2026-02-23T01:32:11+01:00" level=warning msg="uploading final logs failed, but will be retried: already_exists: log file has been archived" task_id=51
forgejo-runner.log:time="2026-02-23T01:32:11+01:00" level=warning msg="uploading final logs failed, but will be retried: already_exists: log file has been archived" task_id=51
forgejo-runner.log:time="2026-02-23T01:32:12+01:00" level=warning msg="uploading final logs failed, but will be retried: already_exists: log file has been archived" task_id=51
forgejo-runner.log:time="2026-02-23T01:32:13+01:00" level=warning msg="uploading final logs failed, but will be retried: already_exists: log file has been archived" task_id=51
forgejo-runner.log:time="2026-02-23T01:32:14+01:00" level=warning msg="uploading final logs failed, but will be retried: already_exists: log file has been archived" task_id=51
forgejo-runner.log:time="2026-02-23T01:32:16+01:00" level=info msg="runner: received shutdown signal"
forgejo-runner.log:time="2026-02-23T01:32:16+01:00" level=info msg="runner: shutdown initiated, waiting [runner].shutdown_timeout=0s for running jobs to complete before shutting down"
forgejo-runner.log:time="2026-02-23T01:32:16+01:00" level=info msg="[poller] shutdown begin, 1 tasks currently running"
forgejo-runner.log:time="2026-02-23T01:32:16+01:00" level=info msg="forcing the jobs to shutdown"
forgejo-runner.log:time="2026-02-23T01:32:18+01:00" level=warning msg="uploading final logs failed, but will be retried: already_exists: log file has been archived" task_id=51
forgejo-runner.log:time="2026-02-23T01:32:24+01:00" level=warning msg="uploading final logs failed, but will be retried: already_exists: log file has been archived" task_id=51
```
This appears to be the cause of the `push-cancel` end-to-end test failing since #10008 was merged. https://code.forgejo.org/forgejo/end-to-end/actions/runs/4985/jobs/8/attempt/1 The `push-cancel` test case itself seems to succeed, but then the test process aborts with `return 1`. Doesn't reproduce locally.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11462
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2026-03-02 15:34:09 +01:00
|
|
|
return nil
|
2025-04-08 14:32:16 +02:00
|
|
|
}
|
2025-04-08 17:41:13 +02:00
|
|
|
|
|
|
|
|
// UpdateTaskByState updates the task by the state.
|
|
|
|
|
// It will always update the task if the state is not final, even there is no change.
|
|
|
|
|
// So it will update ActionTask.Updated to avoid the task being judged as a zombie task.
|
|
|
|
|
func UpdateTaskByState(ctx context.Context, runnerID int64, state *runnerv1.TaskState) (*actions_model.ActionTask, error) {
|
|
|
|
|
stepStates := map[int64]*runnerv1.StepState{}
|
|
|
|
|
for _, v := range state.Steps {
|
|
|
|
|
stepStates[v.Id] = v
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 22:57:33 +01:00
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
2025-04-08 17:41:13 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-01-26 22:57:33 +01:00
|
|
|
defer committer.Close()
|
2025-04-08 17:41:13 +02:00
|
|
|
|
|
|
|
|
e := db.GetEngine(ctx)
|
|
|
|
|
|
|
|
|
|
task := &actions_model.ActionTask{}
|
|
|
|
|
if has, err := e.ID(state.Id).Get(task); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else if !has {
|
|
|
|
|
return nil, util.ErrNotExist
|
|
|
|
|
} else if runnerID != task.RunnerID {
|
2025-05-29 17:34:29 +02:00
|
|
|
return nil, errors.New("invalid runner for task")
|
2025-04-08 17:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if task.Status.IsDone() {
|
|
|
|
|
// the state is final, do nothing
|
|
|
|
|
return task, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// state.Result is not unspecified means the task is finished
|
|
|
|
|
if state.Result != runnerv1.Result_RESULT_UNSPECIFIED {
|
|
|
|
|
task.Status = actions_model.Status(state.Result)
|
|
|
|
|
task.Stopped = timeutil.TimeStamp(state.StoppedAt.AsTime().Unix())
|
|
|
|
|
if err := actions_model.UpdateTask(ctx, task, "status", "stopped"); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-04-24 15:15:24 +00:00
|
|
|
if _, err := UpdateRunJob(ctx, &actions_model.ActionRunJob{
|
2025-04-08 17:41:13 +02:00
|
|
|
ID: task.JobID,
|
|
|
|
|
Status: task.Status,
|
|
|
|
|
Stopped: task.Stopped,
|
|
|
|
|
}, nil); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Force update ActionTask.Updated to avoid the task being judged as a zombie task
|
|
|
|
|
task.Updated = timeutil.TimeStampNow()
|
|
|
|
|
if err := actions_model.UpdateTask(ctx, task, "updated"); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := task.LoadAttributes(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, step := range task.Steps {
|
|
|
|
|
var result runnerv1.Result
|
|
|
|
|
if v, ok := stepStates[step.Index]; ok {
|
|
|
|
|
result = v.Result
|
|
|
|
|
step.LogIndex = v.LogIndex
|
|
|
|
|
step.LogLength = v.LogLength
|
|
|
|
|
step.Started = convertTimestamp(v.StartedAt)
|
|
|
|
|
step.Stopped = convertTimestamp(v.StoppedAt)
|
|
|
|
|
}
|
|
|
|
|
if result != runnerv1.Result_RESULT_UNSPECIFIED {
|
|
|
|
|
step.Status = actions_model.Status(result)
|
|
|
|
|
} else if step.Started != 0 {
|
|
|
|
|
step.Status = actions_model.StatusRunning
|
|
|
|
|
}
|
|
|
|
|
if _, err := e.ID(step.ID).Update(step); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 22:57:33 +01:00
|
|
|
if err := committer.Commit(); err != nil {
|
2025-04-08 17:41:13 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return task, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func convertTimestamp(timestamp *timestamppb.Timestamp) timeutil.TimeStamp {
|
|
|
|
|
if timestamp.GetSeconds() == 0 && timestamp.GetNanos() == 0 {
|
|
|
|
|
return timeutil.TimeStamp(0)
|
|
|
|
|
}
|
|
|
|
|
return timeutil.TimeStamp(timestamp.AsTime().Unix())
|
|
|
|
|
}
|
2026-05-11 16:02:36 +02:00
|
|
|
|
|
|
|
|
// deleteTask removes the given task with all associated steps, outputs, logs, and ephemeral runners, if any. For
|
|
|
|
|
// deleteTask to succeed, it must have completed. If it has not, an error is returned. If the given task does not exist,
|
|
|
|
|
// nothing happens.
|
|
|
|
|
func deleteTask(ctx context.Context, taskID int64) error {
|
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
|
task, err := actions_model.GetTaskByID(ctx, taskID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("unable to load task %d: %w", taskID, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !task.Status.IsDone() {
|
|
|
|
|
return fmt.Errorf("unable to remove task %d because it has not completed yet", taskID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = actions_module.RemoveLogs(ctx, task.LogInStorage, task.LogFilename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("unable to remove logs of task %d: %w", taskID, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Whether an ephemeral runner has been used is determined based on whether it is assigned to a task.
|
|
|
|
|
// Consequently, ephemeral runners have to be cleaned up before any task can be removed.
|
|
|
|
|
err = actions_model.DeleteEphemeralRunner(ctx, task.RunnerID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("unable to cleanup ephemeral runners before removing task %d: %w", taskID, err)
|
|
|
|
|
}
|
|
|
|
|
err = actions_model.DeleteTask(ctx, task.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("unable to remove task %d: %w", task.ID, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|