TUTORIAL

Uploading Code Coverage in a Separate Job on GitHub Actions

May 12, 2021 Tom Hu

This tutorial walks through how to upload coverage reports in a separate job on GitHub Actions. Although all developers can benefit from doing this, we recommend it most for users that do the following:

  1. Upload more than 100 uploads per commit
  2. Need a separation in responsibility between jobs, and/or
  3. Want to control uploads to Codecov as a single final step.

To do this, we’ll be using GitHub’s actions/upload-artifact and actions/download-artifact. All of the code for this post can be found in our example repository.

Saving artifacts

GitHub provides an action to upload artifacts in the same workflow. We use this to temporarily store coverage reports before uploading them to Codecov. We can add the following code at the end of a job that tests and collects coverage for a subset of tests.

 - uses: actions/upload-artifact@v2
   with:
   name: {{ any_descriptive_name }}
   path: {{ path/to/coverage/report }}

This will upload the coverage report at the given path to a specific name. The name attribute is not required and the path can be a directory, a wildcard selector, or a list of files.

If you are having trouble finding the coverage report path, and you have uploaded coverage to Codecov previously, you can find the file in the Codecov output:

==> Reading reports
+ ./coverage.xml

In this case, the coverage report is found at ./coverage.xml, and we can use the following snippet in our job

 - uses: actions/upload-artifact@v2
   with:
   name: backend-coverage
   path: ./coverage.xml

Creating the Codecov job

The Codecov job can be written as a dependency to the applicable coverage jobs.

jobs:
  ...
  upload-to-codecov:
    needs: [ {{ names-of-jobs }} ]
    steps:
      - name: Checkout
        uses: actions/checkout@v2

In the example repository, we have two jobs, frontend-tests and backend-tests that upload artifacts. Our code snippet becomes

jobs:
  ...
  upload-to-codecov:
    needs: [ frontend-tests, backend-tests ]
    steps:
      - name: Checkout
        uses: actions/checkout@v2

Downloading artifacts

The Codecov job will need access to the artifacts. We can use actions/download-artifact to do this.

      - name: Download artifacts
        uses: actions/download-artifact@v2

Using the Codecov Action

We are now set to upload coverage reports to Codecov. We can use the Codecov GitHub Action to do this.

      - name: Upload to Codecov
        uses: codecov/codecov-action@v2

Summary

With these code changes, we can upload coverage reports as a single job to Codecov. The entire job looks like this

steps:
  ...
  upload-to-codecov:
    needs: [frontend-tests, backend-tests]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Download artifacts
        uses: actions/download-artifact@v2
      - name: Upload to Codecov
        uses: codecov/codecov-action@v2

To see the final product in action, check out the workflow or the example repository above.

Before we redirect you to GitHub...
In order to use Codecov an admin must approve your org.