Trigger a workflow when the base reference of a pull request is updated

Hi,
I’d like to trigger a workflow every time that there is a code change on a pull request
Using the following trigger

name: run-tests
on:
  pull_request:
    branches: master
    types: [synchronize, opened, reopened, ready_for_review, unlabeled]

using the synchronize event, the workflow is correctly triggered every time that a new change is pushed, but not when I rebase my branch

Which is the correct event to use to trigger the workflow when the base reference is updated?

Thanks a lot!

@elisa-dc,
You need to setup a workflow runs on push event in the base branch.
For example, the base branch is master in your case, you can setup a workflow like as below in the master branch.

on: push

OR

on:
  push:
    branches:
      - master

NOTE:
This workflow in the master branch is essentially not related to the PR, the jobs in the workflow run will not become the parts of the PR’s checks.

@brightran thanks a lot for your reply.

I’ve tried the solution that you suggest, but it doesn’t look to be working to cover my specific use case:

  • I have a branch x on which I’ve opened a pull request to master
  • With the solution that you suggest I can see the push event and my workflow triggered on master every time that a new change is pushed there

My PR has been approved but in the meanwhile new commits have been merged on master

Since we use a rebase strategy I might have the following situations:

  1. I rebase the branch locally and then I push => covered by the on synchronised trigger
  2. I rebase triggering an Automatic rebase workflow (https://github.com/marketplace/actions/automatic-rebase) => don’t see any synchronised event
  3. I rebase using the Rebase&Merge button on my PR => don’t see any synchronised event

@elisa-dc,

when branch x is rebased (case branch x was behind master) to a newer version of master

What is the meaning of “rebased to a newer version of master”? How do you do that?
Do you mean after pushing some new commits to master, you will merge these new changes master into branch x? Any misunderstandings, feel free to tell me.

Here I have some suggestions may be helpful to you:

  1. Navigate to your repository’s Settings > Branches > Branch protection rules > “Add rule” button to set protection rules for master branch.
  2. Enable “Require status checks to pass before merging” and “Require branches to be up to date before merging” options, and mark some checks as Required.
  3. After above settings, when a PR is opened to merge into master from another branch, if push some new changes to master, then you can see the “Update branch” button appears on the PR page. Click the “Update branch” button to merge the latest changes from master into the source branch of the PR (branch x in your case).
  4. Because have new changes pushed to the source branch via the “Update branch”, so the pull_request.synchronize event will trigger the workflow to run. In this way, you do not need to setup a workflow runs on push event in master branch.

@brightran thanks again for your reply. I’ve added some more information to my previous post in order to try to provide additional details on the unclear points. Using the “Update branch” indeed I see a synchronise event and the workflow triggered. Not sure if and which event is triggered in case of Rebase&Merge if our repo use a rebase strategy

@elisa-dc,

Essentially, we can think of Rebase&Merge is pushing some new commits from a branch to another branch (other merge types are similar). This is a push event for the target branch.
If you setup a workflow run on push event in the target branch, after Rebase&Merge, normally this workflow will be triggered.

Another situation, if you want a workflow triggered to run on the pull_request event after Rebase&Merge, you can try using pull_request.closed event. You need to add this event to the workflow in the source branch before Rebase&Merge.

on:
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review, unlabeled, closed]
    branches:
      - master

When a PR is merged, it is closed automatically. Then the pull_request.closed event triggers the workflow.
However, you need to note that the pull_request.closed event is only evaluated for closing the PR, not for merging PR. If you manually close a PR without merged, this event still triggers the workflow.