Deploy a Ruby on Rails 7 App on Render for Free

Render is a cloud platform that allows developers to deploy their applications. The best part about Render is that it offers a free plan, which makes it easy for developers to get started. By default, Render automatically deploys GitHub and GitLab projects.

This article will show you how to deploy a Ruby on Rails application on Render. We'll create a basic blog app using Ruby on Rails 7.1.3 and Ruby 3.3.0 and guide you through the deployment process.

Set Up the Application

Let's get started by creating a new application. In the terminal, we type:

rails new my_blog --database=postgresql

We use the --database=postgresql flag because Render offers fully managed Postgresql databases.

When the creation of the app is over, we switch to its directory:

cd my_blog

Then, we create the database:

bin/rails db:create

Next, we generate a Post scaffold:

bin/rails g scaffold Post title:string content:text

Then we run the migration:

bin/rails db:migrate

Let's set the root route to the posts index action. In the config/routes.rb file, add this:

root "posts#index"

Now let's start the server:

bin/rails s

Create a render-build.sh file

The deploy process on Render goes through the following commands: the build command, the pre-deploy command, and the start command. If it is successful, the deployment is complete. If not, the deployment stops, and the remaining commands will not run. The service will continue to run the latest successful deployment.

The build command performs the precompilation of the assets and the installation of the dependencies required for an application to run. The pre-deploy command runs after the deployment but before the service starts. You can use this command for database migrations and similar tasks. But be advised this command is not available for free plans on Render. We will have to run the database migrations from the command line. The start command starts the service. 

To specify the build command, we will create a script file that we will put in the bin directory of our application. Let's call it render-build.sh. Add these lines to this script:

#!/usr/bin/env bash
# exit on error
set -o errexit

bundle install
./bin/rails assets:precompile
./bin/rails assets:clean

Make this script executable by running this command in the terminal:

chmod a+x bin/render-build.sh

Create A render.yaml file

To deploy our application, we will create a render.yaml file at the root of our application. We will add the following content to it:

databases:
  - name: my_blog
    databaseName: my_blog_database
    plan: free

services:
  - type: web
    name: my_blog
    runtime: ruby
    plan: free
    buildCommand: "./bin/render-build.sh"
    # preDeployCommand: "./bin/rails db:migrate" # preDeployCommand only available on paid instance types
    startCommand: "./bin/rails server"
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: my_blog
          property: connectionString
      - key: RAILS_MASTER_KEY
        sync: false
      - key: WEB_CONCURRENCY
        value: 2 # sensible default

Render will create a free database and a free web service. We specify the buildCommand by instructing Render to use the script we added in the previous step. Render will utilize the database URL and application master key.

Push Your Application'S Code to GitHub

To authenticate with GitHub, you need a token. Follow the steps on this page to create one. 

Now, let's create a new repository on GitHub. We will call it my_blog. Next, we will push our application's code to this repo. In the terminal type:

git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/<your-username>/my_blog.git
git push -u origin main

When prompted, type in your user name. When asked for a password, paste your token.

Set Up Render Deployment through a Blueprint

Navigate to the Render Dashboard and go to the Blueprint page. Click on New Print Instance to initiate the setup process. Click Connect GitHub and proceed to Authorize Render to connect your GitHub account and Render. After authorization, click Install to complete the installation of Render.

After selecting the repository for your Blueprint instance, click Connect to establish the connection between your GitHub repository and the Blueprint. Then, give a distinctive name to your Blueprint instance for easy identification in the Render dashboard. At the bottom of the page, you must enter the value of your Rails master key. You can find this key in the config/master.key file of your application. Once you have done this, click Apply to initiate the deployment process. Be patient; the deployment process may take a while to complete.

Run the Migrations in Production

Navigate to your Render Dashboard and select your database service. We'll be editing Rails credentials to include necessary database information. In the terminal, type the following command to open Rails credentials for editing using the nano editor:

EDITOR="nano" bin/rails credentials:edit

Add the following lines to your Rails credentials, replacing placeholders with your actual external database URL, username, and password. This information is crucial for establishing a connection to your external database on Render

render:
    database_url: <YOUR_EXTERNAL_DATABASE_URL>
    username: <YOUR_DATABASE_USERNAME>
    password: <YOUR_DATABASE_PASSWORD>

In your config/database.yml file, set the production database configuration by referencing the credentials added earlier. This setup ensures that your Rails application can successfully connect to the external database on Render during production:

production:
  <<: *default
  url: <%= Rails.application.credentials.dig(:render, :database_url) %>
  username: <%= Rails.application.credentials.dig(:render, :username) %>
  password: <%= Rails.application.credentials.dig(:render, :password) %>

In the terminal, execute the command above to run migration files specifically in the production environment:

RAILS_ENV=production bin/rails db:migrate

Now, your database schema is updated correctly for the production environment on Render.

Conclusion

Congratulations! You've successfully deployed your Ruby on Rails application on Render. With a streamlined setup and powerful features, Render provides an efficient platform for hosting your web applications.

Post last updated on Feb 2, 2024