Deploy Ruby on Rails on Heroku

in heroku •  4 years ago 

I think every beginner developer at some point learns enough that they would like to share their wonderful application with the world. And share working applications, where users can play with it, not just see source code and GitHub commits.

It’s time to put our apps out there and collect excitement of the users and feedback about uncouth bugs.
First we will talk about heroku and how to host our back-end there. In this case we are going to use ruby on rails API.

Let’s begin!

Local Setup

First we need to install Heroku CLI.

For mac:
brew tap heroku/brew && brew install heroku

For ubuntu:
sudo snap install --classic heroku

Now in your console let’s navigate to our app folder (Assuming that you already install rails and you app ready to go live).

cd path/to/your/app

Inside app folder let’s login to heroku:
heroku login

Next let’s initialize git repository inside the app folder:

git init
git add .
git commit -m "init"

Push to heroku

Assuming that the working tree is clean we are ready to push our app to heroku.

First let’s create heroku app:
heroku create

And let’s push our repo to heroku and wait a bit:

git push heroku master

You can see in your console how heroku deploying your app If there are no errors your app should be deployed.

Run database migrations

Now when our app on heroku let’s run rails migrations.

heroku run rails db:migrate

By the way we can execute any rails command by typing heroku run.

If migrations run successfully our app should be live. We can visit in by typing:
heroku open

If something went wrong we can check logs and see where error occurs on heroku side:
heroku logs

And if you would like to check what was wrong on rails side, just run rails console:
heroku run rails console

Create Procfile

Change the command used to launch your web process by creating a file called Procfile.

In file Procfile write:
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}

And push it up.

.env

.env file is important not only for heroku. In case you need to publish or deploy some code but you also want to hide API keys or other not public information. It’s a good practice to create .gitignore file and add .env to it to keep APIs keys and other environmental variables only locally.

API secret

To use API keys in heroku we need to create Config Variables.
To see you current config vars type this:
heroku config
heroku config:get GITHUB_USERNAME
Set config var:
heroku config:set GITHUB_USERNAME=pavel
Remove config var:
heroku config:unset GITHUB_USERNAME

Conclusion

Heroku is a great service for beginners to deploy apps. It’s free and relatively simple. It allows us to publish our apps and add a live version of it to our resumes.

If you have any questions about deployment, don’t hesitate to contact me.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Very helpful! Thank you!