ci: fix codecov workflow - projects and targets inferred from nx graph #491
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Code Coverage | |
| # TODO: revert | |
| on: push | |
| # on: | |
| # push: | |
| # branches: [main] | |
| env: | |
| NX_NON_NATIVE_HASHER: true | |
| 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: project | |
| run: | | |
| packages=$(npx nx show projects --projects="packages/*" | sort) | |
| printf "All packages:\n$packages\n\n" | |
| packages_with_unit_tests=$(npx nx show projects --with-target=unit-test --projects="packages/*" | sort) | |
| packages_with_int_tests=$(npx nx show projects --with-target=int-test --projects="packages/*" | sort) | |
| printf "Packages with unit tests:\n$packages_with_unit_tests\n\n" | |
| printf "Packages with integration tests:\n$packages_with_int_tests\n\n" | |
| project=$(echo $packages | jq -R | jq -s -c) | |
| echo "project=$project" >> $GITHUB_OUTPUT | |
| printf "GitHub output:\nproject=$project\n\n" | |
| - name: Exclude package tests with missing target | |
| id: exclude | |
| run: | | |
| packages_without_unit_tests=$(comm -23 <(echo $packages) <(echo $packages_with_unit_tests)) | |
| packages_without_int_tests=$(comm -23 <(echo $packages) <(echo $packages_with_int_tests)) | |
| printf "Packages without unit tests:\n$packages_without_unit_tests\n\n" | |
| printf "Packages without integration tests:\n$packages_without_int_tests\n\n" | |
| exclude_unit_tests=$(echo $packages_without_unit_tests | sed '/^$/d' | jq -R | jq -s 'map({ "project": ., "target": "unit-test" })') | |
| exclude_int_tests=$(echo $packages_without_int_tests | sed '/^$/d' | jq -R | jq -s 'map({ "project": ., "target": "int-test" })') | |
| exclude=$(echo "$exclude_unit_tests\n$exclude_int_tests" | jq -s -c add) | |
| echo "exclude=$exclude" >> $GITHUB_OUTPUT | |
| printf "GitHub output:\nexclude=$exclude\n\n" | |
| outputs: | |
| project: ${{ steps.project.outputs.project }} | |
| exclude: ${{ steps.exclude.outputs.exclude }} | |
| coverage: | |
| needs: [list-packages] | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| project: ${{ fromJson(needs.list-packages.outputs.project) }} | |
| target: [unit-test, int-test] | |
| exclude: ${{ fromJson(needs.list-packages.outputs.exclude) }} | |
| name: Collect code coverage | |
| 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: 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.project }}/${{ matrix.target }}s/ | |
| files: ./lcov.info | |
| flags: ${{ matrix.project }}-${{ matrix.target }} | |
| token: ${{ secrets.CODECOV_TOKEN }} | |
| fail_ci_if_error: true |