Run approved commands from comments (ChatOps)
In this task you'll let trusted people run a deployment by commenting on a pull request.
ChatOps puts operations where the team already talks: a comment such as /deploy staging on a pull request starts the deployment, and the bot replies with the result. Everyone sees who asked, what ran and what happened.
The workflow runs on issue_comment. It must check who commented (author_association such as OWNER, MEMBER or COLLABORATOR) and what they asked for, and ignore everything else: a public repository gets comments from anyone.
Example. SS Kaiser's developers comment /deploy staging on their pull requests. The workflow checks they're a collaborator, deploys that branch to staging, and replies 'Deployed abc1234 to staging' with a link to the run.
Before you start
You need: A GitHub repository. Time: 25 minutes.
Where to look
- Repository
- Actions
- New workflow
Steps
- Add a workflow on
issue_comment: types: created. - In the job's
if:, require the comment to be on a pull request, to start with /deploy, and to come from OWNER, MEMBER or COLLABORATOR. - Do the work, then reply with
gh pr commentand the result. - Test with a comment from your account, and confirm a comment from someone else is ignored.
Commands
gh pr comment 12 --body "/deploy staging"
Watch out for
- Cost
- Free on public repositories.
- Security
- Never run code or commands taken from the comment text itself; match exact commands only.
- Common mistakes
- Forgetting the author check, so anyone can trigger a deployment.
Clean up
Delete the workflow file.
Words to know
- ChatOps
- Running operations from a chat or comment, with the result posted back for everyone to see.
- issue_comment
- The GitHub event for comments on issues and pull requests.
- author_association
- How a commenter relates to the repository, such as OWNER, MEMBER, COLLABORATOR or NONE.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On