Runtime env variables with git branch filter

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:

vercel pull --yes --environment=preview --git-branch=my-feature

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?

Hi, @vitaliypotapov! Welcome to the Vercel Community :smile:

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 .

 vercel env pull .env.production --environment=preview --git-branch=my-featurevercel env pull .env.production --environment=preview --git-branch=my-feature

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 .

 vercel deploy --prebuilt --env-file=.env.productionvercel deploy --prebuilt --env-file=.env.production

You can modify your GitHub Action workflow to include these steps:

 - name: Pull Vercel Environment Information- name: Pull Vercel Environment Information
  run: vercel pull --yes --environment=preview --git-branch=${{ github.head_ref }}

- name: Pull Branch-Specific Environment Variables
  run: vercel env pull .env.production --environment=preview --git-branch=${{ github.head_ref }}

- name: Build Project Artifacts
  run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}

- name: Deploy Project Artifacts to Vercel
  run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }} --env-file=.env.production

This approach should ensure that both build-time and runtime environment variables are correctly filtered by the git branch.

Some relevant guides that may help:

Let us know how you get on!

This does not help, I’m getting an error:

Vercel CLI 37.14.0
Error: unknown or unexpected option: --env-file

@pawlean could you check my response?

Can you remove the option --env-file in your command? Does it work then?

When I remove --env-file option, vercel deploy command works, but it uses incorrect env variables (see my initial message).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.