πŸ’ΈSave up to $132K/month in CI costs!πŸ‘‰ Try Free✨
Skip to main content
← Back to workflows

How to Use Slash Commands in GitHub Actions for ChatOps

peter-evans-slash-command-dispatch -
GitHub Action
Slash Command Dispatch v4.0.0
578
Contributors
Contributor - peter-evans
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: Slash Command Dispatch
on:
issue_comment:
types: [created]

jobs:
slashCommandDispatch:
runs-on: ubuntu-latest
steps:
- name: Slash Command Dispatch
uses: peter-evans/slash-command-dispatch@v4
with:
token: ${{ secrets.PAT }}
commands: |
deploy
integration-test
build-docs

slash-command-dispatch-action logo

slash-command-dispatch-action

This action enables ChatOps by processing slash commands from GitHub issue comments and creating dispatch events.


What is slash-command-dispatch-action?​

The slash-command-dispatch-action allows you to set up slash commands in GitHub comments and dispatch them to execute workflows, enabling efficient "ChatOps" integration. This allows for flexible command processing while keeping the workflow queue moving quickly.

How to Use the token Input​

To dispatch commands, the action requires a repository scoped Personal Access Token (PAT). The default GITHUB_TOKEN will not work for this.

with:
token: ${{ secrets.PAT }}

Make sure the PAT has sufficient permissions, such as public_repo for public repositories.

How to Add Reactions to Comments​

You can configure reactions (e.g., πŸ‘€ for seen, πŸš€ for dispatched) by setting the reactions input:

with:
reactions: true

You can also use a custom reaction-token to have the reactions made by a specific user rather than the GitHub Actions bot:

with:
reaction-token: ${{ secrets.PAT }}

How to Specify Required Permissions for Command Dispatch​

Use the permission input to specify the repository permission level required to execute slash commands. The default is write, meaning users with write, maintain, or admin permissions can execute commands:

with:
permission: write

You can set it to one of the following: none, read, triage, write, maintain, admin.

How to Define the Event Type Suffix​

The event-type-suffix option allows you to customize the suffix for the event type created by the slash command. The default suffix is -command:

with:
event-type-suffix: "-action"

For example, the command /deploy would create an event of type deploy-action.

How to Dispatch Workflow Events​

By default, the action creates repository_dispatch events. If you need to create workflow_dispatch events, set dispatch-type to workflow:

with:
dispatch-type: workflow

This allows workflows to run in specific branches by using the ref argument in your slash command.

How to Add Static Arguments to Commands​

You can define static arguments that will be dispatched with every command using the static-args input:

with:
static-args: |
production
region=us-east-1

These arguments will be included in the payload along with any dynamic arguments provided in the comment.

How to Handle Command Outputs​

To respond to a command completion, you can add a reaction or a comment. Here’s how to add a πŸŽ‰ reaction to the comment:

- name: Add reaction
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.PAT }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
reactions: hooray

You can also create a new comment with a link to the run output:

- name: Create URL to the run output
id: vars
run: echo "run-url=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> $GITHUB_OUTPUT

- name: Create comment
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.PAT }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
issue-number: ${{ github.event.client_payload.github.payload.issue.number }}
body: |
[Command run output][1]

[1]: ${{ steps.vars.outputs.run-url }}