Deploying a Rails app on Render
Tags: webdev, rails, beginners, tutorial
Published: 2023-07-04 17:55:57

When it comes to deploying web applications, choosing the right platform can significantly impact development efficiency and overall performance. The Render platform offers a powerful and user-friendly solution for deploying Rails apps. In this tutorial, I'll walk you through a step-by-step guide on how to deploy your Rails + PostgreSQL applications on Render. 

## Creating Build Script
The first step in preparing your Rails app for deployment is to create a custom build script. This script will allow you to define specific steps required to build your application. To create the script, follow these steps:

**Step 1**. Open your Rails project's directory.
**Step 2**. Create a new file named `render-build.sh` inside `bin` directory.
**Step 3**. Paste the following script. *You can also add the necessary build steps, such as installing node dependencies and compiling assets*
```sh
#!/usr/bin/env bash
# exit on error
set -o errexit

bundle install
bundle exec rake assets:precompile
bundle exec rake assets:clean
bundle exec rake db:migrate
```
**Step 4**. To ensure `bin/render-build.sh` script is executable, run the following command in your terminal:
```sh
chmod a+x bin/render-build.sh
```
## Configuring Database and Environment Files
Several configurations need to be updated before deploying your Rails app on Render.
**Step 1**. Update `config/database.yml` with the following changes.
```rb
production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
```
**Step 2**. Open `config/puma.rb` and uncomment the following lines.
```rb
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count

port ENV.fetch("PORT") { 3000 }

environment ENV.fetch("RAILS_ENV") { "development" }

pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

workers ENV.fetch("WEB_CONCURRENCY") { 4 }

preload_app!

plugin :tmp_restart
```
**Step 3**. Enable public file server in `config/environments/production.rb`.
```rb
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? || ENV['RENDER'].present?
```
**Step 4**. Make sure that your project has `config/master.key` file. If you don't have a master key yet, delete `credentials.yml.enc` and run this command to generate a new one.
```sh
EDITOR="code --wait" rails credentials:edit
```

### Setting up PostgreSQL database
Render simplifies the process of setting up a PostgreSQL database for your Rails app. Follow these steps to get your database up and running:
**Step 1**. Log in to your Render dashboard.
**Step 2**. Create a new PostgreSQL database.
**Step 3**. Provide a name for your database. 
**Step 4**. For **database** and **username** values, use the ones specified in `config/database.yml`.
```rb
production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
  database: your_database
  username: your_user
  password: <%= ENV["YOUR_APP_DATABASE_PASSWORD"] %>
```
**Step 5**. Copy the generated **Internal Database URL** and **Password** as you'll need them later.

## Deployment
With your app and database configurations in place, it's time to deploy on Render. 
1. On Render dashboard, click **New** and choose **Web Service**.
2. Select **Ruby** as the environment.
3. In the **Commands** section, specify the build and start commands.
  - *Build* : `./bin/render-build.sh`
  - *Start* : `bundle exec puma -C config/puma.rb`
4. Next, add the necessary environment variables:
  - `DATABASE_URL` : User the **Internal Database URL** you copied earlier.
  - `YOUR_APP_DATABASE_PASSWORD`: Use the **Password** you copied.
  - `RAILS_MASTER_KEY` : Add the key from `config/master.key`
5. Finally, click **Create Web Service** to deploy your application.

## Conclusion
Congratulations! You've successfully deployed your Rails app on the Render platform. I hope you found this step-by-step guide helpful in successfully deploying your own application. If you have any further questions or need additional guidance, feel free to reach out to me.

*Happy Coding!*