Back
Devops

How to only deploy on spesific branches with Vercel

1 minute read
#Vercel

I found that its was not straight forward to configure Vercel to only deploy on the main branch so here is a guide and i hope it saves you some time.

Note that in this project i am using Next.js but it should work with most frameworks.

First we need to create a file in the root of out project. You can call it what you want but it has to end in .sh for a bash file. I am going to call mine for script.sh .

In the file you just created paste the code below:

sh

1#!/bin/bash
2echo "VERCEL_GIT_COMMIT_REF: $VERCEL_GIT_COMMIT_REF"
3if [[ "$VERCEL_GIT_COMMIT_REF" == "staging" || "$VERCEL_GIT_COMMIT_REF" == "main" ]] ; then
4  # Proceed with the build
5  echo "✅ - Build can proceed"
6  exit 1;
7else
8  # Don't build
9  echo "🛑 - Build cancelled"
10  exit 0;
11fi

When you have added this we need to tell vercel to use this file in ignore build steps

We do this by going to our vercel project => settings => git and then scroll down to ignore build steps

Command to run out script before building
Command to run out script before building

As you can see in the picture i have written bash script.sh in the command. You can write bash [your file name].sh and then press save.

That's it🥳 You have now configured vercel to only build when a commit is pushed to the main branch!

Thank you for reading this article. If you liked it be sure to leave a 👏

Martin Kulvedrøsten Myhre

Fullstack developer & junior consultant at Inmeta Consoulting AS. CEO and founder of Limeyfy AS and 3Steps AS.