Deploy Ruby on Rails app and PostgreSQL to Render.com with a blueprint

Written by
Juan Gallardo
Published
Last updated

Table of Contents

I’m assuming you have an account with Render. I’m deploying straight to production for this guide, but I recommend deploying to a staging environment instead. You can always deploy to a subdomain of yours and change the domain to production when you’re ready.

1. Create the deployment config file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
databases:
  - name: <APP_DB_NAME>
    databaseName: <APP_DB_NAME>
    region: oregon
    user: <APP_DB_NAME>
    plan: basic-256mb

services:
  - type: web
    name: <APP_RAILS_NAME>
    domains:
      - <YOUR_VALUE>
    runtime: ruby
    plan: starter
    region: oregon
    buildCommand: "./bin/render-build.sh"
    startCommand: "bundle exec rails server"
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: <APP_DB_NAME>
          property: connectionString
      - key: RAILS_MASTER_KEY
        sync: false
      - key: WEB_CONCURRENCY
        value: 2 # sensible default
  - type: web
    name: <APP_CMS_NAME>
    runtime: image
    env: docker
    plan: starter
    region: oregon
    image:
      url: docker.io/directus/directus:latest
    healthCheckPath: /server/health
    envVars:
      - key: DB_CLIENT
        value: pg
      - key: DB_CONNECTION_STRING
        fromDatabase:
          name: <APP_DB_NAME>
          property: connectionString
      - key: ADMIN_EMAIL
        sync: false
      - key: ADMIN_PASSWORD
        sync: false
      - key: SECRET
        generateValue: true
      - key: PUBLIC_URL
        value: <YOUR_VALUE>

Explanation:

2. Ensure your database.yml file references the DATABASE_URL environment variable in production

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# /config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  primary: &primary_production
    <<: *default
    url: <%= ENV['DATABASE_URL'] %>
  cache:
    <<: *primary_production
    database: app_production_cache
    migrations_paths: db/cache_migrate
  queue:
    <<: *primary_production
    database: app_production_queue
    migrations_paths: db/queue_migrate
  cable:
    <<: *primary_production
    database: app_production_cable
    migrations_paths: db/cable_migrate

3. Create your render-build.sh script

1
2
3
4
5
6
7
8
# /bin/render-build.sh
# exit on error
set -o errexit

bundle install
bundle exec rake assets:precompile
bundle exec rake assets:clean
bundle exec rake db:migrate

5. Grab your web services URLs and create some CNAME records wherever you administer your domains. In my case, DigitalOcean