~2 min read|
Github actions are a convenient way to embed automation directly into your project’s repository.
The only problem is that my initial script was aware when the branch was
main and so would do something different if it was and when it wasn’t. I wanted that same degree of control.
To do that, I needed a way to target my Github action to only run on “merge to
Unfortunately, that isn’t actually available.
Github has special events for
on: ? [push, pull_request]
Both of which are too generic.
We can narrow it down to just closed pull requests:
on: pull_request: types: [closed]
However, this would mean that the workflow is run even if the pull request is not merged.
For specific actions, we can narrow it down with
github.event.pull_request.merged == true, however for workflows with multiple actions, this would be redundant.
Once a Github Branch Protection rule has been set up to prevent pushes directly to
main, we can run the workflow on pushes to
main since we’ll know that only occurs on a successful merge of pull request:
on: push: branches: - main
And just like that, I have a Github action that only runs on merges to
main - exactly what I wanted!
In fact, this is even better than targeting a pull request event because if it was a pull request, the targeted branch would the branch with the changes, not the target - which means that a workflow may run even if the target isn’t
main. While that may be desireable, it’s definitely not in my case.
Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!