How to Comment on a Pull Request From a Workflow in GitHub Actions
actions/github-script calls the REST API with the built-in token to leave a PR comment.
Use actions/github-script to call issues.createComment with the PR number and your message. Grant pull-requests: write permission.
Steps
- Add
permissions: pull-requests: writeto the job. - Use
actions/github-scriptwithgithub.rest.issues.createComment. - Pass
context.issue.numberas the PR number.
Workflow
.github/workflows/pr.yml
permissions:
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: 'Build passed and coverage is 92%.'
})Gotchas
- On
pull_requestfrom a fork, GITHUB_TOKEN is read-only; usepull_request_targetcarefully. - Find and update an existing comment to avoid spamming a new one each run.
Related guides
How to Auto-Label Pull Requests in GitHub ActionsAutomatically label pull requests by changed paths in GitHub Actions with actions/labeler and a labeler confi…
How to Run a Workflow on a Fork Safely in GitHub ActionsRun GitHub Actions on fork pull requests without leaking secrets by splitting untrusted build from a privileg…