How to make sure pull_request action re-runs on changes on master

Hi,

I have a GitHub action that runs CI tests on branches and pull requests. When triggered on a pull_request CI runs on the merge between the candidate branch and the target branch (say: master). Excellent, however: how do I have CI re-run (or at least invalidate the status check status) when the master branch changes? Changes in master may make the CI tests fail, still the PR status check normally remains green even when new commits land in master.

Thanks!

1 Like

@vvug ,

You can try the following steps to set up a workflow on master branch in your repository:

  1. Set the workflow runs on push event.
    on: 
     push:
       branches:
         - master
    
  2. Set up a step to execute two APIs.
    • List pull requests - - using this API to fetch the PR number that master is the base branch of the PR.
    • Update a pull request branch - - using this API to sync the latest changes from master into the source branch of the PR.
      For example:
      - name: Update PR branch
        run: |
          response=$(curl \
          -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
          -H "Accept: application/vnd.github.v3+json" \
          https://api.github.com/repos/${{ github.repository }}/pulls?base=master)
          pr_number=$(echo "$response" | jq '.[].number')

          curl \
          -X PUT \
          -H "Authorization: token ${{ secrets.API_AUTH_PAT }}" \
          -H "Accept: application/vnd.github.lydian-preview+json" \
          https://api.github.com/repos/${{ github.repository }}/pulls/$pr_number/update-branch

In this way, if master is the target branch of an Open PR, every time when you push some new changes to master branch, the workflow is triggered to automatically sync these new changes from master into the source branch of the PR.
When calling the “Update a pull request branch” API, if you want the new changes can trigger the workflows on PR, you need to create a personal access token (PAT) and use it to authenticate.
If using the GITHUB_TOKEN, the workflows will not be triggered after updating the PR branch, because,

When you use the repository’s GITHUB_TOKEN to perform tasks on behalf of the GitHub Actions app, events triggered by the GITHUB_TOKEN will not create a new workflow run. This prevents you from accidentally creating recursive workflow runs.