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.shfile in your project's root directory. Use the following conditional logic to run a script according to the environment:You can adjust this to use any other System Environment Variables on Vercel. For example, you can usevercel.sh#!/bin/bashif [[ $VERCEL_ENV == "production" ]] ; thennpm run build:productionelsenpm run build:previewfiVERCEL_BRANCH_URLto run a script according to the branch name. - Set your Build Command to use
vercel.sheither in yourvercel.jsonfile or in the project dashboard:
- Using a
vercel.jsonfile:vercel.json{"$schema": "https://openapi.vercel.sh/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.shscript created earlier.