I’m setting up our render.yaml and so far, haven’t run into any problems. That said, I’m struggling conceptually with how I might set up a preview environment given the existing spec.
Scenario - Let’s say that I want to run a separate managed DB in our preview environment that persists over time (rather than building and being torn down each time a PR is closed).
So my question is… How do I replace this value in a preview environment? Ideally, here is how I would expect this to work:
envVars:
- key: MY_DB_CONNECTION_STRING
fromDatabase:
name: prod-db
property: connectionString
# This is not valid render.yaml syntax, but is what I'm looking for
previewOverride:
name: preview-db
property: connectionString
Is there a way to achieve something like this already? Or should I just create an entirely new env variable for preview environment and throw it in an env group?
Unfortunately, right now, there’s no way to have preview-specific configuration for environment variables that isn’t defined directly in the render.yaml like the example you linked. One workaround I can think of is that you could have two env vars, one for prod and one for staging and then programmatically select between them in your application (I know this is not the most ideal situation). We have an open internal issue to track this type of request as it comes up quite often.
Thank you for the response! I figured this might have been the case; just wanted to confirm. The 2 variable solution works fine and I’ll be looking out for updates to the spec
Really loving the Blueprints options so far, looking forward to seeing where you guys take it!
For future readers, here is what the “2 variable” solution might look like:
And then in your app file that connects to the database:
// dbConfig.js
let dbString;
if (process.env.NODE_ENV === 'development') {
dbString = process.env.YOUR_LOCAL_DB_STRING;
}
if (process.env.NODE_ENV === 'production') {
// If IS_PULL_REQUEST === true, we know we are in a preview environment
// See - https://render.com/docs/environment-variables
if (process.env.IS_PULL_REQUEST) {
dbString = process.env.PREVIEW_DB_URL;
} else {
dbString = process.env.PROD_DB_URL;
}
}
const dbConnection = dbClient.connect(dbString)