Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 12

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?

11 Replies
Highlighted
Commander Lvl 1
Message 2 of 12

Re: Trigger action via pull request message

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

 

 

Highlighted
Copilot Lvl 2
Message 3 of 12

Re: Trigger action via pull request message

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?

Highlighted
Pilot Lvl 1
Message 4 of 12

Re: Trigger action via pull request message

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.

 

 

 

 

 

 

Highlighted
Copilot Lvl 2
Message 5 of 12

Re: Trigger action via pull request message

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.

Highlighted
GitHub Partner
Message 6 of 12

Re: Trigger action via pull request message

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.

Highlighted
Copilot Lvl 2
Message 7 of 12

Re: Trigger action via pull request message

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.

Highlighted
GitHub Partner
Message 8 of 12

Re: Trigger action via pull request message

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/408...

Thanks.

Highlighted
Copilot Lvl 2
Message 9 of 12

Re: Trigger action via pull request message

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

Thanks.

Highlighted
Copilot Lvl 2
Message 10 of 12

Re: Trigger action via pull request message

@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?