Optimize Pull Requests in Travis CI

TCI-Graphics for AdsBlogs (4)

One thing you might want when using Travis or any CI tool is to prevent broken code from getting merged, this makes for quality pull requests and making sure nothing broken gets merged. Let’s take a dive into Pull Requests in Travis.

Branch Protection

Branch protection is something you may not know about, you can create a branch protection rule to enforce certain workflows for one or more branches. So for example we want status checks to pass before merging, let’s take a look in GitHub:

image

This will enforce that the status check pass is required before anything gets merged to a branch.

2 Branches or 1 Branch?

By default in Travis GUI, you’ll see both boxes enabled, this is due to the fact that Travis is running a build on the branch and a build on the pull request:

Screen Shot 2022-01-28 at 12 22 09 PM

Leaving it on will allow you to fully explore all the deploy/branching options we have to offer. What if you want the opposite though? To build on a pull request without redundancy?

Build Pull Requests and Merges to the Master Branch

Assuming you want to build all PRs, something like the following will do the trick, you’ll need to enable both the branch and PR builds on the settings page like we showed above, but have all boxes ticked, 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 conditionals you set it place. Read more about conditional builds here.

Switch this off

If you don’t want this by default in your build (PR’s triggering a build), you can do this in the GUI as well, via:

image

If ON, builds will be run on new pull requests. So the answer would be to turn it off:

image

Conclusion

There you go, you just got an inside look on how flexible pull request management is in Travis! Now, take full advantage of your branches and workflow.

As always if you have any questions, any questions at all, please email me at montana@travis-ci.org.

Happy building!