Search code examples
github-actionspull-request

How to run a GHA when the PR is MERGED?


im using github enterprise
i need a github action with 2 steps: 1st runnning whenever a pull-request is opened from 'head' branch to 'base' and the 2nd when the PR is merged.
for now i've created 2 github actions for it:

name: PR Opened

on:
  pull_request:
    branches:
    - base
jobs:
  api_call_on_pr:
    runs-on: ubuntu-latest
    steps:

    - name: API call on PR
      if: ${{ github.head_ref == 'head'  }}   
      id: api_call
      run: |
         curl "api call on PR"

and

name: PR MeRged

on:
  pull_request:
    branches:
      - base
    types:
      - closed      

jobs:
  api_call_on_merge:
    runs-on: ubuntu-latest
    steps:

    - name: API call on MERGE
      if: github.event.pull_request.merged == true    
      run: |
        curl "api call on MERGE"

i still wasn't able to test these actions actually but even if they function properly - is there any other, more elegant/smart way to achieve this result?


Solution

  • I think the way I would approach it is to have the PR workflow run whenever there is a change to the PR and then the PR merged workflow would just run whenever there is a push to the base branch (which includes merging the PR).

    name: Push to base
    
    on:
      push:
        branches:
          - base      
    
    jobs:
      api_call_on_merge:
        runs-on: ubuntu-latest
        steps:
        # Steps here!
    

    If you want to specifically run a workflow every time a PR gets merged and not on every push, then the second workflow you had looks correct. There is an example of it in Github's documentation