Search code examples
github-actions

GitHub Actions: How to trigger a workflow on a pull_request event filtered on the name of the merging branch


OK I've seen at least 1 solution on this but it isn't as elegant as it should be IMO so I'm holding out hope that I'm just not understanding the documentation.

Preamble

A pull request involves 2 branches, a "source" and a "destination", if you will. If you look at the top of the pull request on Git Hub you will see two branches with an arrow between them. "Source" will be at the tail of the arrow and "destination" will be at the point of the arrow.

There is a workflow trigger called pull_request that, by default, triggers whenever a pull request is opened wherein the "destination" (the branch at the point of the arrow) is the main/master branch. It is possible to change this such that the workflow would trigger whenever a pull request is opened wherein the "destination" branch is something other than main/master by simply using branches:

Question

How does one configure a workflow to trigger based on the "source" (the branch at the tail of the arrow)?

The solution I have seen (and it works) is to put a conditional on the job, like so

jobs:
  my-job:
    if:  startsWith(github.head_ref, '<source branch name>')

but, as I said, I'm hoping there is actually a way to do this that is equally as elegant as the way it is done for the "destination" branch of a pull request.


Solution

  • It's only possible using the conditional on the job.