How to configure Travis-CI to build pull requests & merges to master w/o redundancy

I eventually found another GH issue (#2111) which gave me the idea to try enabling both PRs & pushes, but with a whitelist to restrict pushes to a specific branch. This seems to satisfy the criteria for my workflow. Here's what I did:

  1. Enable both PRs & branch pushes in the Travis settings for the repo:

travis push/pr settings enabled

  1. Change .travis.yml to white-list master branch (i.e. only build pushes to master):
branches:
  only: 
    - master
  1. Test it by creating a PR with the .travis.yml change, and another PR with some empty commits to verify it works for forks too.

  2. Verify successful merge commit build from master.

build result of merge to master


Just found in travis docs

Add to .travis.yml

if: type = push

alternatively:

if: type = pull_request

Assuming you want to build all PRs, something like the following will do the trick. Enable both branch and PR builds on the settings page, and put this line as the first line in your travis.yml:

if: (type = push AND branch IN (master, dev)) OR (type = pull_request AND NOT branch =~ /no-ci/)

This will attempt a push build on all pushes and a PR build on all pushes to an open PR, but will filter out any that don't meet the condition. You might need to modify this a bit - the clause about not building branches with no-ci somewhere in their name is obviously optional, and you may not have two branches that you always want to run builds on.

You can read more on conditions and conditional builds on Travis's site.