mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-15 07:20:26 +00:00
Forgejo Actions keeps one set of artifacts per workflow run -- those of the latest workflow run. If a particular workflow run is rerun, Forgejo is supposed to remove outdated artifacts. However, it does not do that. As a result, the user is presented a mix of outdated and new artifacts, even within the same archive.
This is remedied by wiping the artifacts before each rerun. The same happens when only one or more jobs are rerun, which also matches the behaviour of GitHub Actions. In the example below, when only rerunning `artifacts-two`, `many-artifacts-one` would disappear and a new version of `many-artifacts-two` would be made available.
Reproducer:
```yaml
on:
push:
jobs:
artifacts-one:
runs-on: ubuntu-latest
steps:
- run: mkdir -p artifacts-one
- run: |
if [[ "${{ github.run_attempt}}" == 1 ]] ; then echo "${{ github.run_attempt}}" > artifacts-one/ONE; fi
echo "${{ github.run_attempt}}" > artifacts-one/TWO
- uses: forgejo/upload-artifact@v4
with:
name: many-artifacts-one
path: artifacts-one/
artifacts-two:
runs-on: ubuntu-latest
steps:
- run: mkdir -p artifacts-two
- run: |
if [[ "${{ github.run_attempt}}" == 1 ]] ; then echo "${{ github.run_attempt}}" > artifacts-two/ONE; fi
echo "${{ github.run_attempt}}" > artifacts-two/TWO
- uses: forgejo/upload-artifact@v4
with:
name: many-artifacts-two
path: artifacts-two/
```
Resolves https://codeberg.org/forgejo/forgejo/issues/12163.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). 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...
- [ ] 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
### Tests for JavaScript changes
(can be removed for Go changes)
- I added test coverage for JavaScript changes...
- [ ] in `web_src/js/*.test.js` if it can be unit tested.
- [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).
### 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/12523
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
203 lines
7.1 KiB
Go
203 lines
7.1 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
|
|
actions_model "forgejo.org/models/actions"
|
|
"forgejo.org/models/db"
|
|
"forgejo.org/models/unit"
|
|
"forgejo.org/modules/container"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
var (
|
|
// ErrRerunWorkflowInvalid signals that the workflow cannot be run because it is invalid, for example, due to syntax
|
|
// errors.
|
|
ErrRerunWorkflowInvalid = errors.New("workflow is invalid")
|
|
// ErrRerunWorkflowDisabled indicates that the workflow cannot be run because it has been disabled by the user or
|
|
// Forgejo.
|
|
ErrRerunWorkflowDisabled = errors.New("workflow is disabled")
|
|
// ErrRerunWorkflowStillRunning signals that the workflow cannot be rerun because at least one job is still running.
|
|
ErrRerunWorkflowStillRunning = errors.New("workflow is still running")
|
|
// ErrRerunJobStillRunning signals that the job cannot be rerun because it is still running.
|
|
ErrRerunJobStillRunning = errors.New("job is still running")
|
|
)
|
|
|
|
// GetAllRerunJobs get all jobs that need to be rerun when job should be rerun
|
|
func GetAllRerunJobs(job *actions_model.ActionRunJob, allJobs []*actions_model.ActionRunJob) []*actions_model.ActionRunJob {
|
|
rerunJobs := []*actions_model.ActionRunJob{job}
|
|
rerunJobsIDSet := make(container.Set[string])
|
|
rerunJobsIDSet.Add(job.JobID)
|
|
|
|
for _, j := range allJobs {
|
|
if rerunJobsIDSet.Contains(j.JobID) {
|
|
continue
|
|
}
|
|
if slices.ContainsFunc(j.Needs, rerunJobsIDSet.Contains) {
|
|
rerunJobs = append(rerunJobs, j)
|
|
rerunJobsIDSet.Add(j.JobID)
|
|
}
|
|
}
|
|
|
|
return rerunJobs
|
|
}
|
|
|
|
// RerunAllJobs reruns all jobs of the given run and returns them. For it to succeed, the workflow must be valid, and the
|
|
// previous run must have completed.
|
|
func RerunAllJobs(ctx context.Context, run *actions_model.ActionRun) ([]*actions_model.ActionRunJob, error) {
|
|
if !run.IsValid() {
|
|
return nil, ErrRerunWorkflowInvalid
|
|
}
|
|
if !run.Status.IsDone() {
|
|
return nil, ErrRerunWorkflowStillRunning
|
|
}
|
|
|
|
if err := run.LoadRepo(ctx); err != nil {
|
|
return nil, fmt.Errorf("cannot load repo of run %d: %w", run.ID, err)
|
|
}
|
|
|
|
actionsConfig := run.Repo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
|
|
if actionsConfig.IsWorkflowDisabled(run.WorkflowID) {
|
|
return nil, ErrRerunWorkflowDisabled
|
|
}
|
|
|
|
var rerunJobs []*actions_model.ActionRunJob
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
if run.Status != actions_model.StatusUnknown && !run.Status.IsDone() {
|
|
return fmt.Errorf("cannot prepare next attempt because run %d is active: %s", run.ID, run.Status.String())
|
|
}
|
|
|
|
// Wipe all artifacts before a rerun to prevent stale artifacts from polluting artifacts collected during the
|
|
// rerun.
|
|
if err := actions_model.SetArtifactsOfRunDeleted(ctx, run.ID); err != nil {
|
|
return fmt.Errorf("cannot remove artifacts of previous run of run %d: %w", run.ID, err)
|
|
}
|
|
|
|
run.PreviousDuration = run.Duration()
|
|
|
|
run.Status = actions_model.StatusWaiting
|
|
run.Started = 0
|
|
run.Stopped = 0
|
|
|
|
// The columns have to be specified here to work around a xorm quirk: It won't update columns that are set to
|
|
// their zero value without AllCols().
|
|
if err := UpdateRun(ctx, run, "status", "started", "stopped", "previous_duration"); err != nil {
|
|
return fmt.Errorf("cannot update run %d: %w", run.ID, err)
|
|
}
|
|
|
|
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("could not load jobs of run %d: %w", run.ID, err)
|
|
}
|
|
|
|
for _, job := range jobs {
|
|
initialStatus := actions_model.StatusWaiting
|
|
if len(job.Needs) > 0 {
|
|
initialStatus = actions_model.StatusBlocked
|
|
}
|
|
|
|
if err := rerunSingleJob(ctx, job, initialStatus); err != nil {
|
|
return fmt.Errorf("could not rerun job %d of run %d: %w", job.ID, run.ID, err)
|
|
}
|
|
|
|
rerunJobs = append(rerunJobs, job)
|
|
}
|
|
|
|
return nil
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
return rerunJobs, nil
|
|
}
|
|
|
|
// RerunJob reruns the given job and all its dependent jobs. It returns all jobs that were rerun. For it to succeed, the
|
|
// workflow that defines this job must be valid, and the previous run must have completed. Dependent jobs that have not
|
|
// completed yet are ignored.
|
|
func RerunJob(ctx context.Context, job *actions_model.ActionRunJob) ([]*actions_model.ActionRunJob, error) {
|
|
if err := job.LoadAttributes(ctx); err != nil {
|
|
return nil, fmt.Errorf("cannot load attributes of job %d: %w", job.ID, err)
|
|
}
|
|
if !job.Run.IsValid() {
|
|
return nil, ErrRerunWorkflowInvalid
|
|
}
|
|
|
|
actionsConfig := job.Run.Repo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
|
|
if actionsConfig.IsWorkflowDisabled(job.Run.WorkflowID) {
|
|
return nil, ErrRerunWorkflowDisabled
|
|
}
|
|
|
|
if !job.Status.IsDone() {
|
|
return nil, ErrRerunJobStillRunning
|
|
}
|
|
|
|
var rerunJobs []*actions_model.ActionRunJob
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
jobs, err := actions_model.GetRunJobsByRunID(ctx, job.RunID)
|
|
if err != nil {
|
|
return fmt.Errorf("could not load jobs of run %d: %w", job.RunID, err)
|
|
}
|
|
|
|
// Wipe all artifacts before a rerun to prevent stale artifacts from polluting the artifacts collected during
|
|
// the rerun. Because artifacts are bound to a run and not to a job, it is not possible to only remove the
|
|
// artifacts of the jobs that are going to be rerun. That means that artifacts created by jobs that are not
|
|
// rerun will be lost. That matches GitHub Actions' behaviour as of May 2026.
|
|
if err := actions_model.SetArtifactsOfRunDeleted(ctx, job.RunID); err != nil {
|
|
return fmt.Errorf("cannot remove artifacts of previous run of run %d: %w", job.RunID, err)
|
|
}
|
|
|
|
for _, jobToRerun := range GetAllRerunJobs(job, jobs) {
|
|
canBeRerun, err := jobToRerun.CanBeRerun(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot determine whether job %d can be rerun: %w", jobToRerun.ID, err)
|
|
}
|
|
|
|
// Skipping jobs that cannot be rerun is wrong. They should be cancelled and rerun, instead, because they
|
|
// are dependent jobs and the old results might be worthless, anyway. But we keep that behaviour for now,
|
|
// because changing it requires more rework.
|
|
if !canBeRerun {
|
|
continue
|
|
}
|
|
|
|
// The job that should be rerun cannot be blocked, even if it has needs.
|
|
initialStatus := actions_model.StatusWaiting
|
|
if len(jobToRerun.Needs) > 0 && jobToRerun.ID != job.ID {
|
|
initialStatus = actions_model.StatusBlocked
|
|
}
|
|
|
|
if err := rerunSingleJob(ctx, jobToRerun, initialStatus); err != nil {
|
|
return fmt.Errorf("cannot rerun job %d: %w", jobToRerun.ID, err)
|
|
}
|
|
rerunJobs = append(rerunJobs, jobToRerun)
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return rerunJobs, nil
|
|
}
|
|
|
|
func rerunSingleJob(ctx context.Context, job *actions_model.ActionRunJob, initialStatus actions_model.Status) error {
|
|
oldStatus := job.Status
|
|
|
|
if err := job.PrepareNextAttempt(initialStatus); err != nil {
|
|
return err
|
|
}
|
|
|
|
// The columns have to be specified here to work around a xorm quirk: It won't update columns that are set to their
|
|
// zero value without AllCols().
|
|
if _, err := UpdateRunJob(ctx, job, builder.Eq{"status": oldStatus}, "handle", "attempt", "task_id", "status", "started", "stopped"); err != nil {
|
|
return err
|
|
}
|
|
|
|
CreateCommitStatus(ctx, job)
|
|
|
|
return nil
|
|
}
|