Lab: Event - Discussion Comment
Introduction
In this lab, you will create a GitHub Actions workflow that reacts when someone comments on a GitHub Discussion. You will also use the contains
function to conditionally run logic based on the comment’s content and demonstrate how to restrict workflow permissions for better security.
Estimated Duration: 25–30 minutes
Instructions
Step 1: Prepare your repository
Ensure that GitHub Discussions is enabled in your repository:
- Navigate to your repository on GitHub.
- Click Settings > General.
- Under Features, ensure Discussions is checked.
Step 2: Create the workflow
-
In your repository, click the Code tab.
-
Create a new workflow file:
- Path:
.github/workflows/event-discussion-comment.yml
- File content:
name: Event - Discussion Comment on: discussion_comment: types: [created] permissions: contents: read discussions: read jobs: check-comment: runs-on: ubuntu-latest steps: - name: Log comment run: echo "Comment: $" - name: Check for keyword in comment if: contains(github.event.comment.body, 'urgent') run: echo "This comment is marked as URGENT!" - name: Check for unauthorized keyword if: contains(github.event.comment.body, 'shutdown') run: | echo "::warning::Detected sensitive keyword 'shutdown'. Manual review recommended."
- Path:
-
Commit the file to the
main
branch.
Step 3: Trigger the workflow
- Go to the Discussions tab.
- Open any existing discussion or create a new one.
- Add a comment that contains the word
urgent
orshutdown
.
Example comments:
- “This is urgent, please help!”
- “Should we shutdown the system?”
Step 4: View the workflow output
- Click the Actions tab.
- Open the latest run of Handle Discussion Comments.
-
Review the logs under each step:
- See the full comment body.
- Observe if the
urgent
message is logged. - See a warning if
shutdown
is detected.
Explanation of Key Concepts
1. discussion_comment
Event
- Triggers the workflow when a new comment is added to a discussion.
- Supported
types
:created
,edited
,deleted
.
2. contains
Function
Used for conditional logic based on strings:
if: contains(github.event.comment.body, 'urgent')
3. permissions
Restrict workflow token scope:
permissions:
contents: read
discussions: read
This ensures the workflow can’t modify content or discussions—only read access is allowed.
Summary
In this lab, you created a workflow that responds to new discussion comments, uses conditional logic based on comment content, and demonstrates scoped GitHub token permissions. This sets the foundation for building more secure and context-aware workflows in collaborative projects.