Sub topic 13
Publishing to check runs, pull requests, and coverage services
This chapter assumes you can already write JUnit XML and coverage XML in a workflow. It also assumes you know how permissions: works at the workflow and job level. Job summaries and artifacts stay inside the run page. Here you push results to places reviewers already look: the Checks tab, a pull request comment, or an external coverage service.
Three places to publish
| Target | What the reader sees | Typical permission |
|---|---|---|
| Check run | A named check with a test table and annotations | `checks: write` |
| Pull request comment | A comment that is updated on each push | `pull-requests: write` |
| Coverage service | Hosted history, diffs, and a status check | A service token or OIDC |
The default GITHUB_TOKEN can read repository contents but cannot write checks or comments until you grant it permission. Grant only what the job needs, and set it on the job that publishes.
Publishing test results
The EnricoMi/publish-unit-test-result-action action reads JUnit XML files. It creates a check run and, on pull requests, a comment. It needs these permissions:
name: tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read
issues: read
checks: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install -r requirements.txt pytest pytest-cov
- run: pytest --junitxml=reports/junit.xml --cov=src --cov-report=xml:reports/coverage.xml
- uses: EnricoMi/publish-unit-test-result-action@v2
if: ${{ !cancelled() }}
with:
files: reports/junit.xmlThe if: ${{ !cancelled() }} line matters. Without it, a failing pytest step skips the publish step, and you lose the report exactly when it is most useful.
Sending coverage to a service
Services such as Codecov accept the XML file you already generated. Store the upload token as a repository secret and pass it in.
- uses: codecov/codecov-action@v5
if: ${{ !cancelled() }}
with:
files: reports/coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}The service then reports a status on the pull request, and you can set its own rules, such as a minimum for changed lines. These rules are separate from --cov-fail-under, which fails your job. Decide which one is the gate so developers do not get two conflicting signals.
Common errors
- "Resource not accessible by integration". The token lacks
checks: writeorpull-requests: write. Add the permission to the job that publishes. - Permissions silently narrowed. Once you set
permissions:at all, every unlisted scope becomesnone. Re-listcontents: reador checkout fails on private repositories. - Duplicate comments. Hand-written
gh pr commentsteps post a new comment on every push. Use an action that updates a single comment. - No report on failure. The publish step is missing a status condition.
- Empty results on fork pull requests. The token is read-only there, so publishing fails. The fork chapter covers the safe workaround.
Exercise
A publishing job uses permissions: checks: write and nothing else. The checkout step now fails in a private repository. Why, and what is the fix?
Show answer
Setting any permissions: key sets every unlisted scope to none, including contents. Checkout needs contents: read, so add contents: read alongside checks: write.
Exercise
Why should the publish step use if: ${{ !cancelled() }}?
Show answer
By default a step runs only if all earlier steps succeeded. A failing test step would skip publishing. !cancelled() lets the step run after a failure but still skips it if the run was cancelled.