The vercel CLI deploy command has the option to pass build environment variables, because the build is happening at Vercel. We are trying to do the build on our CI/CD server and using the build command to accomplish this and then use the prebuilt flag for the deploy.
The vercel CLI build command doesn’t have an option to pass environment variables to use during the build. In theory, this should only affect NEXT_PUBLIC_ variables, since the others are accessed at runtime. Is there a recommended way to pass environment variables to the build command?
This would be used to override an environment variable for a Preview deploy.
Local builds have access to local environment variables, so I expect you shouldn’t need to pass them into the command in most cases. You can set different variables for production, preview, development, specific branches, and custom environments in Project Settings. That’s where I usually recommend managing env vars for deployments.
I might be able to give you a better option if you can walk me through the reason you need to override the variable
@amyegan Sorry for taking so long to reply. We have an application we are building that, normally, the preview builds point at an API in a UAT environment. Occasionally, we would like to point a preview build at the APIs DEV environment. The URL for the API is in an environment variable, so we would like to change the value of that variable only for that build.
Note: We are using Azure DevOps, so unable to use Git integration. Basically, we need the equivalent of “specific branches” for the CLI using pre-built deploys (running ‘vercel build’ then ‘vercel deploy --prebuilt’)
The web interface for assigning branch specific environment variables requires that you have a linked git provider, which isn’t possible with Azure DevOps. Would we need to do it with the CLI then? That isn’t the most user friendly.
Is there a way to see the environment variables attached to a deploy? It would be useful for troubleshooting.
You can define custom environments for pre-production workflows. If you’re using the Vercel Deployment Extension for Azure DevOps, it’s best to use the buildEnv property listed in the extension reference. The extension lets you target custom environments. This is the safest path.
If you’re using the CLI for a custom workflow, things can get a bit more tricky. You have some other options to create your own steps tailored to your project and team.