Using Render.com to Deploy a Rails App (Student Guide)
These instructions were compiled from Render.com’s existing documentation and resources. You are free to ignore this page and follow directions on Render’s website to deploy your application, if you wish. The instructions and links below have been tested as of March 2023.
Turing is not associated or affiliated with Render.com in any way; this page was created as a reference for students to deploy their applications for the first time.
When should I deploy?
- Deploy to Render (or more generally, the production environment) when your application has reached a new apex in feature availability - in other words, when your
main
branch has been updated to where you are confident that you want it to be available on the internet. - When deploying, Render will take your remote copy, you’ll be able to select which branch (we recommend
main
), and deploy it to your server. So, follow good git practice and only merge tomain
once your team has decided the code quality is good enough to deploy a new version of the app.
Getting Started
-
Create an account on Render, if you haven’t already. (You can use GitHub to create an account to make your sign-in even easier.)
-
Locally in the repository you’ll be deploying, update the secret key before deploying:
- Delete the existing
config/credentials.yml.enc
file. - In Terminal in this repository, run
EDITOR="code" bin/rails credentials:edit
.- You do need to specify
EDITOR="code"
for it to work unless it already appears in your Terminal’s config file, otherwise it may give you errors. - When successful, it will generate 2 files called
credentials.yml.enc
andmaster.key
. Themaster.key
file will be ignored by git - this is normal, you do not want to share this key with other teammates.
- You do need to specify
- Commit and push the change that added the new
credentials.yml.enc
file to your remote.
- Delete the existing
Deploying an Existing Rails App
Create DB Service
- After you sign in, it will take you to your Dashboard.
- Click the button to Create a New PostgreSQL.
- Give the PostgreSQL service a name (recommend to use the same name as your repo for sanity)
- You can keep the Database and User fields blank, they will be randomly generated.
- Change the PostgreSQL Version to 14.
- Keep the Instance Type set to Free, and then click
Create Database
.
- Once the database is created, click the
Connect
dropdown on the top-right, and copy the Internal Database URL. You’ll use this link to connect to the production DB after you create the web service.
Create Web Service
- Click the button to Create a New Web Service. If you’ve connected your GitHub account, it will display some of your repositories that you can connect to for this web service. Click
Connect
next to the repo you’d like to deploy. - Give the service a name (anything, you can use the same name as the repo)
- In the Build Command step, add
rails db:{migrate,seed}
to the end of the string.- Note that this command will be run each time your application is deployed. As such, your
seeds.rb
file should contain{Model}.destroy_all
lines at the top so that running theseed
command in production does not cause duplicate data.
- Note that this command will be run each time your application is deployed. As such, your
- Make sure your Instance Type is Free, then click the
Create Web Service
button. - On the next screen, you’ll need to add 2 Environment Variables, so click Environment.
- Key =
DATABASE_URL
, Value = your PostgreSQL service’s Internal Database URL, from the previous step. - Key =
RAILS_MASTER_KEY
, Value = your repo’s localmaster.key
file.
- Key =
If the steps are all working, it will then attempt to build your application and copy it to its container, this will take several minutes. You can watch the service’s terminal for updates. If the build is successful, it will display ==> Build successful 🎉
then ==> Deploying...
. When this step finishes, you can click the URL of the application displayed at the top of the page to visit your site.
At any time, you can see your applications on Render from a high-level by going to the Render Dashboard.
Troubleshooting
- If the build fails, you will see a message like
==> Build failed 😞
in the dashboard terminal, and it will send you an e-mail notification. Any build errors or deployment issues will be listed in the terminal above this message. - If during your deploy you see a message in Render’s console that says
rails: command not found
, run the following command in your local terminal:bundle lock --add-platform x86_64-linux
. This will allow Render’s server architecture to run your application. - Note that the Free tier of the Web Service does not allow for SSH console, so you can’t use
rails c
on the dashboard unfortunately. - You can read more about the Free tier limitations if you encounter other issues.
- Further troubleshooting documentation can be found on Render’s website.