Trigger GitHub Actions Workflow on Pull Request Merge
The Flawed Configuration
In my personal development, I had set up a GitHub Actions workflow to run when a pull request gets merged, as shown below:
name: Some Build
on:
pull_request:
branches:
- main
types:
- closed
jobs:
build:
runs-on: ubuntu-latest
steps:
However, there was a flaw in this configuration: the workflow would also run when the pull request was closed without being merged. I wanted to modify this configuration so that the workflow only runs when a pull request gets merged.
Revised Configuration
GitHub Actions does not have a pull request-related trigger like the merged
event.
Therefore, to ensure the job runs only on merge, I used the if
condition as shown below:
name: Some Build
on:
pull_request:
branches:
- main
types:
- closed
jobs:
build:
if: github.event.pull_request.merged == true # This line
runs-on: ubuntu-latest
steps:
With this setup, the workflow now only runs when a pull request is merged.