feat(actions): support referencing ${{ needs... }} variables in strategy.matrix (#10244)

https://code.forgejo.org/forgejo/forgejo-actions-feature-requests/issues/71 requires partial implementation in runner, and partial in Forgejo; this is the Forgejo implementation.

Allows for the definition of dynamic job matrixes in Forgejo Actions, where an earlier job provides and output that is used in `strategy.matrix` for a later job that requires it.  For example, adapted from the GitHub Actions example for this feature:

```yaml
name: shared matrix
on:
  push:
  workflow_dispatch:

jobs:
  define-matrix:
    runs-on: docker

    outputs:
      colors: ${{ steps.colors.outputs.colors }}

    steps:
      - name: Define Colors
        id: colors
        run: |
          echo 'colors=["red", "green", "blue"]' >> "$GITHUB_OUTPUT"

  produce-artifacts:
    runs-on: docker
    needs: define-matrix
    strategy:
      matrix:
        color: ${{ fromJSON(needs.define-matrix.outputs.colors) }}

    steps:
      - name: Define Color
        env:
          color: ${{ matrix.color }}
        run: |
          echo "$color" > color
      - name: Produce Artifact
        uses: https://data.forgejo.org/forgejo/upload-artifact@v4
        with:
          name: ${{ matrix.color }}
          path: color

  consume-artifacts:
    runs-on: docker
    needs:
    - define-matrix
    - produce-artifacts
    strategy:
      matrix:
        color: ${{ fromJSON(needs.define-matrix.outputs.colors) }}

    steps:
    - name: Retrieve Artifact
      uses: https://data.forgejo.org/forgejo/download-artifact@v4
      with:
        name: ${{ matrix.color }}

    - name: Report Color
      run: |
        cat color
```

## 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.
  - [x] 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

- [x] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
    - https://codeberg.org/forgejo/docs/pulls/1607
- [ ] 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/10244): <!--number 10244 --><!--line 0 --><!--description ZmVhdChhY3Rpb25zKTogc3VwcG9ydCByZWZlcmVuY2luZyAke3sgbmVlZHMuLi4gfX0gdmFyaWFibGVzIGluIGBzdHJhdGVneS5tYXRyaXhg-->feat(actions): support referencing ${{ needs... }} variables in `strategy.matrix`<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10244
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>
This commit is contained in:
Mathieu Fenniak 2025-11-29 17:49:04 +01:00 committed by Mathieu Fenniak
parent 20dd01908c
commit 482ba3a4e5
17 changed files with 1084 additions and 7 deletions

View file

@ -313,6 +313,15 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
clearRepoRunCountCache(ctx, run.Repo)
if err := InsertRunJobs(ctx, run, jobs); err != nil {
return err
}
return commiter.Commit()
}
// Adds `ActionRunJob` instances from `SingleWorkflows` to an existing ActionRun.
func InsertRunJobs(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWorkflow) error {
runJobs := make([]*ActionRunJob, 0, len(jobs))
var hasWaiting bool
for _, v := range jobs {
@ -329,7 +338,7 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
}
payload, _ = v.Marshal()
if len(needs) > 0 || run.NeedApproval {
if len(needs) > 0 || run.NeedApproval || v.IncompleteMatrix {
status = StatusBlocked
} else {
status = StatusWaiting
@ -352,6 +361,7 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
Status: status,
})
}
if len(runJobs) > 0 {
if err := db.Insert(ctx, runJobs); err != nil {
return err
@ -365,7 +375,7 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
}
}
return commiter.Commit()
return nil
}
func GetLatestRun(ctx context.Context, repoID int64) (*ActionRun, error) {

View file

@ -17,6 +17,8 @@ import (
"forgejo.org/modules/translation"
"forgejo.org/modules/util"
"code.forgejo.org/forgejo/runner/v12/act/jobparser"
"go.yaml.in/yaml/v3"
"xorm.io/builder"
)
@ -253,3 +255,14 @@ func (job *ActionRunJob) StatusDiagnostics(lang translation.Locale) []template.H
return diagnostics
}
// Checks whether the target job is an `(incomplete matrix)` job that will be blocked until the matrix is complete, and
// then regenerated and deleted.
func (job *ActionRunJob) IsIncompleteMatrix() (bool, error) {
var jobWorkflow jobparser.SingleWorkflow
err := yaml.Unmarshal(job.WorkflowPayload, &jobWorkflow)
if err != nil {
return false, fmt.Errorf("failure unmarshaling WorkflowPayload to SingleWorkflow: %w", err)
}
return jobWorkflow.IncompleteMatrix, nil
}

View file

@ -148,3 +148,40 @@ func TestActionRunJob_StatusDiagnostics(t *testing.T) {
})
}
}
func TestActionRunJob_IsIncompleteMatrix(t *testing.T) {
tests := []struct {
name string
job ActionRunJob
isIncomplete bool
errContains string
}{
{
name: "normal workflow",
job: ActionRunJob{WorkflowPayload: []byte("name: workflow")},
isIncomplete: false,
},
{
name: "incomplete_matrix workflow",
job: ActionRunJob{WorkflowPayload: []byte("name: workflow\nincomplete_matrix: true")},
isIncomplete: true,
},
{
name: "unparseable workflow",
job: ActionRunJob{WorkflowPayload: []byte("name: []\nincomplete_matrix: true")},
errContains: "failure unmarshaling WorkflowPayload to SingleWorkflow: yaml: unmarshal errors",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isIncomplete, err := tt.job.IsIncompleteMatrix()
if tt.errContains != "" {
assert.ErrorContains(t, err, tt.errContains)
} else {
require.NoError(t, err)
assert.Equal(t, tt.isIncomplete, isIncomplete)
}
})
}
}

View file

@ -12,6 +12,7 @@ import (
"forgejo.org/models/unittest"
"forgejo.org/modules/cache"
"code.forgejo.org/forgejo/runner/v12/act/jobparser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -200,3 +201,40 @@ func TestActionRun_NeedApproval(t *testing.T) {
assertApprovalEqual(t, runNeedApproval, runs[0])
})
}
func TestActionRun_IncompleteMatrix(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
pullRequestPosterID := int64(4)
repoID := int64(10)
pullRequestID := int64(2)
runDoesNotNeedApproval := &ActionRun{
RepoID: repoID,
PullRequestID: pullRequestID,
PullRequestPosterID: pullRequestPosterID,
}
workflowRaw := []byte(`
jobs:
job2:
runs-on: ubuntu-latest
strategy:
matrix:
dim1: "${{ fromJSON(needs.other-job.outputs.some-output) }}"
steps:
- run: true
`)
workflows, err := jobparser.Parse(workflowRaw, false, jobparser.WithJobOutputs(map[string]map[string]string{}))
require.NoError(t, err)
require.True(t, workflows[0].IncompleteMatrix) // must be set for this test scenario to be valid
require.NoError(t, InsertRun(t.Context(), runDoesNotNeedApproval, workflows))
jobs, err := db.Find[ActionRunJob](t.Context(), FindRunJobOptions{RunID: runDoesNotNeedApproval.ID})
require.NoError(t, err)
require.Len(t, jobs, 1)
job := jobs[0]
// Expect job with an incomplete matrix to be StatusBlocked:
assert.Equal(t, StatusBlocked, job.Status)
}