GitHub Action on a PR comment

We wanted a way to run our Laravel Dusk GitHub Action only in certain circumstances. Of course, with GitHub Actions you can use the workflow_dispatch to manually run the action from the Actions tab. However, I have always found that tab in the GitHub Repos to be confusing and out of the way.

Instead, I wanted to run the action when we comment on an active PR with: /dusk. This comment would then kick off our Dusk Action from above and give us a thumbs up or thumbs down if it fails.

An example action.yml looks like this:

name: on-pr-comment

on:
  pull_request:
  issue_comment:
    types: [created]
  workflow_dispatch:

jobs:
  rocket:
    ## Just to react to valid comment with rocket
    runs-on: ubuntu-latest
    if: ${{ github.event.comment &&
          github.event.issue.pull_request &&
          startsWith(github.event.comment.body, '/buildme') }}
    steps:
      -
        name: Reaction
        run: |
          # send reaction to comment to show build was triggered
          curl ${{github.event.comment.url}}/reactions \
            -X POST \
            -d '{"content":"rocket"}' \
            -H "Accept: application/vnd.github.squirrel-girl-preview+json" \
            -H "Authorization: token ${{github.token}}"

Walking through the lines you can see we run the action on whenever an issue_comment is created this allows us to capture all comments on the PR.

This typically can run up your GitHub actions but to prevent that we add the if check. We check if the event is a comment, if we are on a PR, and if the body of the comment starts with our keyword. This if check prevents us from running the Action if those conditions are not met, meaning we don't spend any Action time for comments we want to ignore.

Then you can process your job as usual, I added a reaction to show the job was queued and another to show the status. To react when you job fails, you can simply add a step with if: failure().

You can check out my test repo to see it in action.