From 21e025dca084b9a926f15ec64dc5237f723c83a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Chalk?= Date: Wed, 27 Aug 2025 12:41:24 +0200 Subject: [PATCH] ci: fix codecov workflow - projects and targets inferred from nx graph --- .github/workflows/code-coverage.yml | 44 ++++++++++++++++---------- tools/scripts/create-codecov-matrix.js | 25 +++++++++++++++ 2 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 tools/scripts/create-codecov-matrix.js diff --git a/.github/workflows/code-coverage.yml b/.github/workflows/code-coverage.yml index 6770ae0e3..a4a567a16 100644 --- a/.github/workflows/code-coverage.yml +++ b/.github/workflows/code-coverage.yml @@ -9,21 +9,33 @@ env: NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }} jobs: + list-packages: + name: List packages + runs-on: ubuntu-latest + steps: + - name: Checkout the repository + uses: actions/checkout@v4 + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version-file: .nvmrc + cache: npm + - name: Install dependencies + run: npm ci + - name: List packages using Nx CLI + id: list-packages + run: | + matrix=$(node tools/scripts/create-codecov-matrix.js) + echo "matrix=$matrix" >> $GITHUB_OUTPUT + outputs: + matrix: ${{ steps.list-packages.outputs.matrix }} + coverage: + needs: [list-packages] strategy: fail-fast: false - matrix: - lib: - - cli - - core - - models - - utils - - plugin-eslint - - plugin-coverage - - plugin-js-packages - - plugin-lighthouse - scope: [unit, int] - name: Update code coverage + matrix: ${{ fromJson(needs.list-packages.outputs.matrix) }} + name: Collect code coverage runs-on: ubuntu-latest steps: - name: Checkout the repository @@ -35,13 +47,13 @@ jobs: cache: npm - name: Install dependencies run: npm ci - - name: Execute all tests and generate coverage reports - run: npx nx run ${{ matrix.lib }}:${{ matrix.scope }}-test --coverage.enabled + - name: Execute tests with coverage + run: npx nx run ${{ matrix.project }}:${{ matrix.target }} --coverage.enabled - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4 with: - directory: coverage/${{ matrix.lib }}/${{ matrix.scope }}-tests/ + directory: coverage/${{ matrix.project }}/${{ matrix.target }}s/ files: ./lcov.info - flags: ${{ matrix.lib }}-${{ matrix.scope }} + flags: ${{ matrix.project }}-${{ matrix.target }} token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: true diff --git a/tools/scripts/create-codecov-matrix.js b/tools/scripts/create-codecov-matrix.js new file mode 100644 index 000000000..429455215 --- /dev/null +++ b/tools/scripts/create-codecov-matrix.js @@ -0,0 +1,25 @@ +// @ts-check +import { createProjectGraphAsync } from '@nx/devkit'; + +const graph = await createProjectGraphAsync({ + exitOnError: true, + resetDaemonClient: true, +}); + +const projects = Object.values(graph.nodes) + .filter(project => project.data.root === `packages/${project.name}`) + .sort((a, b) => a.name.localeCompare(b.name)); +const targets = ['unit-test', 'int-test']; +const excludes = targets.flatMap(target => + projects + .filter(project => project.data.targets?.[target] == null) + .map(project => ({ project: project.name, target })), +); + +const matrix = { + project: projects.map(project => project.name), + target: targets, + exclude: excludes, +}; + +console.info(JSON.stringify(matrix));