Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Limit Built Branches on Travis

    Kent C. DoddsKent C. Dodds

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    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.

    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.

    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.

    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.

    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.

    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.

    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.