Render.yaml preview environment variables

Hi all,

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).

I see in the documentation an example like this:

envVars:
- key: MY_DB_CONNECTION_STRING
  value: production-string
  previewValue: preview-string

I’m struggling to see the utility in this syntax since I don’t want to expose either of these variables via source control.

What I’m currently doing is this:

envVars:
  - key: MY_DB_CONNECTION_STRING
    fromDatabase:
         name: prod-db
         property: connectionString

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?

1 Like

Hi @zachgoll,

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.

1 Like

Hey @tyler,

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 :slight_smile:

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:

# render.yaml file

previewsEnabled: true

services:
    - type: web
      name: server-app 
      buildCommand: yarn ci:some-build-script
      staticPublishPath: /dist/some-path 
    envVars:
      - key: PROD_DB_URL
        fromDatabase:
          name: prod-postgres # Referencing name from below
          property: connectionString
      - key: PREVIEW_DB_URL
        fromDatabase:
          name: preview-postgres
          property: connectionString 


databases:
    - name: prod-postgres
      databaseName: prod-db 
      user: app_user 
      ipAllowList: [] # Empty array only allows internal connections

    - name: preview-postgres
      databaseName: preview-db 
      user: app_user 
      ipAllowList: [] # Empty array only allows internal connections

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)
1 Like