Home

How to only deploy on spesific branches with Vercel

MK

Martin Kulvedrøsten Myhre

10:00-9/26/2022

Next.js
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

#!/bin/bash
echo "VERCEL_GIT_COMMIT_REF: $VERCEL_GIT_COMMIT_REF"
if [[ "$VERCEL_GIT_COMMIT_REF" == "staging" || "$VERCEL_GIT_COMMIT_REF" == "main" ]] ; then
  # Proceed with the build
  echo "✅ - Build can proceed"
  exit 1;
else
  # Don't build
  echo "🛑 - Build cancelled"
  exit 0;
fi

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

[@portabletext/react] Unknown block type "image", specify a component for it in the `components.types` prop

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 👏