← Back to Guides

Dynamically run build commands

Learn how to run different scripts based on the environment or branch.

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.

  1. 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"
  2. Create a vercel.sh file in your project's root directory. Use the following conditional logic to run a script according to the environment:

    if [[ $VERCEL_ENV == "production"  ]] ; then
      npm run build:production
      npm run build:preview

    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.

  3. Set your Build Command to use vercel.sh either in your vercel.json file or in the project dashboard:

Last updated on May 18, 2024