How to prevent unnecessary Vercel builds

This article was published on Jul 15, 2021, and takes approximately a minute to read.

The website you're reading this post is part of a TS monorepo.

I won't dive much into the details of it but I have inside the same repo:

  • CMS that runs on Heroku;
  • A public package for handling MDX via rehype;
  • lambda functions that run on Netlify;
  • This website that runs on Vercel.

One thing that bothers me a lot is is when I change something in a lambdas project which is not related to my website but since my website is connected via git at Vercel, it triggers a build pipeline.

But then, I've realized that Vercel offers an option to decide what can cause a build trigger.

Settings: Ignore Build Step

Under Your project > Settings > Git, we can find an option called Ignore Build Step.

Git settings at Vercel's project config
Git settings at Vercel's project config

It's a single input field where we can specify a custom git diff command.

Based on the input placeholder, it seems Vercel uses the following command to decide rather if something has changed or not:

git diff --quiet HEAD^ HEAD ./

I'm not a git specialist but I was pretty sure we could ignore certain files on that. Doing some search of how to ignore folders/files using `git diff` and it's simple.

At the end of the same command Vercel uses, we can add :(exclude)<path-to-folder-to-exclude

git diff --quiet HEAD^ HEAD ./ ':(exclude)apps/lambdas'

And that's it.

Tip: We can also concatenate more folders by adding the same expression after each other like '(exclude)apps/lambdas' '(exclude)apps/cms' ...

Now, every time I open a PR to fix something in my lambdas code, Vercel will creates a build for that but it'll cancel it right after.

Other useful way is by just specifying the folder you want to listen for changes:

git diff --quiet HEAD^ HEAD ./apps/website

The downside is that if something has changed on root level (like global packages) it won't also trigger the build, however this could be solved by combining linting/building via Github Actions or any other CI pipeline.