diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index f7ee35a25..fbdf5b81c 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -26,35 +26,96 @@ jobs: contents: read pull-requests: read outputs: - go: ${{ steps.filter.outputs.go }} + go: ${{ github.event_name != 'pull_request' || steps.filter.outputs.full == 'true' || steps.filter.outputs.go == 'true' }} + agent_skill: ${{ github.event_name != 'pull_request' || steps.filter.outputs.full == 'true' || steps.filter.outputs.agent_skill == 'true' }} + licenses: ${{ github.event_name != 'pull_request' || steps.filter.outputs.full == 'true' || steps.filter.outputs.licenses == 'true' }} + gh_credentials: ${{ github.event_name != 'pull_request' || steps.filter.outputs.full == 'true' || steps.filter.outputs.gh_credentials == 'true' }} + provider_update: ${{ github.event_name != 'pull_request' || steps.filter.outputs.full == 'true' || steps.filter.outputs.provider_update == 'true' }} + mode: ${{ github.event_name == 'pull_request' && 'pr' || 'full' }} + changed_files: ${{ steps.filter.outputs.all_files || '[]' }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4 id: filter + if: github.event_name == 'pull_request' with: + list-files: json filters: | - go: - - '**/*.go' - - 'pkg/inject/**' + full: + - '.github/workflows/pr-ci.yml' + - '.github/actions/**' + - 'hack/ci/**' - 'go.mod' - 'go.sum' - '.goreleaser.yml' + - 'e2e/framework/**' + - 'e2e/cmd/**' + - 'e2e/*.go' + go: + - '**/*.go' + - 'cmd/**' + - 'pkg/**' + - 'providers/**' - 'e2e/**' - - 'hack/ci/**' - 'hack/microsandbox/**' - 'hack/provider_update_ci/**' - - '.github/workflows/pr-ci.yml' - 'hack/licenses/**' - 'hack/podman_ci/**' + - '.golangci.yaml' + agent_skill: + - '.agents/skills/devsy/**' + - 'hack/validate_agent_skill/**' + licenses: + - '**/*.go' + - 'hack/licenses/**' - 'THIRD_PARTY_LICENSES.md' + gh_credentials: + - 'pkg/ghcredentials/**' + - 'pkg/credentials/**' + - 'pkg/tunnel/**' + - 'pkg/agent/tunnel/**' + - 'pkg/agent/tunnelserver/**' + - 'pkg/config/**' + - 'pkg/log/**' + - 'pkg/http/**' + - 'pkg/port/**' + - 'pkg/netstat/**' + - 'pkg/random/**' + - 'pkg/gitsshsigning/**' + - 'pkg/transport/**' + - 'pkg/client/**' + - 'pkg/ssh/**' + - 'pkg/flags/**' + - 'pkg/exitcode/**' + - 'e2e/cmd/ghfixture/**' + provider_update: + - 'pkg/workspace/**' + - 'pkg/provider/**' + - 'pkg/config/**' + - 'pkg/extract/**' + - 'pkg/copy/**' + - 'pkg/download/**' + - 'pkg/http/**' + - 'pkg/gitcredentials/**' + - 'pkg/hash/**' + - 'pkg/log/**' + - 'pkg/id/**' + - 'pkg/types/**' + - 'providers/**' + - 'hack/provider_update_ci/**' + all: + - '**' plan: name: Plan + needs: [changes] + if: always() runs-on: ubuntu-latest permissions: contents: read outputs: unit: ${{ steps.matrix.outputs.unit }} + build: ${{ steps.matrix.outputs.build }} unprivileged: ${{ steps.matrix.outputs.unprivileged }} integration: ${{ steps.matrix.outputs.integration }} microsandbox: ${{ steps.matrix.outputs.microsandbox }} @@ -71,7 +132,11 @@ jobs: id: matrix env: SECRET_AVAILABLE: ${{ secrets.DEVSY_GITHUB_APP_ID != '' }} - run: go run ./hack/ci matrix --secret-available="$SECRET_AVAILABLE" >> "$GITHUB_OUTPUT" + MATRIX_MODE: ${{ needs.changes.outputs.mode }} + CHANGED_FILES_JSON: ${{ needs.changes.outputs.changed_files }} + run: | + printf '%s' "$CHANGED_FILES_JSON" | jq -r '.[]' > "$RUNNER_TEMP/changed-files.txt" + go run ./hack/ci matrix --mode="$MATRIX_MODE" --changed-files="$RUNNER_TEMP/changed-files.txt" --secret-available="$SECRET_AVAILABLE" >> "$GITHUB_OUTPUT" precommit: name: Pre-commit @@ -81,10 +146,12 @@ jobs: - uses: jdx/mise-action@b44d12e7d0d8ee7461bb893e1b1993500433185b # v5.2.7 - - run: prek run --all-files + - run: SKIP=golangci-lint prek run --all-files agent-skill: name: Agent Skill + needs: [changes] + if: needs.changes.outputs.agent_skill == 'true' runs-on: ubuntu-latest timeout-minutes: 10 permissions: @@ -106,6 +173,8 @@ jobs: lint: name: Lint + needs: [changes] + if: needs.changes.outputs.go == 'true' runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 @@ -128,7 +197,7 @@ jobs: github-credential-host-tests: name: GitHub Credential Host Tests (${{ matrix.runner }}) needs: [changes] - if: needs.changes.outputs.go == 'true' + if: needs.changes.outputs.gh_credentials == 'true' strategy: fail-fast: false matrix: @@ -193,7 +262,7 @@ jobs: provider-update-windows: name: Provider Update Tests (windows-latest) needs: [changes] - if: needs.changes.outputs.go == 'true' + if: needs.changes.outputs.provider_update == 'true' runs-on: windows-latest # Allow cold Go compilation and the post-job cache save to complete. timeout-minutes: 30 @@ -221,17 +290,11 @@ jobs: build-cli: name: Build CLI Binary on ${{ matrix.runner }} - needs: [changes, precommit, lint] - if: needs.changes.outputs.go == 'true' + needs: [changes, plan] + if: needs.changes.outputs.go == 'true' && needs.plan.outputs.build != '{"include":[]}' strategy: - matrix: - include: - - runner: ubuntu-latest - job_timeout_minutes: 45 - - runner: macos-latest - - runner: windows-latest - # Cold builds need room to save the Go cache after artifact uploads. - job_timeout_minutes: 45 + fail-fast: false + matrix: ${{ fromJSON(needs.plan.outputs.build) }} runs-on: ${{ matrix.runner }} timeout-minutes: ${{ matrix.job_timeout_minutes || 20 }} steps: @@ -318,24 +381,24 @@ jobs: path: dist/devsy-${{ steps.os.outputs.runner_os }}_*/devsy-${{ steps.os.outputs.runner_os }}-* - name: build MicroSandbox CI helper - if: runner.os == 'Linux' + if: runner.os == 'Linux' && matrix.build_microsandbox env: CGO_ENABLED: 0 run: go build -trimpath -o "${RUNNER_TEMP}/devsy-ci-microsandbox" ./hack/microsandbox - name: build pinned runtime performance tools - if: runner.os == 'Linux' + if: runner.os == 'Linux' && matrix.build_microsandbox run: '"${RUNNER_TEMP}/devsy-ci-microsandbox" build-performance-tools --output-dir "${RUNNER_TEMP}/runtime-performance-tools"' - name: upload runtime performance tools - if: runner.os == 'Linux' + if: runner.os == 'Linux' && matrix.build_microsandbox uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 with: name: runtime-performance-tools-linux path: ${{ runner.temp }}/runtime-performance-tools/ - name: upload MicroSandbox CI helper - if: runner.os == 'Linux' + if: runner.os == 'Linux' && matrix.build_microsandbox uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 with: name: devsy-ci-microsandbox-linux @@ -380,8 +443,8 @@ jobs: licenses: name: Third-party licenses - needs: [changes, precommit, lint] - if: needs.changes.outputs.go == 'true' + needs: [changes] + if: needs.changes.outputs.licenses == 'true' runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 @@ -397,7 +460,7 @@ jobs: integration-tests-unprivileged: name: ${{ matrix.title }} needs: [changes, plan, build-cli] - if: needs.changes.outputs.go == 'true' + if: needs.changes.outputs.go == 'true' && needs.plan.outputs.unprivileged != '{"include":[]}' strategy: fail-fast: false matrix: ${{ fromJSON(needs.plan.outputs.unprivileged) }} @@ -446,8 +509,8 @@ jobs: prepare-microsandbox-provider: name: Prepare MicroSandbox provider assets - needs: [changes, build-cli] - if: needs.changes.outputs.go == 'true' + needs: [changes, plan, build-cli] + if: needs.changes.outputs.go == 'true' && needs.plan.outputs.microsandbox != '{"include":[]}' runs-on: ubuntu-latest timeout-minutes: 10 steps: @@ -496,7 +559,7 @@ jobs: integration-tests-microsandbox: name: ${{ matrix.title }} needs: [changes, plan, build-cli, prepare-microsandbox-provider] - if: needs.changes.outputs.go == 'true' + if: needs.changes.outputs.go == 'true' && needs.plan.outputs.microsandbox != '{"include":[]}' strategy: fail-fast: false matrix: ${{ fromJSON(needs.plan.outputs.microsandbox) }} @@ -621,7 +684,7 @@ jobs: integration-tests: name: ${{ matrix.title }} needs: [changes, plan, build-cli] - if: needs.changes.outputs.go == 'true' + if: needs.changes.outputs.go == 'true' && needs.plan.outputs.integration != '{"include":[]}' strategy: fail-fast: false matrix: ${{ fromJSON(needs.plan.outputs.integration) }} @@ -933,9 +996,7 @@ jobs: with: go-version-file: go.mod - # Jobs gated on Go changes may be skipped only when no Go-relevant file - # changed; the jobs listed in --always-run must never be skipped. - name: Check required jobs env: NEEDS_JSON: ${{ toJSON(needs) }} - run: go run ./hack/ci success --always-run changes,plan,precommit,lint,agent-skill + run: go run ./hack/ci success --always-run changes,plan,precommit diff --git a/hack/ci/README.md b/hack/ci/README.md index a60e248e6..a7fb7a418 100644 --- a/hack/ci/README.md +++ b/hack/ci/README.md @@ -19,6 +19,32 @@ and subcommands here decide which tests exist, where they run, and how. runs it on each runner in `unit.runners`. The workflow builds one job matrix per e2e group from `e2e.suites`. +## Change selection + +Pushes to main and manual workflow runs use `--mode=full`. They run every +suite whose required secret is available and build Linux, macOS, and Windows +binaries. + +Pull requests use `--mode=pr --changed-files FILE`. The file contains one +repository-relative path per line. Paths in `selection.full_run_paths` select +every suite. Paths in `selection.core_paths` select the 13 suites marked +`pr_smoke` plus suites with a matching `paths` entry. Other changes select +only suites with a matching entry. Patterns support `*` within one path +segment and `**` across zero or more segments. + +Changes under `e2e/cmd/` are shared CI dependencies and select the full matrix. + +The build matrix follows selected suites. Every E2E run needs the Linux +binary, including Windows runs that inject it into containers. Windows +suites also need a Windows build. MicroSandbox suites enable the provider +build on Linux. Full-run paths retain all three build runners. Unmatched +changes produce empty E2E and build matrices. Unit test runners are unchanged. + +`ci success` accepts conditional skips only when successful change detection +and planning provide valid outputs that explain them. Missing or failed +planning, invalid outputs, and unexpectedly skipped required jobs fail the +gate. + ## Adding tests - **Go unit test in an ordinary package**: no change. The `packages` step finds @@ -31,10 +57,16 @@ e2e group from `e2e.suites`. - **Ginkgo spec**: add its label to `e2e.suites` and choose a `group`. Set only the fields that differ from the group defaults, such as `runner`, `test_timeout`, `install_kind`, `install_podman`, `requires_secret`, and - `host_setup`. List labels that PR CI must not select directly in + `host_setup`. Add `paths` for the suite's test files and product dependencies. + Set `pr_smoke` only for the shared core smoke tier. List labels that PR CI + must not select directly in `e2e.ungated` with a reason. These include sub-labels exercised by gated suites and labels run manually or by another workflow. +Keep suite paths aligned with implementation packages when code moves. +Include runtime helpers such as `pkg/dockerinstall/**`, not only provider wrappers. +Include direct compile dependencies in native host job filters. + `go test ./hack/ci` fails when a Ginkgo label or Go test package has no tier, or when the manifest names a label that no longer exists. New tests need no workflow edits. @@ -43,7 +75,7 @@ workflow edits. | Command | Purpose | | --- | --- | -| `ci matrix [--group G] [--secret-available]` | Print the job matrices as GitHub outputs. Drops suites that need the GitHub App secret when it is unavailable. | +| `ci matrix [--group G] [--secret-available] [--mode full\|pr] [--changed-files FILE]` | Print unit, build, and E2E job matrices as GitHub outputs. Apply change selection and secret availability. | | `ci unit [--os GOOS] [--step NAME] [--list] [--coverprofile P]` | Run the unit tier on this machine. | | `ci e2e prepare --bin-src DIR --e2e-dir DIR` | Put the CLI binaries next to `e2e.test`. | | `ci e2e run --label L --runner R` | Run a suite, with its focused checks, sudo and environment setup, and post checks. | diff --git a/hack/ci/coverage_test.go b/hack/ci/coverage_test.go index 006998233..a717f3ec4 100644 --- a/hack/ci/coverage_test.go +++ b/hack/ci/coverage_test.go @@ -7,6 +7,7 @@ import ( "io/fs" "os" "os/exec" + "path" "path/filepath" "regexp" "slices" @@ -148,6 +149,37 @@ func (s *coverageSuite) TestUnitEntriesExistOnDisk() { } } +func (s *coverageSuite) TestEverySuiteHasSelectionPaths() { + s.NotEmpty(s.manifest.Selection.FullRunPaths) + s.NotEmpty(s.manifest.Selection.CorePaths) + patterns := append([]string{}, s.manifest.Selection.FullRunPaths...) + patterns = append(patterns, s.manifest.Selection.CorePaths...) + for _, selected := range s.manifest.E2E.Suites { + s.NotEmpty(selected.Paths, "%s/%s needs selection paths", selected.Runner, selected.Label) + patterns = append(patterns, selected.Paths...) + } + for _, pattern := range patterns { + normalized := normalizeSelectionPath(pattern) + prefix := normalized + wildcard := strings.IndexAny(normalized, "*?[") + if wildcard >= 0 { + prefix = path.Dir(normalized[:wildcard]) + } + info, err := os.Stat(filepath.Join(s.root, filepath.FromSlash(prefix))) + if !s.NoError(err, "selection pattern %q has missing static prefix %q", pattern, prefix) { + continue + } + if wildcard >= 0 { + s.True( + info.IsDir(), + "selection pattern %q has a non-directory static prefix %q", + pattern, + prefix, + ) + } + } +} + func (s *coverageSuite) assignedLabels() map[string]bool { assigned := map[string]bool{} for _, label := range s.manifestLabels() { diff --git a/hack/ci/manifest.go b/hack/ci/manifest.go index 3402b2ab1..ddbb495d1 100644 --- a/hack/ci/manifest.go +++ b/hack/ci/manifest.go @@ -38,12 +38,19 @@ var ( errDuplicateSuite = errors.New("duplicate suite") errInvalidSuite = errors.New("invalid suite") errInvalidStep = errors.New("invalid unit step") + errInvalidGlob = errors.New("invalid selection glob") ) // Manifest is the parsed hack/ci/suites.yaml. type Manifest struct { - Unit UnitTier `yaml:"unit"` - E2E E2ETier `yaml:"e2e"` + Unit UnitTier `yaml:"unit"` + E2E E2ETier `yaml:"e2e"` + Selection SelectionConfig `yaml:"selection"` +} + +type SelectionConfig struct { + FullRunPaths []string `yaml:"full_run_paths"` + CorePaths []string `yaml:"core_paths"` } // UnitTier describes how Go unit tests run. @@ -114,9 +121,11 @@ type FocusedCheck struct { // Suite is one Ginkgo label executed as one CI job. After Resolve, every // inherited field is populated. type Suite struct { - Group string `yaml:"group"` - Label string `yaml:"label"` - Runner string `yaml:"runner"` + Group string `yaml:"group"` + Label string `yaml:"label"` + Runner string `yaml:"runner"` + PRSmoke bool `yaml:"pr_smoke"` + Paths []string `yaml:"paths"` // Privileged selects sudo for the e2e binary on Linux. Nil inherits the group. Privileged *bool `yaml:"privileged"` TestTimeout string `yaml:"test_timeout"` @@ -220,6 +229,9 @@ func (s Suite) validate() error { case s.requiresSpecCount() && s.ExpectedSpecCount <= 0: return fmt.Errorf("%w: %s needs expected_spec_count", errInvalidSuite, s.Label) } + if err := validateGlobs(s.Paths); err != nil { + return fmt.Errorf("%w: %s paths: %w", errInvalidSuite, s.Label, err) + } if err := s.validateFixtures(); err != nil { return err } @@ -312,6 +324,12 @@ func (m *Manifest) resolve() error { } func (m *Manifest) validate() error { + if err := validateGlobs(m.Selection.FullRunPaths); err != nil { + return fmt.Errorf("full_run_paths: %w", err) + } + if err := validateGlobs(m.Selection.CorePaths); err != nil { + return fmt.Errorf("core_paths: %w", err) + } seen := map[string]bool{} for _, s := range m.E2E.Suites { key := s.Runner + "/" + s.Label diff --git a/hack/ci/manifest_test.go b/hack/ci/manifest_test.go index ce9f3f27d..19f2ac8ba 100644 --- a/hack/ci/manifest_test.go +++ b/hack/ci/manifest_test.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "testing" "github.com/stretchr/testify/suite" @@ -200,6 +201,40 @@ func (s *manifestSuite) TestParseRejectsUnknownKey() { s.ErrorContains(err, "flake_attempt") } +func (s *manifestSuite) TestSelectionGlobValidation() { + const pathsField = "paths" + for _, pattern := range []string{"", " ", "[", "pkg/[", "pkg/[/../file.go", "/pkg/**", "../pkg/**"} { + for _, field := range []string{"full_run_paths", "core_paths", pathsField} { + s.Run(field+"/"+pattern, func() { + data := testGroupsYAML + " - { group: integration, label: x }\n" + if field == pathsField { + data = testGroupsYAML + fmt.Sprintf( + " - { group: integration, label: x, paths: [%q] }\n", pattern, + ) + } else { + data += fmt.Sprintf("selection:\n %s: [%q]\n", field, pattern) + } + _, err := ParseManifest([]byte(data)) + s.ErrorIs(err, errInvalidGlob) + if field == pathsField { + s.ErrorIs(err, errInvalidSuite) + } + }) + } + } +} + +func (s *manifestSuite) TestSyntheticManifestNeedsNoSelectionRules() { + m, err := ParseManifest([]byte(testGroupsYAML + " - { group: integration, label: x }\n")) + s.Require().NoError(err) + s.Equal( + []string{"x"}, + selectionLabels( + selectSuites(m, groupIntegration, selectionOptions{mode: selectionModeFull}), + ), + ) +} + // Footgun: RunsOn disagrees with the os list, so a step runs on a platform it // was meant to skip or is skipped on its own platform. func (s *manifestSuite) TestUnitStepRunsOn() { diff --git a/hack/ci/matrix.go b/hack/ci/matrix.go index 22eb566a4..0a35f6ce5 100644 --- a/hack/ci/matrix.go +++ b/hack/ci/matrix.go @@ -7,21 +7,32 @@ import ( "flag" "fmt" "io" + "os" "slices" "strings" ) -const matrixUnitGroup = "unit" +const ( + matrixUnitGroup = "unit" + matrixBuildGroup = "build" + matrixUbuntuRunner = "ubuntu-latest" + matrixMacOSRunner = "macos-latest" + matrixWindowsRunner = "windows-latest" +) // matrixGroupNames lists the job groups in the order `ci matrix` prints them. var matrixGroupNames = []string{ matrixUnitGroup, + matrixBuildGroup, groupUnprivileged, groupIntegration, groupMicrosandbox, } -var errUnknownMatrixGroup = errors.New("unknown matrix group") +var ( + errUnknownMatrixGroup = errors.New("unknown matrix group") + errUnknownMatrixMode = errors.New("unknown matrix mode") +) // matrixDoc is the GitHub Actions matrix object. type matrixDoc struct { @@ -34,6 +45,12 @@ type matrixUnitEntry struct { OS string `json:"os"` } +type matrixBuildEntry struct { + Runner string `json:"runner"` + JobTimeoutMinutes int `json:"job_timeout_minutes,omitempty"` + BuildMicrosandbox bool `json:"build_microsandbox"` +} + // matrixEntry is one e2e suite as a job of the workflow matrix. type matrixEntry struct { Name string `json:"name"` @@ -63,19 +80,47 @@ type matrixEntry struct { // matrixCLI prints the job matrices for the PR workflow. func matrixCLI(args []string, stdout, stderr io.Writer) int { + group, opts, code := parseMatrixOptions(args, stderr) + if code >= 0 { + return code + } + m, err := LoadManifest() + if err != nil { + _, _ = fmt.Fprintf(stderr, "ci matrix: %v\n", err) + return 1 + } + out, err := matrixOutput(m, group, opts) + if err != nil { + _, _ = fmt.Fprintf(stderr, "ci matrix: %v\n", err) + return matrixFailureCode(err) + } + if _, err := io.WriteString(stdout, out); err != nil { + _, _ = fmt.Fprintf(stderr, "ci matrix: write output: %v\n", err) + return 1 + } + return 0 +} + +func parseMatrixOptions(args []string, stderr io.Writer) (string, selectionOptions, int) { fs := flag.NewFlagSet("matrix", flag.ContinueOnError) fs.SetOutput(stderr) group := fs.String( "group", "", - "print only this group: unit, unprivileged, integration or microsandbox", + "print only this group: unit, build, unprivileged, integration or microsandbox", ) secretAvailable := fs.Bool("secret-available", false, "include suites that require a secret") + mode := fs.String("mode", selectionModeFull, "suite selection mode: full or pr") + changedFilesPath := fs.String( + "changed-files", + "", + "file containing newline-separated changed paths", + ) if err := fs.Parse(args); err != nil { if errors.Is(err, flag.ErrHelp) { - return 0 + return "", selectionOptions{}, 0 } - return 2 + return "", selectionOptions{}, 2 } if fs.NArg() > 0 { _, _ = fmt.Fprintf( @@ -83,23 +128,40 @@ func matrixCLI(args []string, stdout, stderr io.Writer) int { "ci matrix: unexpected arguments: %s\n", strings.Join(fs.Args(), " "), ) - return 2 + return "", selectionOptions{}, 2 } - m, err := LoadManifest() + if *mode != selectionModeFull && *mode != selectionModePR { + _, _ = fmt.Fprintf(stderr, "ci matrix: %v: %q\n", errUnknownMatrixMode, *mode) + return "", selectionOptions{}, 2 + } + changedFiles, err := readMatrixChangedFiles(*changedFilesPath) if err != nil { - _, _ = fmt.Fprintf(stderr, "ci matrix: %v\n", err) - return 1 + _, _ = fmt.Fprintf(stderr, "ci matrix: read changed files: %v\n", err) + return "", selectionOptions{}, 1 } - out, err := matrixOutput(m, *group, *secretAvailable) + return *group, selectionOptions{ + secretAvailable: *secretAvailable, + mode: *mode, + changedFiles: changedFiles, + }, -1 +} + +func readMatrixChangedFiles(path string) ([]string, error) { + if path == "" { + return nil, nil + } + data, err := os.ReadFile(path) if err != nil { - _, _ = fmt.Fprintf(stderr, "ci matrix: %v\n", err) - return matrixFailureCode(err) + return nil, err } - if _, err := io.WriteString(stdout, out); err != nil { - _, _ = fmt.Fprintf(stderr, "ci matrix: write output: %v\n", err) - return 1 + var changedFiles []string + for line := range strings.SplitSeq(string(data), "\n") { + line = strings.TrimSuffix(line, "\r") + if line != "" { + changedFiles = append(changedFiles, line) + } } - return 0 + return changedFiles, nil } // matrixFailureCode maps a matrixOutput error to an exit code. @@ -112,9 +174,9 @@ func matrixFailureCode(err error) int { // matrixOutput renders the --group JSON line, or without a group one // `name=` line per group, each terminated by a newline. -func matrixOutput(m *Manifest, group string, secretAvailable bool) (string, error) { +func matrixOutput(m *Manifest, group string, opts selectionOptions) (string, error) { if group != "" { - line, err := matrixGroupJSON(m, group, secretAvailable) + line, err := matrixGroupJSON(m, group, opts) if err != nil { return "", err } @@ -122,7 +184,7 @@ func matrixOutput(m *Manifest, group string, secretAvailable bool) (string, erro } lines := make([]string, 0, len(matrixGroupNames)) for _, name := range matrixGroupNames { - line, err := matrixGroupJSON(m, name, secretAvailable) + line, err := matrixGroupJSON(m, name, opts) if err != nil { return "", err } @@ -132,8 +194,8 @@ func matrixOutput(m *Manifest, group string, secretAvailable bool) (string, erro } // matrixGroupJSON returns the compact single-line matrix object of a group. -func matrixGroupJSON(m *Manifest, group string, secretAvailable bool) (string, error) { - include, err := matrixInclude(m, group, secretAvailable) +func matrixGroupJSON(m *Manifest, group string, opts selectionOptions) (string, error) { + include, err := matrixInclude(m, group, opts) if err != nil { return "", err } @@ -148,12 +210,14 @@ func matrixGroupJSON(m *Manifest, group string, secretAvailable bool) (string, e // matrixInclude returns the entries of one group. Entry slices are never nil // so an empty group encodes as [] rather than null. -func matrixInclude(m *Manifest, group string, secretAvailable bool) (any, error) { +func matrixInclude(m *Manifest, group string, opts selectionOptions) (any, error) { switch group { case matrixUnitGroup: return unitMatrixEntries(m), nil + case matrixBuildGroup: + return buildMatrixEntries(m, opts), nil case groupUnprivileged, groupIntegration, groupMicrosandbox: - return e2eMatrixEntries(m, group, secretAvailable), nil + return e2eMatrixEntries(m, group, opts), nil default: return nil, fmt.Errorf("%w: %q", errUnknownMatrixGroup, group) } @@ -167,18 +231,57 @@ func unitMatrixEntries(m *Manifest) []matrixUnitEntry { return entries } -func e2eMatrixEntries(m *Manifest, group string, secretAvailable bool) []matrixEntry { - suites := m.SuitesIn(group) +func e2eMatrixEntries(m *Manifest, group string, opts selectionOptions) []matrixEntry { + suites := selectSuites(m, group, opts) entries := make([]matrixEntry, 0, len(suites)) for _, s := range suites { - if s.RequiresSecret && !secretAvailable { - continue - } entries = append(entries, newMatrixEntry(s)) } return entries } +func buildMatrixEntries(m *Manifest, opts selectionOptions) []matrixBuildEntry { + if opts.mode == "" || opts.mode == selectionModeFull { + return []matrixBuildEntry{ + {Runner: matrixUbuntuRunner, JobTimeoutMinutes: 45, BuildMicrosandbox: true}, + {Runner: matrixMacOSRunner, JobTimeoutMinutes: 20}, + {Runner: matrixWindowsRunner, JobTimeoutMinutes: 45}, + } + } + if matchesAny(opts.changedFiles, m.Selection.FullRunPaths) || + slices.Contains(opts.changedFiles, ".goreleaser.yml") { + return []matrixBuildEntry{ + {Runner: matrixUbuntuRunner, JobTimeoutMinutes: 45, BuildMicrosandbox: true}, + {Runner: matrixMacOSRunner, JobTimeoutMinutes: 20, BuildMicrosandbox: true}, + {Runner: matrixWindowsRunner, JobTimeoutMinutes: 45, BuildMicrosandbox: true}, + } + } + unprivileged := selectSuites(m, groupUnprivileged, opts) + integration := selectSuites(m, groupIntegration, opts) + microsandbox := selectSuites(m, groupMicrosandbox, opts) + entries := make([]matrixBuildEntry, 0, 2) + if len(unprivileged)+len(integration)+len(microsandbox) > 0 { + entries = append( + entries, + matrixBuildEntry{ + Runner: matrixUbuntuRunner, + JobTimeoutMinutes: 45, + BuildMicrosandbox: len(microsandbox) > 0, + }, + ) + } + for _, s := range integration { + if s.OS() == osWindows { + entries = append( + entries, + matrixBuildEntry{Runner: matrixWindowsRunner, JobTimeoutMinutes: 45}, + ) + break + } + } + return entries +} + // newMatrixEntry maps a resolved suite to its matrix job, including the setup // steps the workflow must run for it. func newMatrixEntry(s Suite) matrixEntry { diff --git a/hack/ci/matrix_test.go b/hack/ci/matrix_test.go index 26431b4de..dd7886298 100644 --- a/hack/ci/matrix_test.go +++ b/hack/ci/matrix_test.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding/json" "fmt" + "os" + "path/filepath" "strings" "testing" @@ -45,9 +47,7 @@ e2e: const ( matrixProviderLabel = "provider" - matrixUbuntuRunner = "ubuntu-latest" matrixPodmanRunner = "ubuntu-24.04" - matrixWindowsRunner = "windows-latest" matrixWindowsTimeout = "900s" ) @@ -218,9 +218,17 @@ func (s *matrixSuite) TestPlainSuiteHasNoRuntime() { func (s *matrixSuite) TestSecretSuitesOmittedUnlessAvailable() { m := s.testManifest() - withoutSecret, err := matrixOutput(m, groupIntegration, false) + withoutSecret, err := matrixOutput( + m, + groupIntegration, + selectionOptions{mode: selectionModeFull}, + ) s.Require().NoError(err) - withSecret, err := matrixOutput(m, groupIntegration, true) + withSecret, err := matrixOutput( + m, + groupIntegration, + selectionOptions{secretAvailable: true, mode: selectionModeFull}, + ) s.Require().NoError(err) names := func(line string) []string { @@ -249,21 +257,25 @@ func (s *matrixSuite) TestGroupFlagPrintsOnlyThatGroup() { s.Equal("context", entries[0].Name) } -func (s *matrixSuite) TestNoGroupPrintsFourLinesInOrder() { +func (s *matrixSuite) TestNoGroupPrintsFiveLinesInOrder() { code, stdout, stderr := s.runCLI() s.Equal(0, code, stderr) s.Empty(stderr) lines := strings.Split(strings.TrimSuffix(stdout, "\n"), "\n") - s.Require().Len(lines, 4) - for i, name := range []string{"unit", groupUnprivileged, groupIntegration, groupMicrosandbox} { + s.Require().Len(lines, 5) + for i, name := range []string{"unit", matrixBuildGroup, groupUnprivileged, groupIntegration, groupMicrosandbox} { s.True(strings.HasPrefix(lines[i], name+`={"include":[`), lines[i]) s.True(json.Valid([]byte(strings.TrimPrefix(lines[i], name+"="))), lines[i]) } } func (s *matrixSuite) TestUnitEntriesCarryOS() { - out, err := matrixOutput(s.testManifest(), matrixUnitGroup, false) + out, err := matrixOutput( + s.testManifest(), + matrixUnitGroup, + selectionOptions{mode: selectionModeFull}, + ) s.Require().NoError(err) s.Equal(`{"include":[`+ @@ -273,7 +285,11 @@ func (s *matrixSuite) TestUnitEntriesCarryOS() { func (s *matrixSuite) TestCompactJSONDoesNotEscapeHTML() { m := s.testManifest() - line, err := matrixGroupJSON(m, groupIntegration, true) + line, err := matrixGroupJSON( + m, + groupIntegration, + selectionOptions{secretAvailable: true, mode: selectionModeFull}, + ) s.Require().NoError(err) s.Contains(line, `"name":"a