Trigger action via pull request message

We’re wondering if we can replace Jenkins by GitHub actions.

One thing we wanted to have is the ability to comment on the PR to trigger actions that might or might not had run before (and might not have failed previous executions).

We have an action that takes a while to complete, we don’t want to make it mandatory to be ran on every PR / every commit, but the user should be able to run it on demand by adding a specific comment to that PR.

Some possible use cases for this are

  • spin up an mock environment and run mock tests
  • trigger an Kubernetes deployment with the PRs code for QA/PO/UX validation

Is this Possible?
If not is there Any alternative solution that might help achieve similar features?
Is it on the roadmap?
Is the roadmap public?

Hey! This sounds like a cool idea. I have one clarifying question: Should the user be able to add the comment after the PR is already opened and it still triggers the workflow? 

You could do something like: 

if: github.event_name == ‘pull_request’ && github.event.action == ‘comment’

See https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsif

2 Likes

We just finished porting both our CI (from Travis) and CD (from Jenkins) to Github Actions, and it works great.

I also faced the exact same question - how to trigger.

After evaluating all the options, and especially looking into GitHub Deployment Event, I ended up deciding on using moving tags, here is why:

  1. Tags are easy to push - no need for any API, just git.
  2. Tags can be as simple (“stage”) or as expressive as you need (“stage-some-meta-info”)
  3. Tags can be used on any branch
  4. Tags can be used for everything - deploy to production, deploy to stage, rollback, etc.
  5. Tags can be moved from one commit to another
  6. Looking at your commit history, you see exactly which commit is deployed where, since it is tagged

I know this is not ideal, but until GitHub Actions integrates better with GitHub deployments, or until there is support for manual triggers, this is the best solution I could find.

1 Like

Hi @canastro ,

Add/edit the comment will not trigger the pull request action, currently it’s not supported.

As an alternative, you can add a specific label/milestone/title code…etc to trigger the action. Please refer to the types of the pull request events here for more details.

Code sample as below:

name: prcheck
on:
  pull_request:
    types:
      - labeled
jobs:
  prcheck:
    if: github.event.label.name == 'runtest'
    runs-on: [ubuntu-latest]
    steps:...

Or 

name: prcheck
on:
  pull_request:
    types:
      - edited
jobs:
  prcheck:
    if: contains(github.event.pull_request.title, 'WIP')

The roadmap is not public and i didn’t find related. 

According to the policy, it’s recommended to raise your idea here where github manager will take a review, thanks.

4 Likes

Hi @logankilpatrick 

Yes, the user should be able to add the comment at any time in an open PR.

Forgive me for the newbie question, but wouldn’t doing something like if: github.event\_name == 'pull\_request' && github.event.action == 'comment' trigger the action for all PRs and not only the PR where the comment is done?

Its not ideal, but thanks and I’ll consider it as well!

I saw on their homepage that there is a command called pull_request_comment. I’m hoping that having it there is some kind of indication that it is in the roadmap and should be played soon. fingers crossed.

Also, thank you for the link to submit the idea.

1 Like

My case is to trigger workflows on PRs, these PRs might never get to be merged. So pushing tags would be unnecessary polution for this case.

For the deployment to QA environment, I just deploy on every push to master.

Hi @canastro ,

You can use ‘issue_comment’ event(on master branch), it will monitor the comment for both issue and pull_request.

And you can filter pull request with if condition in job level.

Check the body if it contains specific words.

code sample as below, the workflow will be executed when comment pull request with body contains ‘commenttest’.

name: commentcheck
on:
  issue_comment:
    types: [created, edited, deleted]

jobs:
 job01:
if: contains(github.event.comment.html\_url, '/pull/') && contains(github.event.comment.body, 'commenttest') runs-on: [ubuntu-latest]
    steps:...

You can refer to similar ticket: https://github.community/t5/GitHub-Actions/Triggering-workflow-only-for-Pull-Request-Comment/m-p/40816

Thanks.

1 Like

Great, I’ll give it a try this week and I’ll let you know! 

Thanks.

1 Like

@weide-zhou from what I could understand issue_comment triggers correctly when I comment, but is not executed on the expectedbranch.

In master I had: 

name: Test

on:
  issue_comment:
    types: [created]

jobs:
  tests:
    if: contains(github.event.comment.html_url, '/pull/') && contains(github.event.comment.body, 'test this please')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Install
        uses: bahmutov/npm-install@v1
      - name: Run tests
        run: npm t

I opened a PR where the only change I made was to explicitly break a test. I commented that PR with ‘test this please’ and the test passed. So I assume that the checkout action checkous the “master” branch and not the branch of the PR that I just commented.

Is there a way to work around this? 

Hi @canastro ,

You can sepcify the branch which you’d like to checkout in action ‘actions/checkout@v2’.

Code sample as below:

- name: Checkout
        uses: actions/checkout@v2
        with:
           ref: devbranch

Thanks.

Hello,

As we currently do with Jenkins the goal would be to run in every every PR that receives such comment. So the PR could be comming from any branch, be it upstream or a fork. 

  1. You need to get the pull url from issue_comment event context.

  2. Use restapi to get the pull details and parse the upstream branch name.

  3. Transfer the branch name  to ‘actions/checkout’ for the ref value.

Code sample as below:

name: commentcheck
on:
  issue_comment:
    types: [created, edited, deleted]

jobs:
 job01:
    if: contains(github.event.comment.html_url, '/pull/') && contains(github.event.comment.body, 'commenttest')
    runs-on: [ubuntu-latest]
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - name: get pullrequest url
        run: |
          echo ${{ github.event.issue.pull_request.url }}
      - name: get upstream branch
        id: upstreambranch
        run: |
          echo "::set-output name=branchname::$(curl -v -H "Accept: application/vnd.github.sailor-v-preview+json" -u ${{ secrets.PAT }} ${{ github.event.issue.pull_request.url }} | jq '.head.ref' | sed 's/\"//g')"
      - name: echo upstream branch
        run: |
          echo ${{ steps.upstreambranch.outputs.branchname }}
      - name: Checkout upstream repo
        uses: actions/checkout@v2
        with:
           ref: ${{ steps.upstreambranch.outputs.branchname }}

If the pull request comes from a fork repo, please get the fork repo info from rest api and transfer to checkout action with ‘repository’ and ‘ref’ args.

- uses: actions/checkout@v2
  with:
    repository: ''
    ref: ''
1 Like

@weide-zhou wrote:

  1. You need to get the pull url from issue_comment event context.
  1. Use restapi to get the pull details and parse the upstream branch name.
  1. Transfer the branch name  to ‘actions/checkout’ for the ref value.

Code sample as below:

name: commentcheck
on:
issue_comment:
types: [created, edited, deleted]

jobs:
job01:
if: contains(github.event.comment.html_url, ‘/pull/’) && contains(github.event.comment.body, ‘commenttest’)
runs-on: [ubuntu-latest]
steps:

  • name: Dump GitHub context
    env:
    GITHUB_CONTEXT: ${{ toJson(github) }}
    run: echo “$GITHUB_CONTEXT”
  • name: get pullrequest url
    run: |
    echo ${{ github.event.issue.pull_request.url }}
  • name: get upstream branch
    id: upstreambranch
    run: |
    echo “::set-output name=branchname::$(curl -v -H “Accept: application/vnd.github.sailor-v-preview+json” -u ${{ secrets.PAT }} ${{ github.event.issue.pull_request.url }} | jq ‘.head.ref’ | sed 's/”//g’)"
  • name: echo upstream branch
    run: |
    echo ${{ steps.upstreambranch.outputs.branchname }}
  • name: Checkout upstream repo
    uses: actions/checkout@v2
    with:
    ref: ${{ steps.upstreambranch.outputs.branchname }}

If the pull request comes from a fork repo, please get the fork repo info from rest api and transfer to checkout action with ‘repository’ and ‘ref’ args.

  • uses: actions/checkout@v2
    with:
    repository: ‘’
    ref: ‘’
name: commentcheck
on:
  issue_comment:
    types: [created, edited, deleted]

jobs:
 job01:
    if: contains(github.event.comment.html_url, '/pull/') && contains(github.event.comment.body, 'commenttest')
    runs-on: [ubuntu-latest]
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - name: get pullrequest url
        run: |
          echo ${{ github.event.issue.pull_request.url }}
      - name: get upstream branch
        id: upstreambranch
        run: |
          echo "::set-output name=branchname::$(curl -v -H "Accept: application/vnd.github.sailor-v-preview+json" -u ${{ secrets.PAT }} ${{ github.event.issue.pull_request.url }} | jq '.head.ref' | sed 's/\"//g')"
      - name: echo upstream branch
        run: |
          echo ${{ steps.upstreambranch.outputs.branchname }}
      - name: Checkout upstream repo
        uses: actions/checkout@v2
        with:
           ref: ${{ steps.upstreambranch.outputs.branchname }}

If the pull request comes from a fork repo, please get the fork repo info from rest api and transfer to checkout action with ‘repository’ and ‘ref’ args.

Tried something similiar with octokit/request-action and gr2m/get-json-paths-action based on answers to this question. But actions triggered like this don’t become a “check” of the PR. So if the job fails it won’t be marked as failed in the PR. (Which should prevent the PR to be merged)

That is correct, but you can use a GitHub statuses API to report a failure.

- name: Report failure
        run: |
          curl --request POST \
          --url https://api.github.com/repos/OWNER/REPO/statuses/${{ sha }} \
          --header 'authorization: token ${{ secrets.PAT }}' \
          --header 'content-type: application/json' \
          --data '{"state": "failure","target_url": "https://example.com/build/status","description": "The tests failed!","context": "ci/tests"}'
        if: failure()

Or success with a similar approach.

1 Like