Search code examples
github-actionscodecov

Generating test coverage reports only once within a GitHub Actions matrix


I have a test suite that I want to run in a matrix strategy using GitHub Actions. I want to generate a code coverage report for my tests, but only for a single matrix item.

Here is a copy of the action I'm working with:

name: test
on: [pull_request, workflow_dispatch]
jobs:
  run_tests:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x]
    steps:
      - uses: actions/checkout@v2
        name: Test Node.js ${{ matrix.node-version }}
      - uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm install --production=false
      - run: npm test -- --coverage ## I would prefer to only generate coverage once
      - name: Upload to Codecov     ## I would like to only run this once
        run: |
          curl -Os https://uploader.codecov.io/latest/linux/codecov
          chmod +x codecov
          ./codecov

Is it possible to only invoke the Upload to Codecov step only for one (could be final, or first) of the matrix strategy invocations? I'd like the solution to be general, rather than "if the value is 16.x", so that in future when we update the matrix versions we don't have to also update which version generates the report.


Solution

  • Sure you can:

    - name: Upload to Codecov
      if: ${{ matrix.node-version }} == '16.x'
      run: |
          curl -Os https://uploader.codecov.io/latest/linux/codecov
          chmod +x codecov
          ./codecov
    

    or

    - name: Upload to Codecov
      run: |
        if [[ $(node -v) == v16* ]]; then
          curl -Os https://uploader.codecov.io/latest/linux/codecov
          chmod +x codecov
          ./codecov
        fi