Workflow to build "merged" PR

Hello,

I am looking for some advice on how to create a workflow that will build a PR with the target branch merged. The general idea is to make sure that if the PR was merged into master it would build and tests would pass etc.

I’ve discovered

on:
  pull_request:
    types: [opened]
    branches:
      - master
jobs:
  build:
    name: Build
    runs-on: macOS-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Build
        run: xcodebuild ...

What I’m trying to do is make sure that master is merged into my PR branch “locally” (within the virtual environment that the action is running on) before building, running tests etc.

I would also like to make sure that this workflow is run whenever a new push to master happens on all open PRs.  So whenever master changes we want to know if open PRs are still ok to merge. Hope this makes sense and apologies if I’m asking newb questions.

If I’m understanding your question correctly, you just need to remove the types condition in the workflow example you posted:

on:
  pull_request:
    branches:
      - master

This will make sure the workflow runs not only when the PR is opened but when new commits are pushed (the synchronize type)

When the pull_request event triggers a workflow the GITHUB_REF is automatically set to the merge branch which means that the default behavior of actions/checkout will be to check out the result of the merge to master (see Pull request event: pull_request)

1 Like

By default, the pull_request event type is not triggered when a PR is closed/merged. You need to explicitly bind your workflow to the closed pull_request event type and add a condition to check if the PR was actually merged (rather than just closed without merging). It should look like this:

on:
  pull_request:
    types: [closed]
branches:
- master

jobs:
  deploy:
    runs-on: macOS-latest
    if: github.event.pull_request.merged

    steps:
    - name: Checkout
      uses: actions/checkout@v1

    - name: Build
      run: xcodebuild ...

The key lines here are the types: [closed] one and the if: github.event.pull_request.merged one. This combination makes sure the workflow only starts doing something when a PR is merged.

Hi @ignigena 

Your explanation is very helpful. Thank you. If you don’t mind I do have one follow-up question though.

In my original post I said that I also wanted to trigger the workflow when any change was made on master. Actually when any change is pushed to either the topic branch that the pull request was made from OR the target branch (master). I’m not clear whether this is the behavior of ‘synchronize’ or if it’s just when new commits are pushed to the topic branch but not to master.

Hi @oldskool 

Thanks very much for your reply. Perhaps I wasn’t clear enough in my original post but I’m not trying to get the workflow to run after the PR is merged. What I’m trying to do is run the build on what the result would be _if_ the PR was merged without actually merging it. I think @ignigena’s reply that actions/checkout will checkout the results of the merge to master covers what I’m looking for.