How can I load different environment variables for preview PRs?

Currently, when I create a new PR and a corresponding preview environment is generated, all of my prod env vars are automatically copied over to the preview env :scream: :scream:

This means I have to remember to go in and update each of them to, for example, use our testing db rather than the prod db and more.

Obviously, this is not desirable and also not sustainable. Worse, there is the risk that I or someone else will forget to change this and then make an update that impacts prod.

Is there a recommended way to set up env vars in render to prevent this? Ideally, I would like for it to work in Vercel, where you can have separate env vars for each environment.

Thanks for any tips or insight!

1 Like

I know you can do this if you are using blueprints, Preview Environments | Render · Cloud Hosting for Developers

Hey,

As Christopher indicated, you could define a “previewValue” via a Blueprint, see https://render.com/docs/preview-environments#environment-variables

Regards.
Jérémy, Render Support

Thanks for that info. However, doesn’t that mean I would have to include an env var such as a database url string directly in my repo? Or am I not understanding. I definitely cannot include api keys and database urls in the checked in code, even for preview envs.

I understand. Currently, we don’t have a straightforward way to accomplish that, but we have plans to implement this feature in the near future. You can find some common workarounds at https://feedback.render.com/features/p/enable-preview-environment-group-override.

Regards.
Jérémy, Render Support

Thanks for that info. Do you have an ETA for when this might be supported? I am at this point unfortunately actively looking for hosting alternatives in part due to this issue.

Sure, I’ll reach out to our product team and will let you know where we are with this. I wonder what you thought about the workarounds though?

Jérémy.
Render Support, UTC+3

Thanks! I read through the workarounds, and maybe I missed it, but I did not see way to at least prevent env vars from being copied over. Also, I tried using the value / previewValue option and was not able to get that to work, ie getting a different value for previews.

Hey,

The situation here is that it is currently not possible to exclude the copying of envGroup values to your PR (Pull Request) environment instances. This can only be achieved using the “sync: false” flag if the environment variables were individually set in your .yaml file, as explained in the workaround.

The suggested workaround, in this case, would be to create two environment groups that are carried over to each environment. However, you can implement logic in your code that retrieves the variables from one group or the other, depending on the environment you want your code to run on (e.g., prod/dev). You can also check if the current instance is a PR environment using the “IS_PULL_REQUEST” environment variable.

Jérémy.
Render Support, UTC+3

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