gh pr comment Command Reference
Post a comment on a pull request straight from your pipeline.
gh pr comment adds a comment to a pull request. CI jobs use it to post plan summaries, test results, or deploy links back onto the PR.
What it does
gh pr comment posts a comment on a PR (by number, URL, or the current branch). With --edit-last it updates the most recent comment by the same actor instead of adding a new one, keeping the PR thread tidy across re-runs.
Common flags and usage
- --body / --body-file: comment text inline or from a file
- --edit-last: update the previous comment instead of adding a new one
- --create-if-none: with --edit-last, create one if none exists
- [number | url | branch]: target a specific PR
Example
- name: Post plan summary
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
terraform show -no-color tfplan > plan.txt
gh pr comment "${PR_NUMBER}" --body-file plan.txt --edit-last --create-if-noneIn CI
Use --edit-last --create-if-none so repeated runs update a single comment rather than flooding the PR. --body-file is convenient for posting a rendered terraform plan or test report.
Using this in CI
Cloud CLIs behave differently on a runner than on your laptop. They assume no interactive terminal, no cached credentials, and no browser for device-code flows, so the same command that works locally can hang or fail on a runner.
- Authenticate with a short-lived OIDC token rather than a long-lived static key. GitHub Actions can exchange
id-token: writefor cloud credentials with no stored secret. - Always pass the non-interactive flag. Most cloud CLIs will otherwise prompt and hang until the job times out.
- Pin the CLI version. Cloud CLIs change output formats between minor releases, and any script parsing that output will break silently.
- Set the output format explicitly (
--output json) rather than relying on the default, which can differ by version and configuration profile.
Key takeaways
- --body-file posts file contents like a rendered plan or test report.
- --edit-last --create-if-none keeps one updating comment across re-runs.
- Target any PR by number, URL, or current branch.