How to Handle a Slash Command With repository_dispatch in GitHub Actions
A repository_dispatch workflow receives the command name as an event type and the parsed arguments in github.event.client_payload.
slash-command-dispatch fires a repository_dispatch with event_type of <command>-command. Match it with on.repository_dispatch.types and read the PR context from client_payload.
Steps
- Trigger on
repository_dispatchwithtypes: [deploy-command]. - Read the PR number from
github.event.client_payload. - Run the command logic against that PR.
Handler workflow
.github/workflows/deploy-command.yml
on:
repository_dispatch:
types: [deploy-command]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- env:
PR: ${{ github.event.client_payload.pull_request.number }}
REF: ${{ github.event.client_payload.pull_request.head.sha }}
run: ./deploy.sh --pr "$PR" --sha "$REF"Gotchas
- The dispatcher already checked permissions, but a standalone repository_dispatch handler must not be triggerable by unauthorized API calls; require a scoped token.
client_payloadcarries the slash command args and the PR object populated by the dispatcher.- repository_dispatch workflows always run from the default branch.
Related guides
How to Use slash-command-dispatch in GitHub ActionsRoute pull request slash commands to dedicated workflows in GitHub Actions with peter-evans/slash-command-dis…
How to Use Dispatch Types and Named Args in slash-command-dispatchConfigure dispatch types and named arguments in peter-evans/slash-command-dispatch so a slash command carries…