Compilers hate this! (Just kidding, compilers are easy-going.)
For many build systems, you have to do a clean build to be sure you’re getting the correct result, so your CI has to always do a clean build. On the other hand, Bazel is designed so that you never have to do a
bazel clean (if you ever run
bazel clean and get a different answer, please file a bug!).
Thus, to cater to most build tools, CI systems generally run a loop like this:
However, for Bazel, doing a clean build every time wastes a lot of time and resources. For Bazel, the continuous integration system should look like this:
Of course, most build systems actually make this a bit difficult to set up, as you wouldn’t want this kind of configuration for a non-Bazel continuous build! However, Jenkins lets you set it up fairly simply. I’ll go through setting up a Jenkins instance that will build a local codebase every 5 minutes.
First, I’ll make my “source directory:”
$ mkdir jenkins-workspace $ cd jenkins-workspace $ touch WORKSPACE
I’m on OS X, so I installed the Jenkins pkg. When it is done installing, it pops up a web page prompts you to create your first job.
Click on the link and put in a name and select “Freestyle Build”, then hit “OK”.
On the next page, select “Advanced”. Select “Use custom workspace” and put in the directory from above.
Set it up to run every 5 minutes:
Now add the following command as the build step:
This will build all targets in your workspace and then run all tests (so it will find any compile errors as well as test failures). //tools and //third_party contain a bunch of targets that won’t build out-of-the-box, so they’re filtered out using the
-//target syntax (see the “Subtractive Patterns” section under Target Patterns).
Note that you don’t have to do any cleanup after the build, since Bazel never pollutes your source directory with build artifacts. You just want to pull, build, pull, build.
Now hit the “Save” button. It’ll take you back to your dashboard and begin the countdown to the first build. Click on the play button in the rightmost column to test it out (or just wait five minutes).
If you make changes, Jenkins will just build/test those changes:
If you haven’t made any changes, Bazel won’t have to do any work at all:
This can be a huge time saver for large projects.