Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions: Getting Push event when pull request merged

When I merge a pull request, my github action is getting a push event, there is no pull request event at all.

According to this doc: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-your-pull_request-workflow-when-a-pull-request-merges ,there should be a pull request event with closed type but my action is not getting a pull request event, only a push event. As a result, I cannot check "github.event.pull_request.merged" since the github.event object does not have the pull_request object in it at all.

Is this expected? What other way is there to detect when a PR is merged in the action? One solution I can think of is to check if the commit message contains the string "Merge pull request" but if the user modifies the default merge message and removes that string, then it wont work.

Thank you

Below is our workflow file. There are two jobs, one for handling commits and one for pull requests. The pull request job is not triggering when a PR is merged, only the commit job.

name: Github-Jira Integration

on:
  [push, pull_request]
    
jobs:
  commit:
    runs-on: win10-machine

    env:
      JIRA_URL: ${{ secrets.JIRA_URL }}
      JIRA_USER: ${{ secrets.JIRA_USER }}
      JIRA_PWORD: ${{ secrets.JIRA_PWORD }}
      GITHUB_EVENT: ${{ toJson(github.event) }}
      
    if: github.event_name != 'pull_request'
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          repository: our_repo
          path: generic_tools

      - name: Setting up Node.js Version
        uses: actions/setup-node@v3
        with:
          node-version: 14
          
      - name: Installing Node Modules
        run: |
          cd generic_tools/github-plugin
          npm install
          npm install -g ts-node
      
      - name: Run
        shell: cmd
        run: |
          ts-node generic_tools/github-plugin/src/app.ts "head_commit"

  pull_request:
    runs-on: win10-machine
    
    env:
      JIRA_URL: ${{ secrets.JIRA_URL }}
      JIRA_USER: ${{ secrets.JIRA_USER }}
      JIRA_PWORD: ${{ secrets.JIRA_PWORD }}
      GITHUB_EVENT: ${{ toJson(github.event) }}
      
    if: github.event_name == 'pull_request' || github.event.pull_request.merged == true
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          repository: our_repo
          path: generic_tools

      - name: Setting up Node.js Version
        uses: actions/setup-node@v3
        with:
          node-version: 14
          
      - name: Installing Node Modules
        run: |
          cd generic_tools/github-plugin
          npm install
          npm install -g ts-node
      
      - name: Run
        shell: cmd
        run: |
          ts-node generic_tools/github-plugin/src/app.ts "pull_request"

like image 529
Wilson Zhang Avatar asked Dec 22 '25 09:12

Wilson Zhang


1 Answers

The pull_request event has a lot of activity types that could trigger it, enough that there is a caveat on which activity types actually trigger the event, if none are specified;

By default, a workflow only runs when a pull_request event's activity type is opened, synchronize, or reopened. To trigger workflows by different activity types, use the types keyword.

To add the closed event type, all you need to do is change

on:
  [push, pull_request]

to

on:
  push:
  pull_request:
    types:
    # The 3 default types, if you still want them
    - opened
    - synchronize
    - reopened
    # As well as the closed type.
    - closed

Note though that both events will independently trigger the workflow, so it'll still run the push event as well as the pull request event.

Replacing a pull_request trigger with a pull_request_target trigger instead is not a wise idea without fully embracing all the caveat differences between them, such as execution context.

like image 194
Skenvy Avatar answered Dec 24 '25 02:12

Skenvy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!