Building with Bazel
Part 1: Learning Bazel Use a Monorepo
— iOS & Swift, Android & Kotlin

Lesson Complete

Play Next Lesson
Next

Use a Monorepo

Learn about a monorepo. Get an idea about the advantages of using a monorepo and then you’ll do a little setup to get your monorepo.

Contributors

Episode 6 - Use a MonoRepo

Typically when creating projects, we store each project in its own repo. This may even include related projects. For instance, you may have one repository for an iOS app and another repository for the android version. This makes sense. Both projects uses different environments and different languages. But Bazel is designed to work with multiple environments. By intelligently organizing our workspaces and build files,

we can store all of our code into a single repository. Believe it or not, this is something Google does with all their code. Now, for the size and scope of Google’s repository, they actually wrote their own source control solution called Piper. But Google isn’t the exceptions. Companies such as Dropbox, Linked and Uber all utilize monorepos in their development process.

So why use a monorepo? By having all your code in one repo, you can analyze code and create shared libraries that all your products in your company can use. This prevents projects creating redundant code and of course, share knowledge across different parts of your company. Dependencies can be also be stored in the repo. In such a case, there’s no need for a package management. This means, all your projects have everything needed for it compile on hand as well as the security of not having to worry about unexpected breaking changes being pushed into the dependency without you knowing about it. And of course, being you have the companies entire codebase in one repository, you can easily collaborate with other teams. Heck, you may even have other teams submitting bug fixes to your project.

Now monorepos aren’t for every situation and may require your team to make adjustments. That said, for the purposes of this course, we’re going to put all our code into a single repo.

Demo

To get started, open up your terminal application and change directories to the Desktop.

cd ~/Desktop

I’m using macOS so the commands may work a little different on your platform. When in doubt, check the documentation.

Create a new folder called BazelBuild.

mkdir BazelBuild

Then navigate into the directory.

cd BazelBuild

This is going to be our monorepo. It’s going to contain all the projects used in this course. Next, we will setup our gitignore. After all, we don’t want to commit any Bazel’s support files into the repository. A good file to use is from the bazel project itself.

Type the following into your browser.

https://github.com/bazelbuild/bazel/blob/master/.gitignore

Click the Raw button. Then select all and copy the file. Now head back to your terminal and bringup gitignore in vim.

vim .gitignore

Press lowercase i to enter insert mode. Paste the contents of your clipboard into the file. The press escape to exit insert mode. Type colon w to write the contents of the file. Then colon q to exit the file.

Okay, find your joke generator java project and copy that into the repository.

Next, copy the mobile apps and also put them in your repository.

Now we have a single repository that contains all our projects. Next, we have to define a few workspaces for our new mobile projects. Unlike the our previous workspace which was just an empty file, these workspaces will need to be configured.

But before you can write a custom workspace, you should have an understanding of the language used to write the workspace. That is, Starlark and it’s coming up next.

Reviews

Comments