mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-12-07 14:09:47 +00:00
Allows referencing the outputs of previously executed jobs in the `runs-on` field directly by a `${{ needs.some-job.outputs.some-output }}`, and also *indirectly* through the job's `strategy.matrix`. At its most complicated, supports a workflow with dynamic matrices like this:
```yaml
jobs:
define-matrix:
runs-on: docker
outputs:
array-value: ${{ steps.define.outputs.array }}
steps:
- id: define
run: |
echo 'array=["debian-bookworm", "debian-trixie"]' >> "$FORGEJO_OUTPUT"
runs-on-dynamic-matrix:
needs: define-matrix
strategy:
matrix:
my-runners: ${{ fromJSON(needs.define-matrix.outputs.array-value) }}
runs-on: ${{ matrix.my-runners }}
steps:
- run: uname -a
```
## 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.
- [x] I did not document these changes and I do not expect someone else to do it.
- Documentation already (incorrectly) states that `jobs.<job-id>.runs-on` can access the `needs` context. 😛 https://forgejo.org/docs/latest/user/actions/reference/#availability
### 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/10308): <!--number 10308 --><!--line 0 --><!--description ZmVhdChhY3Rpb25zKTogc3VwcG9ydCByZWZlcmVuY2luZyBgJHt7IG5lZWRzLi4uIH19YCB2YXJpYWJsZXMgaW4gYHJ1bnMtb25g-->feat(actions): support referencing `${{ needs... }}` variables in `runs-on`<!--description-->
<!--end release-notes-assistant-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10308
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
387 lines
14 KiB
Go
387 lines
14 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package actions
|
|
|
|
import (
|
|
"testing"
|
|
|
|
actions_model "forgejo.org/models/actions"
|
|
"forgejo.org/models/db"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/models/unit"
|
|
"forgejo.org/models/unittest"
|
|
"forgejo.org/modules/timeutil"
|
|
webhook_module "forgejo.org/modules/webhook"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestServiceActions_startTask(t *testing.T) {
|
|
defer unittest.OverrideFixtures("services/actions/TestServiceActions_startTask")()
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// Load fixtures that are corrupted and create one valid scheduled workflow
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
|
|
|
workflowID := "some.yml"
|
|
schedules := []*actions_model.ActionSchedule{
|
|
{
|
|
Title: "scheduletitle1",
|
|
RepoID: repo.ID,
|
|
OwnerID: repo.OwnerID,
|
|
WorkflowID: workflowID,
|
|
TriggerUserID: repo.OwnerID,
|
|
Ref: "branch",
|
|
CommitSHA: "fakeSHA",
|
|
Event: webhook_module.HookEventSchedule,
|
|
EventPayload: "fakepayload",
|
|
Specs: []string{"* * * * *"},
|
|
Content: []byte(
|
|
`
|
|
jobs:
|
|
job2:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- run: true
|
|
`),
|
|
},
|
|
}
|
|
|
|
require.Equal(t, 2, unittest.GetCount(t, actions_model.ActionScheduleSpec{}))
|
|
require.NoError(t, actions_model.CreateScheduleTask(t.Context(), schedules))
|
|
require.Equal(t, 3, unittest.GetCount(t, actions_model.ActionScheduleSpec{}))
|
|
_, err := db.GetEngine(db.DefaultContext).Exec("UPDATE `action_schedule_spec` SET next = 1")
|
|
require.NoError(t, err)
|
|
|
|
// After running startTasks an ActionRun row is created for the valid scheduled workflow
|
|
require.Empty(t, unittest.GetCount(t, actions_model.ActionRun{WorkflowID: workflowID}))
|
|
require.NoError(t, startTasks(t.Context()))
|
|
require.NotEmpty(t, unittest.GetCount(t, actions_model.ActionRun{WorkflowID: workflowID}))
|
|
|
|
// The invalid workflows loaded from the fixtures are disabled
|
|
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
|
actionUnit, err := repo.GetUnit(t.Context(), unit.TypeActions)
|
|
require.NoError(t, err)
|
|
actionConfig := actionUnit.ActionsConfig()
|
|
assert.True(t, actionConfig.IsWorkflowDisabled("workflow2.yml"))
|
|
assert.True(t, actionConfig.IsWorkflowDisabled("workflow1.yml"))
|
|
assert.False(t, actionConfig.IsWorkflowDisabled("some.yml"))
|
|
}
|
|
|
|
func TestCreateScheduleTask(t *testing.T) {
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2, OwnerID: 2})
|
|
|
|
assertConstant := func(t *testing.T, cron *actions_model.ActionSchedule, run *actions_model.ActionRun) {
|
|
t.Helper()
|
|
assert.Equal(t, cron.Title, run.Title)
|
|
assert.Equal(t, cron.RepoID, run.RepoID)
|
|
assert.Equal(t, cron.OwnerID, run.OwnerID)
|
|
assert.Equal(t, cron.WorkflowID, run.WorkflowID)
|
|
assert.Equal(t, cron.TriggerUserID, run.TriggerUserID)
|
|
assert.Equal(t, cron.Ref, run.Ref)
|
|
assert.Equal(t, cron.CommitSHA, run.CommitSHA)
|
|
assert.Equal(t, cron.Event, run.Event)
|
|
assert.Equal(t, cron.EventPayload, run.EventPayload)
|
|
assert.Equal(t, cron.ID, run.ScheduleID)
|
|
assert.Equal(t, actions_model.StatusWaiting, run.Status)
|
|
assert.Equal(t, "branch_some.yml_schedule__auto", run.ConcurrencyGroup)
|
|
assert.Equal(t, actions_model.UnlimitedConcurrency, run.ConcurrencyType)
|
|
}
|
|
|
|
assertMutable := func(t *testing.T, expected, run *actions_model.ActionRun) {
|
|
t.Helper()
|
|
assert.Equal(t, expected.NotifyEmail, run.NotifyEmail)
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
cron actions_model.ActionSchedule
|
|
want []actions_model.ActionRun
|
|
}{
|
|
{
|
|
name: "simple",
|
|
cron: actions_model.ActionSchedule{
|
|
Title: "scheduletitle1",
|
|
RepoID: repo.ID,
|
|
OwnerID: repo.OwnerID,
|
|
WorkflowID: "some.yml",
|
|
TriggerUserID: repo.OwnerID,
|
|
Ref: "branch",
|
|
CommitSHA: "fakeSHA",
|
|
Event: webhook_module.HookEventSchedule,
|
|
EventPayload: "fakepayload",
|
|
Content: []byte(
|
|
`
|
|
name: test
|
|
on: push
|
|
jobs:
|
|
job2:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- run: true
|
|
`),
|
|
},
|
|
want: []actions_model.ActionRun{
|
|
{
|
|
Title: "scheduletitle1",
|
|
NotifyEmail: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "enable-email-notifications is true",
|
|
cron: actions_model.ActionSchedule{
|
|
Title: "scheduletitle2",
|
|
RepoID: repo.ID,
|
|
OwnerID: repo.OwnerID,
|
|
WorkflowID: "some.yml",
|
|
TriggerUserID: repo.OwnerID,
|
|
Ref: "branch",
|
|
CommitSHA: "fakeSHA",
|
|
Event: webhook_module.HookEventSchedule,
|
|
EventPayload: "fakepayload",
|
|
Content: []byte(
|
|
`
|
|
name: test
|
|
enable-email-notifications: true
|
|
on: push
|
|
jobs:
|
|
job2:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- run: true
|
|
`),
|
|
},
|
|
want: []actions_model.ActionRun{
|
|
{
|
|
Title: "scheduletitle2",
|
|
NotifyEmail: true,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
require.NoError(t, CreateScheduleTask(t.Context(), &testCase.cron))
|
|
require.Equal(t, len(testCase.want), unittest.GetCount(t, actions_model.ActionRun{RepoID: repo.ID}))
|
|
for _, expected := range testCase.want {
|
|
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{Title: expected.Title})
|
|
assertConstant(t, &testCase.cron, run)
|
|
assertMutable(t, &expected, run)
|
|
}
|
|
unittest.AssertSuccessfulDelete(t, actions_model.ActionRun{RepoID: repo.ID})
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCancelPreviousJobs(t *testing.T) {
|
|
defer unittest.OverrideFixtures("services/actions/TestCancelPreviousJobs")()
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: 894})
|
|
assert.Equal(t, actions_model.StatusRunning, run.Status)
|
|
assert.EqualValues(t, 1683636626, run.Updated)
|
|
runJob := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{RunID: 894})
|
|
assert.Equal(t, actions_model.StatusRunning, runJob.Status)
|
|
assert.EqualValues(t, 1683636528, runJob.Started)
|
|
|
|
err := CancelPreviousJobs(t.Context(), 63, "refs/heads/main", "running.yaml", webhook_module.HookEventWorkflowDispatch)
|
|
require.NoError(t, err)
|
|
|
|
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: 894})
|
|
assert.Equal(t, actions_model.StatusCancelled, run.Status)
|
|
assert.Greater(t, run.Updated, timeutil.TimeStamp(1683636626))
|
|
runJob = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{RunID: 894})
|
|
assert.Equal(t, actions_model.StatusCancelled, runJob.Status)
|
|
assert.Greater(t, runJob.Stopped, timeutil.TimeStamp(1683636528))
|
|
}
|
|
|
|
func TestCancelPreviousWithConcurrencyGroup(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
updateRun901 map[string]any
|
|
}{
|
|
// run 900 & 901 in the fixture data have almost the same data and so should both be cancelled by
|
|
// TestCancelPreviousWithConcurrencyGroup -- but each test case will vary something different about 601 to
|
|
// ensure that only run 600 is targeted by the cancellation
|
|
{
|
|
name: "only cancels target repo",
|
|
updateRun901: map[string]any{"repo_id": 2},
|
|
},
|
|
{
|
|
name: "only cancels target concurrency group",
|
|
updateRun901: map[string]any{"concurrency_group": "321cba"},
|
|
},
|
|
{
|
|
name: "only cancels running",
|
|
updateRun901: map[string]any{"status": actions_model.StatusSuccess},
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
defer unittest.OverrideFixtures("services/actions/TestCancelPreviousWithConcurrencyGroup")()
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
e := db.GetEngine(t.Context())
|
|
|
|
expected901Status := actions_model.StatusRunning
|
|
if tc.updateRun901 != nil {
|
|
affected, err := e.Table(&actions_model.ActionRun{}).Where("id = ?", 901).Update(tc.updateRun901)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, 1, affected)
|
|
newStatus, ok := tc.updateRun901["status"]
|
|
if ok {
|
|
expected901Status = newStatus.(actions_model.Status)
|
|
}
|
|
}
|
|
|
|
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: 900})
|
|
assert.Equal(t, actions_model.StatusRunning, run.Status)
|
|
assert.EqualValues(t, 1683636626, run.Updated)
|
|
assert.Equal(t, "abc123", run.ConcurrencyGroup)
|
|
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: 901})
|
|
assert.Equal(t, expected901Status, run.Status)
|
|
runJob := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{RunID: 900})
|
|
assert.Equal(t, actions_model.StatusRunning, runJob.Status)
|
|
assert.EqualValues(t, 1683636528, runJob.Started)
|
|
|
|
// Search for concurrency group should be case-insensitive, which we test here by using a different capitalization
|
|
// than the fixture data
|
|
err := CancelPreviousWithConcurrencyGroup(t.Context(), 63, "ABC123")
|
|
require.NoError(t, err)
|
|
|
|
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: 900})
|
|
assert.Equal(t, actions_model.StatusCancelled, run.Status)
|
|
assert.Greater(t, run.Updated, timeutil.TimeStamp(1683636626))
|
|
runJob = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{RunID: 900})
|
|
assert.Equal(t, actions_model.StatusCancelled, runJob.Status)
|
|
assert.Greater(t, runJob.Stopped, timeutil.TimeStamp(1683636528))
|
|
|
|
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: 901})
|
|
assert.Equal(t, expected901Status, run.Status)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestServiceActions_DynamicMatrix(t *testing.T) {
|
|
defer unittest.OverrideFixtures("services/actions/TestServiceActions_startTask")()
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// Load fixtures that are corrupted and create one valid scheduled workflow
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
|
|
|
workflowID := "some.yml"
|
|
schedules := []*actions_model.ActionSchedule{
|
|
{
|
|
Title: "scheduletitle1",
|
|
RepoID: repo.ID,
|
|
OwnerID: repo.OwnerID,
|
|
WorkflowID: workflowID,
|
|
TriggerUserID: repo.OwnerID,
|
|
Ref: "branch",
|
|
CommitSHA: "fakeSHA",
|
|
Event: webhook_module.HookEventSchedule,
|
|
EventPayload: "fakepayload",
|
|
Specs: []string{"* * * * *"},
|
|
Content: []byte(
|
|
`
|
|
jobs:
|
|
job2:
|
|
runs-on: ubuntu-latest
|
|
strategy:
|
|
matrix:
|
|
dim1: "${{ fromJSON(needs.other-job.outputs.some-output) }}"
|
|
steps:
|
|
- run: true
|
|
`),
|
|
},
|
|
}
|
|
|
|
require.Equal(t, 2, unittest.GetCount(t, actions_model.ActionScheduleSpec{}))
|
|
require.NoError(t, actions_model.CreateScheduleTask(t.Context(), schedules))
|
|
require.Equal(t, 3, unittest.GetCount(t, actions_model.ActionScheduleSpec{}))
|
|
_, err := db.GetEngine(db.DefaultContext).Exec("UPDATE `action_schedule_spec` SET next = 1")
|
|
require.NoError(t, err)
|
|
|
|
// After running startTasks an ActionRun row is created for the valid scheduled workflow
|
|
require.Empty(t, unittest.GetCount(t, actions_model.ActionRun{WorkflowID: workflowID}))
|
|
require.NoError(t, startTasks(t.Context()))
|
|
require.NotEmpty(t, unittest.GetCount(t, actions_model.ActionRun{WorkflowID: workflowID}))
|
|
|
|
runs, err := db.Find[actions_model.ActionRun](db.DefaultContext, actions_model.FindRunOptions{
|
|
WorkflowID: workflowID,
|
|
})
|
|
require.NoError(t, err)
|
|
require.Len(t, runs, 1)
|
|
run := runs[0]
|
|
|
|
jobs, err := db.Find[actions_model.ActionRunJob](t.Context(), actions_model.FindRunJobOptions{RunID: run.ID})
|
|
require.NoError(t, err)
|
|
require.Len(t, jobs, 1)
|
|
job := jobs[0]
|
|
|
|
// With a matrix that contains ${{ needs ... }} references, the only requirement to work is that when the job is
|
|
// first inserted it is tagged w/ incomplete_matrix
|
|
assert.Contains(t, string(job.WorkflowPayload), "incomplete_matrix: true")
|
|
}
|
|
|
|
func TestServiceActions_RunsOnNeeds(t *testing.T) {
|
|
defer unittest.OverrideFixtures("services/actions/TestServiceActions_startTask")()
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// Load fixtures that are corrupted and create one valid scheduled workflow
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
|
|
|
workflowID := "some.yml"
|
|
schedules := []*actions_model.ActionSchedule{
|
|
{
|
|
Title: "scheduletitle1",
|
|
RepoID: repo.ID,
|
|
OwnerID: repo.OwnerID,
|
|
WorkflowID: workflowID,
|
|
TriggerUserID: repo.OwnerID,
|
|
Ref: "branch",
|
|
CommitSHA: "fakeSHA",
|
|
Event: webhook_module.HookEventSchedule,
|
|
EventPayload: "fakepayload",
|
|
Specs: []string{"* * * * *"},
|
|
Content: []byte(
|
|
`
|
|
jobs:
|
|
job2:
|
|
runs-on: "${{ fromJSON(needs.other-job.outputs.some-output) }}"
|
|
steps:
|
|
- run: true
|
|
`),
|
|
},
|
|
}
|
|
|
|
require.Equal(t, 2, unittest.GetCount(t, actions_model.ActionScheduleSpec{}))
|
|
require.NoError(t, actions_model.CreateScheduleTask(t.Context(), schedules))
|
|
require.Equal(t, 3, unittest.GetCount(t, actions_model.ActionScheduleSpec{}))
|
|
_, err := db.GetEngine(db.DefaultContext).Exec("UPDATE `action_schedule_spec` SET next = 1")
|
|
require.NoError(t, err)
|
|
|
|
// After running startTasks an ActionRun row is created for the valid scheduled workflow
|
|
require.Empty(t, unittest.GetCount(t, actions_model.ActionRun{WorkflowID: workflowID}))
|
|
require.NoError(t, startTasks(t.Context()))
|
|
require.NotEmpty(t, unittest.GetCount(t, actions_model.ActionRun{WorkflowID: workflowID}))
|
|
|
|
runs, err := db.Find[actions_model.ActionRun](db.DefaultContext, actions_model.FindRunOptions{
|
|
WorkflowID: workflowID,
|
|
})
|
|
require.NoError(t, err)
|
|
require.Len(t, runs, 1)
|
|
run := runs[0]
|
|
|
|
jobs, err := db.Find[actions_model.ActionRunJob](t.Context(), actions_model.FindRunJobOptions{RunID: run.ID})
|
|
require.NoError(t, err)
|
|
require.Len(t, jobs, 1)
|
|
job := jobs[0]
|
|
|
|
// With a runs-on that contains ${{ needs ... }} references, the only requirement to work is that when the job is
|
|
// first inserted it is tagged w/ incomplete_runs_on
|
|
assert.Contains(t, string(job.WorkflowPayload), "incomplete_runs_on: true")
|
|
}
|