February 18, 2018
•Last updated November 5, 2023
Let's Build: With Ruby on Rails - Deploying an App to Heroku
Deploying a Rails app can be a daunting experience. There are a vast number of hosts out there but many fail to compare to Heroku. In this single part screencast, I guide you through the process of deploying an app to Heroku.
Projekt
This article and video carry over from a previous Let's Build called Let's Build: With Ruby on Rails - Project Management App I did a while back called Projekt. You can find that blog post here as well as all the videos.
Key things to remember
Databases
Rails apps often default to sqlite
for the database. In terms of scalability and performance, you might want to opt for a different type of database for your production based apps. Luckily getting your app working in a completely new environment is quite easy with enough know how. In this guide, I opt for postgresql
as recommended by Heroku directly.
Following Heroku's own written guide can get you extremely far if you are brand new to the process.
You can run postgresql
locally using the command line utility or using an app like Postgres (my preferred method). You still need to know some basic postgresql
commands to get up and running. This typically involves creating new roles and or databases manually. I referenced a Digital Ocean article to help with my own efforts. There are other articles in their tutorial section that can also shed some light on the subject. Digital Ocean is also a fantastic and affordable host but there's a lot more manual maintenance and setup here. If you're new I'd suggest at least starting with Heroku. Although a little daunting at first I found it the easiest to get up and running with.
Git
You have to use git when working with Heroku. The deployment process depends on a git repo to push your final production code to the remote app of your choosing.
Heroku CLI (Heroku Toolbelt)
This is probably the most comprehensive CLI I've used which gives you full control of your Heroku account via the command line. You can create apps, modify apps, deploy, configure, and more. Using the CLI as part of your deployment workflow saves a massive deal of time. Combined with git
you can deploy to any type of environment as you wish by adding more than one git remote
.
Useful gems
Environment variables are a set of keys and values of which you don't want to necessarily be advertised as public. These can often be logins, keys, ids, and more. For example, on the app, I'm building I use Stripe as a payment provider. I have different keys for both my production environment and development environment in order to test transactions without actually spending real money to do so. You can apply these manually by saving them down on your local system to something like your .bash_profile
file but I've found a gem called Figaro to be much more easy to implement and scale. The gem adds a YAML
(application.yml
) file to your config
folder and appends it to your .gitignore
file because you definitely don't want to commit the contents to version control.
Using Figaro is quite simple. Be sure to add the gem to your Gemfile
and then run bundle install
.
From there run the following script:
bundle exec figaro install
This creates the application.yml
file, as I mentioned earlier and also, adds that same file to your .gitignore
file
From there you can add keys as you please for your different environments:
# config/application.yml
pusher_app_id: "2954"
pusher_key: "7381a978f7dd7f9a1117"
pusher_secret: "abdc3b896a0ffb85d373"
production:
pusher_app_id: "5112"
pusher_key: "ad69caf9a44dcac1fb28"
pusher_secret: "83ca7aa160fedaf3b350"
With your credentials supplied you can commit all your changes with git
and finally add the production keys all at once rather than one-by-one.
$ figaro heroku:set -e production
Documentation is king
While my guide could be helpful there's much much more to know about deployment and remote servers as compared to how we've been handling our apps locally. Mail service configuration is crucial if you're sending emails. I'd recommend SendGrid here for any transactional emails your app sends. Heroku offers a handy one-click addon to add this your app.
Optimizing your app to be fast and responsive should also be a focus. Caching in Rails, for instance, is an important thing to think about as your application grows. I've yet to discuss this topic but plan to cover it in the near future. Ruby on Rails is a large framework to learn, I hope you'll join me as I continue to discover more and more. Until next time, peace;
Shameless plug time
I have a new course called Hello Rails. Hello Rails is modern course designed to help you start using and understanding Ruby on Rails fast. If you're a novice when it comes to Ruby or Ruby on Rails I invite you to check out the site. The course will be much like these builds but a super more in-depth version with more realistic goals and deliverables. View the course!
Follow @hello_rails and myself @justalever on Twitter.
Categories
Collection
Part of the Let's Build: With Ruby on Rails collection
Products and courses
-
Hello Hotwire
A course on Hotwire + Ruby on Rails.
-
Hello Rails
A course for newcomers to Ruby on Rails.
-
Rails UI
UI templates and components for Rails.