Sub topic 15
Reporting on pull requests from forks
This chapter assumes you know how pull_request workflows run, what GITHUB_TOKEN is, and how to upload artifacts and write job summaries. It explains why reporting steps that work on your own branches can fail on pull requests from forks, and how to publish results safely anyway.
Why forks behave differently
When a pull request comes from a fork, GitHub treats the code as untrusted. For the pull_request event, the workflow runs with these limits:
GITHUB_TOKENis read-only, whatever yourpermissions:block asks for.- Repository secrets are not passed to the job.
Actions that create check runs or post pull request comments need write access. On a fork PR they usually fail with Resource not accessible by integration.
| Reporting method | Works on fork PRs? |
|---|---|
| Log output | Yes |
| Annotations from workflow commands | Yes |
| Job summary (`GITHUB_STEP_SUMMARY`) | Yes |
| Artifacts | Yes |
| Check runs and PR comments | No, the token is read-only |
| Coverage services that need a secret | No, no secrets are available |
The simple answer: keep it in the run
For many projects the first four rows are enough. The job summary and annotations already show results on the run page. Make sure those steps do not depend on a write token, and keep if: ${{ !cancelled() }} on them so they run after failures.
Publishing with a second workflow
If you need a PR comment or a check run, split the work in two:
- The
pull_requestworkflow runs the tests with no privileges and uploads the reports as an artifact. - A
workflow_runworkflow starts when the first one completes. It runs in the context of the base repository, so it has a write token. It downloads the artifact and publishes.
In the tests workflow, save the PR number next to the reports, because the workflow_run payload does not reliably include it for forks:
- run: echo "${{ github.event.number }}" > reports/pr-number.txtAdd that step before the upload-artifact step from earlier chapters. The publishing workflow then looks like this:
name: publish-test-results
on:
workflow_run:
workflows: [tests]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
comment:
if: ${{ github.event.workflow_run.event == 'pull_request' }}
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: test-reports
path: reports
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Comment on the pull request
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
RUN_URL: ${{ github.event.workflow_run.html_url }}
run: |
pr="$(cat reports/pr-number.txt)"
case "$pr" in ''|*[!0-9]*) echo "Invalid PR number"; exit 1;; esac
gh pr comment "$pr" --body "Test reports are available in the [workflow run]($RUN_URL)."The actions: read permission lets the job download artifacts from another run. The pull-requests: write permission lets it comment.
Security rules
The publishing workflow has write access, so it must never run code from the pull request.
- Treat every downloaded file as untrusted data. Validate it, as the PR number check does above, and never execute it or pass it to a shell unchecked.
- Do not check out the PR branch in a privileged workflow.
- Avoid
pull_request_targetfor running tests. It also gives a write token and secrets, and checking out the PR head there lets a fork run arbitrary code with those rights.
Exercise
A coverage action that posts a PR comment works on branches in your repository but fails on a fork's PR with "Resource not accessible by integration". Why, and what is the safe fix?
Show answer
On pull_request events from forks, GITHUB_TOKEN is read-only and secrets are unavailable, so the action cannot write the comment. The safe fix is to upload the coverage report as an artifact in the pull_request workflow. A separate workflow_run workflow, which has a write token, then downloads the artifact, validates it without executing it, and posts the comment. Do not check out and run the fork's code in that privileged workflow.