Skip to content

Upload coverage to Codecov #584

Upload coverage to Codecov

Upload coverage to Codecov #584

name: Upload coverage to Codecov
on:
workflow_run:
workflows: ["Build, test and package"]
types:
- completed
defaults:
run:
shell: pwsh
jobs:
main:
name: Upload code coverage
permissions:
contents: read
runs-on: ubuntu-latest
steps:
- name: Dump github context for debug purposes
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: $env:GITHUB_CONTEXT
# Checking out the repo is necessary for the codecov/codecov-action@v4 to work properly. Codecov requires
# the source code to be able to process the coverage file and generate coverage reports.
# Otherwise the upload step works but the processing of the coverage files fails with the error: "unusable report".
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
- name: Download test coverage file
uses: dawidd6/action-download-artifact@v6
with:
name: opencover-test-coverage-file
run_id: ${{ github.event.workflow_run.id }}
# On the codecov/codecov-action@v4 steps below I'm using the override_commit, override_branch
# and override_pr options to make sure the right information is sent to Codecov.
#
# If this wasn't done in a separate workflow from the `Build, test and package` workflow then
# these override options wouldn't be needed.
#
# The action would always be able to pick the right values whether it's a PR or not. Since we're
# running the Codecov action from a separate workflow, which is triggered form a workflow run event, we
# need to provide the Codecov action with the right values otherwise the default values that are used are
# incorrect for pull requests and one of the issues that it causes is, for instance, the status checks
# aren't set on PRs because the SHA used for the code coverage upload doesn't point to the PR's SHA.
- name: Upload test coverage to Codecov
uses: codecov/codecov-action@v4
if: github.event.workflow_run.event != 'pull_request'
with:
token: ${{ secrets.CODECOV_TOKEN }} # even though it's not required for public repos it helps with intermittent failures caused by https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954, https://github.com/codecov/codecov-action/issues/598,
files: ./coverage.net8.0.opencover.xml
fail_ci_if_error: true
verbose: true
override_commit: '${{ github.event.workflow_run.head_sha }}'
override_branch: '${{ github.event.workflow_run.head_branch }}'
- name: Upload test coverage to Codecov
uses: codecov/codecov-action@v4
if: github.event.workflow_run.event == 'pull_request'
with:
token: ${{ secrets.CODECOV_TOKEN }} # even though it's not required for public repos it helps with intermittent failures caused by https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954, https://github.com/codecov/codecov-action/issues/598,
files: ./coverage.net8.0.opencover.xml
fail_ci_if_error: true
verbose: true
override_commit: '${{ github.event.workflow_run.head_sha }}'
override_branch: '${{ github.event.workflow_run.head_branch }}'
override_pr: '${{ github.event.workflow_run.pull_requests[0].number }}'
- name: Log Codecov info
run: |
$codeCoveUrl = "https://app.codecov.io/gh/${{ github.repository }}/"
Write-Output "::notice title=Code coverage::Code coverage has been uploaded to Codecov at $codeCoveUrl."