How to run different build commands depending on the branch name in Vercel?
Storyblok is the first headless CMS that works for developers & marketers alike.
We've recently changed our website deployment process over to our friends at Vercel and were facing the challenge to deploy a preview version of our site using a different
package.json script. So depending on us pushing on
main or any other branch, we wanted to execute a different build task. Let's jump right in on how to achieve that using an additional shell script. We will show this by using our use-case with different build tasks in a
package.json. However, this will work with any other CLI tool you want to execute.
In our package.json we've two different scripts defined such as the two demo scripts below:
The Vercel Project settings
In your Vercel Project settings where you will have something like
npm run build,
next build, or any other build task depending on your project type we're now going to configure custom build task command.
To set-up the
vercel.sh we will navigate in the base folder of our project and create a new file called
vercel.sh. In this shell script, we're now able to access our environment variables. This script allows us to access Custom Environment Variables and also gives us access to so-called Vercel System Environment Variables which we will make us of in our shell script.
Ready to build your Next.js site?
Want to know how to build your site using Next.js and Storyblok deployed on Vercel? Check out our articles about Next.js and get started with your site.
Vercel System Environment Variables
Since it's tough to find the Vercel System Environments, we list them for you below.
|The Git Provider the deployment is triggered from. In the case of GitHub, the value is always github.|
|The origin repository of the app on GitHub. Example: |
|The GitHub organization that owns the repository the deployment is triggered from. Example: |
|The ID of the GitHub repository the deployment is triggered from. Example: |
|The GitHub branch that the deployment was made from. Example: |
|The GitHub SHA of the commit the deployment was triggered by. Example: |
|The message attached to the GitHub commit the deployment was triggered by. Example: |
|The GitHub username belonging to the author of the commit that the project was deployed by. Example: |
|The GitHub name belonging to the author of the commit that the project was deployed by. Example: |