To put it in \"BDD\" terms:
Background:
Given I\'m contributing to a GH repoWhen I create a pull request
Then Travis shou
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.
Just found in travis docs
Add to .travis.yml
if: type = push
alternatively:
if: type = pull_request
You can use next workflow if you want to test not only master
branch but some others branches too:
Add branches:except
directive to your .travis.yml
:
branches:
except:
- /^pr\..*/
In this configuration:
feature-A
will trigger the buildpr.feature-A
will not trigger the buildpr.feature-A
is used in opened pull request then build will be triggeredwip.feature-A
, any commit to this branch will trigger the buildmaster
you can rename it from wip.feature-A
to pr.feature-A
and open pull requestpr.feature-A
On all the steps above only one build will be triggered.
For one of the repositories, I was working with, here is what I wanted:
There is an origin
repo which is the main repo which does all the releases.
I wanted that all the pull requests coming to master
branch of origin should be built with Travis only once irrespective of the fact that it comes from a forked repo or any other branch of the origin
itself.
For this case, this works like a charm
if: (type == push) OR (type == pull_request AND fork == true)
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:
.travis.yml
to white-list master branch (i.e. only build pushes to master):branches: only: - master
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.
Verify successful merge commit build from master.
The whitelist approach described in the accepted answer has some significant limitations. In particular, it doesn't support non-redundantly building arbitrary branches without opening a PR.
I opened an issue asking for a better solution.