Branch and conditional deploys in Vercel

Akshay Sura - Partner

24 Feb 2021

Share on social media

Vercel and Netlify have made build and deployments easy, especially in the Jamstack world. I have tried countless offerings to match these two and it is hard to find a replacement.

I prefer Vercel over Netlify for all of our Next.js sites. We noticed a few oddities specific to Next.js on Netlify.

Netlify has an easier UI and lets you control your deployments for pull requests and branch deploys easily.

In Vercel, they approach this issue a bit differently. Before we get into that, let me explain my use case.

We have one repository which powers about 12 websites. We control the look and feel and the functionality available via environment variables and data coming from a Headless CMS. Each website would need a Development, Staging, and Production site based on three branches in the repository.

We created three projects in Vercel running off of each branch for each site. We can configure custom Production branches in Vercel pretty easily.

What happens is that when we check a piece of code in, all the projects attached to the repo get a deployment triggered no matter what branch the code is checked into.

What we want to do is to only build the branch which we want for a specific project. I know my use case is not common but at the same time, a simple do not build branch deploys would have helped.

What we get from Vercel though, lets us implement much more complicated functionality. We are able to run a bash file as part of the deployment and decide if we want to continue the build or cancel it.

Here is a link that might help: How do I use the "Ignored Build Step" field on Vercel?

After setting the custom production branch, I specified the bash command in the Ignore Build Step.

Here are the contents of the Vercel.sh (you could name this file whatever you like).

echo "VERCEL_GIT_COMMIT_REF: $VERCEL_GIT_COMMIT_REF" echo "BRANCH_TO_BUILD: $BRANCH_TO_BUILD" if [[ "$VERCEL_GIT_COMMIT_REF" == "$BRANCH_TO_BUILD" ]] ; then # Proceed with the build echo "✅ - Build can proceed" exit 1; else # Don't build echo "🛑 - Build canceled" exit 0; fi

Set an environment variable with the branch that should build on this project.

Once set up it works like a charm.

If you have any questions, please get in touch with me. @akshaysura13 on Twitter or on Slack.

Follow us on Twitter

Follow us on LinkedIn

Follow us on YouTube

Sign up to our newsletter

Share on social media

Akshay Sura

Akshay is a nine-time Sitecore MVP and a two-time Kontent.ai. In addition to his work as a solution architect, Akshay is also one of the founders of SUGCON North America 2015, SUGCON India 2018 & 2019, Unofficial Sitecore Training, and Sitecore Slack.

Akshay founded and continues to run the Sitecore Hackathon. As one of the founding partners of Konabos Consulting, Akshay will continue to work with clients to lead projects and mentor their existing teams.


Subscribe to newsletter