How to Deploy Rails API to Heroku (ruby-2.6.1) (PostgreSQL)

raaynaldo

Raynaldo Sutisna

Posted on February 21, 2021

How to Deploy Rails API to Heroku (ruby-2.6.1) (PostgreSQL)

Introduction

I always have a problem with production, and I hate it. However, it is really important to host our project. After I finished my Flatiron class for 15 weeks, I decided to host my projects and created this tutorial. I hope it will be helpful.

GitHub logo raaynaldo / herokuy-deploy-test

learn how to deploy rails API to heroku

Add Procfile and Procfile.dev in Rails Project

Create Procfile and Procfile.dev inside the root folder

# root/Procfile
web: bin/rails server -p ${PORT:-5000} -e $RAILS_ENV
release: bundle exec rails db:migrate
release: bundle exec rails db:seed
# db:seed is optional, if you use db:seed it will reseed data every time you push)
# add any other commands
Enter fullscreen mode Exit fullscreen mode

procfile

Push the changes.

Install Heroku CLI

Follow this documentation.

Create New App

Run this command in inside rails folder

heroku create --stack heroku-18
Enter fullscreen mode Exit fullscreen mode

I'm using heroku-18 stack because my ruby version is ruby-2.6.1. if your ruby version is supported for heroku-20, you do not need this command.

Change the Heroku app name (optional)

heroku apps:rename herokuy-deploy-test
Enter fullscreen mode Exit fullscreen mode

Using credentials.yml / master.key (Optional)

If you are using master.key, you should set it up to the Heroku. master.key will not push to the repository because it includes to the .gitignore.

heroku config:set RAILS_MASTER_KEY=`cat config/master.key`
Enter fullscreen mode Exit fullscreen mode

Source

Go to Heroku Dashboard

Heroku App will be available.
heroku-dashborad

  • Click the new app and choose the deploy section.
  • Connect with your GitHub repository. connect-github
  • Turn on Enable Automatic Deploys enable-automatic-deploys
  • Choose a branch to deploy, and Deploy Branch. deploy-branch

Try my Rails API

Bonus: Set origin CORS dynamically (on Development or on Production)

It will be very helpful, so we do not need to change the cors whenever we are in production or development.

# config/environments/development.rb
Rails.application.configure do
    ...

    # cors origns
    config.allowed_cors_origins = "*"
end
Enter fullscreen mode Exit fullscreen mode
# config/environments/production.rb
Rails.application.configure do
    ...

    # cors origns
    config.allowed_cors_origins = "front-end link without http://"
    # ex: config.allowed_cors_origins = "netlikuy-deploy-test.netlify.app"
end
Enter fullscreen mode Exit fullscreen mode
# config/initializers/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
    allow do
        origins Rails.application.config.allowed_cors_origins

        ...
    end
end
Enter fullscreen mode Exit fullscreen mode

Deploy your React App?



Please leave a comment below, and let me know if you need help:)

💖 💪 🙅 🚩
raaynaldo
Raynaldo Sutisna

Posted on February 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related