How to Run a Workflow on Issue Comment Commands in GitHub Actions
ChatOps lets reviewers trigger deploys or tests by commenting, but you must parse the command and check who sent it.
Trigger on issue_comment, gate on the comment body and author association, then run the action the command requested.
Steps
- Trigger on issue_comment with type created.
- Gate the job with an if that matches the command prefix in the comment body.
- Check github.event.comment.author_association so only collaborators can run it.
- Acknowledge with a reaction or reply so the author sees it was picked up.
Workflow
.github/workflows/comment-command.yml
name: Comment Command
on:
issue_comment:
types: [created]
jobs:
deploy:
if: ${{ startsWith(github.event.comment.body, '/deploy') && github.event.comment.author_association == 'MEMBER' }}
runs-on: ubuntu-latest
steps:
- run: echo "running deploy requested by ${{ github.event.comment.user.login }}"Notes
- issue_comment fires for both issues and PRs, so check github.event.issue.pull_request if you only want PRs.
- Latchkey managed runners run these on-demand command jobs cheaper and self-heal on runner failure.
Related guides
How to Run a Job Only When a Label Is Added in GitHub ActionsTrigger a GitHub Actions job only when a specific label is added to a pull request by listening on the labele…
How to Comment on a Pull Request From a Workflow in GitHub ActionsPost a comment on a pull request from a GitHub Actions workflow using actions/github-script and the GITHUB_TO…