Limit Built Branches on Travis

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet

By default, Travis will build all branches, tags, and Pull Requests. Because we're building our master branch before we release, we don't need Travis building our releases. Also, we don't care to have Travis build all the branches. So we're going to limit Travis to only build our master branch and Pull Requests by configuring travis via our .travis.yml file.

[00:00] If you look at the badges on our ReadMe, we'll see that our build is failing. Let's go ahead and take a look at our build. What's actually failing, is there was a problem with one of the lessons that I deployed, and that build failed.

[00:14] But it's showing up that our build is failing and we don't really want that, the actual master build, the releases, those are all passing, and so what we're going to do is we're going to tell Travis that we only want to build specific branches and we don't want to build any of the lesson branches or anything else, it will be just specific branches that we explicitly mention. To do this, we are going to go ahead and jump into our travis.yml file, and we'll add a branches here with an only, and we'll specify master.

[00:51] So we're only going to build master. Now Travis will automatically continue to build the pull request which is something that we definitely want, so that when somebody makes a pull request we can see that their pull request will merge, and that the build will continue to pass. But we only want to build our master branch, and then we can list other branches and it actually takes regex here.

[01:16] This is a white list, you can do a black list with exclude, but we're just going to do a white list with only, and now all that we need to do is commit this change. So let's look at our Git status, we have that modified file. Let's do Git add and Git status, so now we can run npm run commit and we'll go through our commit is in, this is a chore related to Travis, only build master and PRs because we don't want to waste resources, save the trees.

[02:00] We'll go ahead and add a tree emoji, probably a Christmas tree is good, and we don't have any issues related to this change, so we'll just skip that. Now that all of our commit hooks have passed and everything is worked out, our commit succeeded, now we'll go ahead and push. If we look at our Travis build at the current here in a moment, it will update to what we just pushed. There it is. Save the trees, and it's building.

[02:32] Now we can verify this by creating a branch, let's say Git checkout branch Lesson/23 limit Travis Builds and we'll go ahead and push this branch, specifying origin, lesson 23 and that will get pushed as a new branch to our Git repository. If we look at the build history here, we see that no longer will our lessons be built and that is how you limit Travis from building any extra branches and so now if we go to our repo, and we'll refresh here.

[03:14] The build is now passing, and that is exactly what we want. Just to review things really quick, if we go to our travis.yml we simply add this branches property, we white list our master branch, and anything else that doesn't match this or a regex in this place to be built. That is how you limit Travis from building extra branches.