I use GitHub actions to deploy preview versions with custom git branch (per this guide). Specifically, I provide --git-branch flag when pulling env variables:
After deploying, all build-time env variables are correctly filtered by git-branch=my-feature and inlined into output bundle.
But server-side (runtime) env variables are taken from preview env without filtering by --git-branch=my-feature. Actually, the final deployment is a mix that uses branch-filtered env variables on client and non-branch-filtered variables on server. It leads to bugs in our application and obviously is invalid output.
I know about --env flag for vercel deploy. But it defines only single env variable. After vercel pull I have all my variables in .vercel/.env.preview.local. I don’t see a flag to provide a whole env file to vercel deploy. I can write a script that iterates all variables in env file and passes them as --env arguments. But this is quite weird solution.
Do you have any suggestions how to provide branch-filtered env variables to vercel runtime similar to build-time?
Thank you for providing such detailed information!
Instead of relying on the .vercel/.env.preview.local file created by vercel pull, you can use the vercel env pull command to fetch the environment variables specific to your branch. This command supports the --git-branch flag .
This will create a .env.production file with the branch-specific variables.
After pulling the environment variables, you can use the --env-file flag with vercel deploy to specify the file containing your environment variables .