Triggering workflow only if Pull Request Comment

Hello,

I’m trying to run an action only when a PR comment is posted. After reading the documentation I found that the workflow should exist on the master branch so I created and merged to master but there is no way to get it.

This is the workflow definition:

name: Create QA environment
on:
  issue_comment:
    types: [created]
env:
  BASE_PATH: ...
  PROJECT_NAME: ...
jobs:
  wait-for-travis:
    name: Wait for Travis
    runs-on: ubuntu-latest
    if: github.event.issue.pull_request && contains(github.event.comment.body, '/qa')
    outputs:
      conclusion: ${{ steps.wait-for-test.outputs.conclusion }}
    steps:
       ....

Any suggestions? :pray:

name: Create QA environment
on:
  issue_comment:
    types: [created]

jobs:
  qa-env:
    runs-on: ubuntu-latest
    outputs:
      conclusion: ${{ steps.wait-for-test.conclusion }}
    steps:
      # - name: Dump GitHub context
      #   env:
      #     GITHUB_CONTEXT: ${{ toJson(github) }}
      #   run: echo "$GITHUB_CONTEXT"

      - name: Wait for Travis
        id: wait-for-test
        if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/qa') }}
        run: echo "Event triggered!"

Try this workflow, it should echo "Event triggered!" when you comment “/qa” on a pull request.

1 Like

Hi Nathan,

Thanks but I tried the workflow and it’s not triggered. Also added the dump context that should be triggered for any comment and the same result.

name: Test action on PR comment
on:
  issue_comment:
    types: [created]

jobs:
  test-action:
    runs-on: ubuntu-latest
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - name: Test action on PR comment
        if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/qa') }}
        run: echo "Event triggered!"

@mcanaves ,

This should work.
I also set up a workflow like as below to test on my side:

name: Issues

on:
  issue_comment:
    types: [created]

jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: view the github context
        run: echo "$GITHUB_CONTEXT"
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}

  job2:
    if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, '/qa') }}
    runs-on: ubuntu-latest
    steps:
      - name: run for PR
        run: echo "Triggered for PR!"

When I added a comment (contains ‘/qa’) on a PR, job2 can run normally as expected. See the example workflow run.

When I added a comment (contains ‘/qa’) on a issue, job2 was skipped, this also is the expected result. See the example workflow run.

Finally found where was the problem, the events worked always but there was a problem with a dependant job that blocked the comment event. Thanks @brightran and @nathane.