How to Trigger a Workflow on an Issue or PR Comment in GitHub Actions
The issue_comment event fires on comments for both issues and pull requests, so you can build slash-command automation.
Listen with on.issue_comment and gate the job with an if that checks the comment body. For PR comments, github.event.issue.pull_request is present.
Steps
- Add
issue_comment:underonwithtypes: [created]. - Gate the job with
if: contains(github.event.comment.body, '/deploy'). - Check
github.event.issue.pull_requestto act on PR comments only.
Workflow
.github/workflows/chatops.yml
on:
issue_comment:
types: [created]
jobs:
deploy-command:
if: github.event.issue.pull_request && contains(github.event.comment.body, '/deploy')
runs-on: ubuntu-latest
steps:
- run: echo "Deploy requested by ${{ github.event.comment.user.login }}"Gotchas
- issue_comment runs against the default branch workflow, not the PR head, so it cannot build fork code with secrets.
- Anyone who can comment can fire it; check
author_associationbefore running privileged steps.
Related guides
How to Trigger a Workflow When a Label Is Added in GitHub ActionsRun a GitHub Actions workflow when a specific label is added to a pull request using the labeled activity typ…
How to Trigger a Workflow Manually With Inputs in GitHub ActionsAdd a manual Run workflow button in GitHub Actions with workflow_dispatch and typed inputs, then read the cho…