jojo/models/actions
Mathieu Fenniak 5f757d9e83 [v13.0/forgejo] feat: allow workflows to control cancellation of existing jobs (#9797)
Partial backport of #9434.

Fixes a bug: it was intended that pushes to a branch and pushes to a pull request would cancel any previous running job from the same workflow, however this functionality only worked on a `on: push` workflow.  This PR fixes it for an `on: pull_request` workflow.

Adds a feature: `concurrency.cancel-in-progress` can be used to override the automatic cancellation behaviour and disable it.

It can be disabled unconditionally in a workflow:
```yaml
concurrency:
    cancel-in-progress: false
```

Or it can be disabled with some logic; for example, keeping the cancel behaviour on PRs but disabling it otherwise:
```yaml
concurrency:
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}
```

Only a small subset of automated tests were applicable for backport, so I supplemented with manual testing in these cases:

|                                                                    | Expected      | Actual        |
| ------------------------------------------------------------------ | ------------- | ------------- |
| Default behaviour:                                                 |               |               |
| on: pull_request -- push to a PR when it's already running         | Cancelled     | Cancelled     |
| on: push -- push to a branch (eg. main) when it's already running  | Cancelled     | Cancelled     |
| on: workflow_dispatch -- run again when it is already running      | Multiple Runs | Multiple Runs |
|                                                                    |               |               |
| Set cancel-in-progress: true                                       |               |               |
| on: pull_request -- push to a PR when it's already running         | Cancelled     | Cancelled     |
| on: push -- push to a branch (eg. main) when it's already running  | Cancelled     | Cancelled     |
| on: workflow_dispatch -- run again when it is already running      | Multiple Runs | Multiple Runs |
|                                                                    |               |               |
| Set cancel-in-progress: false                                      |               |               |
| on: pull_request -- push to a PR when it's already running         | Multiple Runs | Multiple Runs |
| on: push -- push to a branch (eg. main) when it's already running  | Multiple Runs | Multiple Runs |
| on: workflow_dispatch -- run again when it is already running      | Multiple Runs | Multiple Runs |
|                                                                    |               |               |
| Set cancel-in-progress: ${{ github.event_name == 'pull_request' }} |               |               |
| on: pull_request -- push to a PR when it's already running         | Cancelled     | Cancelled     |
| on: push -- push to a branch (eg. main) when it's already running  | Multiple Runs | Multiple Runs |
| on: workflow_dispatch -- run again when it is already running      | Multiple Runs | Multiple Runs |

## 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

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- 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.
  - **TODO:**  Will backport the relevant documentation.
- [ ] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/9797): <!--number 9797 --><!--line 0 --><!--description ZmVhdDogYWxsb3cgd29ya2Zsb3dzIHRvIGNvbnRyb2wgY2FuY2VsbGF0aW9uIG9mIGV4aXN0aW5nIGpvYnM=-->feat: allow workflows to control cancellation of existing jobs<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9797
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-11-23 23:01:09 +01:00
..
artifact.go feat: artifacts can be downloaded using their id instead of their name (#8957) 2025-08-25 15:51:26 +02:00
forgejo.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
forgejo_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
main_test.go feat: ability to view previous logs for Actions runs that have been retried (#9017) 2025-09-04 22:46:22 +02:00
run.go [v13.0/forgejo] feat: allow workflows to control cancellation of existing jobs (#9797) 2025-11-23 23:01:09 +01:00
run_job.go fix: loading action logs on a task that isn't fetched yet, fails when the job is fetched (#9293) 2025-09-14 14:30:02 +02:00
run_job_list.go feat: make action runs available in api (#7699) 2025-06-02 22:05:12 +02:00
run_job_status_test.go Improve Actions status aggregations (#32860) 2024-12-22 08:46:38 +01:00
run_job_test.go feat: ability to view previous logs for Actions runs that have been retried (#9017) 2025-09-04 22:46:22 +02:00
run_list.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
run_test.go fix: disable Forgejo Actions email notifications on recovery (#8374) 2025-07-02 19:23:07 +02:00
runner.go feat: auto cleanup of offline runners (#7803) 2025-06-08 00:13:37 +02:00
runner_list.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
runner_test.go feat: auto cleanup of offline runners (#7803) 2025-06-08 00:13:37 +02:00
runner_token.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
runner_token_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
schedule.go feat: move StopTask, CancelPreviousJobs and CleanRepoScheduleTasks to services/actions 2025-04-10 08:38:27 +02:00
schedule_spec.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
schedule_spec_list.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
schedule_spec_test.go Use UTC as default timezone when schedule Actions cron tasks (#31742) 2024-08-04 18:24:10 +02:00
status.go feat: make action runs available in api (#7699) 2025-06-02 22:05:12 +02:00
task.go [v13.0/forgejo] fix(perf): add missing index on action_task table (#9795) 2025-10-21 18:40:28 +02:00
task_list.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
task_output.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
task_step.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
task_test.go feat: ability to view previous logs for Actions runs that have been retried (#9017) 2025-09-04 22:46:22 +02:00
tasks_version.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
utils.go chore(sec): unify usage of crypto/rand.Read (#7453) 2025-04-04 03:31:37 +00:00
utils_test.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
variable.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00