Search code examples
githubyamlgithub-actions

github actions run a job on pull request merge


I am trying to run a job within github actions workflow with the following condition. My aim is to run the job if a pull request to develop branch is merged. However, when I merge a pull reqest to develop branch, the job is skipped. Can someone pl point me what I may be doing wrong?


on:
  push:
  pull_request:
    branches:
      - develop
      - main
      - test
      - feature
  workflow_dispatch:


and then the condition for the specific job

    if: ${{ github.ref == 'refs/heads/develop' && github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true }}

If I change the heads to base in the above if condition, its still the same result i.e. the job is skipped.


Solution

  • Try this solution

    on:
     pull_request:
       types:
         - closed
    
    jobs:
      if_merged:
        if: github.event.pull_request.merged == true
        runs-on: ubuntu-latest
        steps:
        - run: |
          echo The PR was merged
    

    Also as described here, GITHUB_REF will be of format

    refs/pull/:prNumber/merge

    If you are controlling changes to develop branch only via a PR merge, then why not just have a workflow on any PUSH to develop branch?