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:
{
"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:
#!/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 your vercel.json
file or in the project dashboard:
vercel.json
file:
{
"buildCommand": "sh vercel.sh"
}
sh vercel.sh
. This runs the vercel.sh
script created earlier.Was this helpful?