Deploy Ruby on Rails app and PostgreSQL to Render.com with a blueprint
Written by
Juan Gallardo
Published
Last updated
Table of Contents
- 1. Create the deployment config file
- 2. Ensure your
database.yml
file references theDATABASE_URL
environment variable in production - 3. Create your
render-build.sh
script - 3. Go on your Render dashboard and link a new blueprint. Point render to your GitHub repository and add your environment variables
- 5. Grab your web services URLs and create some CNAME records wherever you administer your domains. In my case, DigitalOcean
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:
- Consult Render’s documentation for their database and web services plans and their costs
- Our Rails web service and our CMS both get their database credentials straight from the database we are provisioning at the top of this file. Nifty, huh?
- Un-synced environment variables (
sync: false
) are not populated (or present) in your Render dashboard. You’ll be prompted for them the first time you deploy your blueprint. If you don’t set them at that time, you must remember to set them later - Our Rails app depends on the environment variable
RAILS_MASTER_KEY
to decrypt its credentials. Copy this key from/config/master.key
and paste in your Render dashboard - Our Directus web service gets its code from a public Docker repository
- Unfortunately, it seems like you can’t specify the project to which each service belongs. So you’ll have to create projects to organize your Render resources and move them in once the blueprint is synced and the services are deployed
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