In this example, you'll create a vercel.sh
file in your repository that your project's Build Command can use to run conditional logic.
-
Set up your scripts in
package.json
. Specify the following example scripts to be run for your preview and production environments:package.json{ "scripts": { "build:production": "next build", "build:preview": "echo \"Let's build a preview!\" && next build" } }
-
Create a
vercel.sh
file in your project's root directory. Use the following conditional logic to run a script according to the environment:vercel.sh#!/bin/bash if [[ $VERCEL_ENV == "production" ]] ; then npm run build:production else npm run build:preview fi
You can adjust this to use any other System Environment Variables on Vercel. For example, you can use
VERCEL_BRANCH_URL
to run a script according to the branch name. -
Set your Build Command to use
vercel.sh
either in yourvercel.json
file or in the project dashboard:
- Using a
vercel.json
file:vercel.json{ "buildCommand": "sh vercel.sh" }
- Using the project dashboard:
Select your project from the dashboard and go to the Settings tab. Under Build & Development Settings, set the value of the Build Command to be
sh vercel.sh
. This runs thevercel.sh
script created earlier.