git push heroku master

Heroku is the go-to platform for deploying mvp web apps, portfolio projects, and code experiments for junior web developers because it’s free and fairly simple to setup. Previously, I deployed by first web app built on Sinatra to Amazon’s EC2 cloud and interestingly I didn’t find it anymore complicated than deploying on Heroku. I suppose the biggest difference in my mind was that Heroku’s documentation is much more extensive, comprehensive, up-to-date, and user-friendly when compared to Amazon’s. Heroku has so plentiful documentation that you’re likely to find the answer to any issue you run into directly on their site rather than having to reference StackOverflow. Perhaps this is the reason it’s seen as the primary platform for new developers.

The Prelude

  1. Accept that Heroku doesn’t work with sqlite and that you’ll need to change your DB to one that Heroku supports. Postgres seems to be the most popular for newbies and it’s fairly simple to move to.
  2. Figure out how to install Postgresql on your local machine.
  3. Make sure you can access your Postgres installation via your command line. (Note: Like most cli apps the commands vary greatly depending on your OS – with a little Googling you should be able to get started quickly.)
  4. Remove the pre-installed gem ‘sqlite3’ from your Gemfile and replace it with this gem ‘postgresql’ and of course be sure to bundle install. If you didn’t get Postgres installed on your machine properly you’ll get an error during bundling where the newly added ‘postgresql’ gem failed to install.
  5. Go to your /config folder and find the database.yml file. Here you’ll change the line with adapter to adapter: postgresql
  6. Now you need to create a Heroku account and confirm your email after signing up.
  7. Install/login to the Heroku account you created via your command line terminal.

Let’s Try to Deploy

DOWNLOAD HEROKU CLI

Choose your download format here.

LOGIN TO YOU HEROKU ACCOUNT VIA CLI

Simply run this command in your terminal to login. heroku login You’ll be prompted to enter your Heroku account email and password. After you’ve successfully logged in navigate to the local version of your app directory – assuming you’ve already cloned your git repo. Next create your Heroku app by running heroku create  in your terminal.

ENSURE YOUR BUILDPACK IS CORRECT

After creating your new Heroku app make sure you’ve set the build pack. Otherwise, you’ll run into issues.

heroku buildpacks:clear


heroku buildpacks:set heroku/ruby

ENSURE MASTER IS UP-TO-DATE

If you don’t do this you’ll like run into a “Remote Rejected Master” error when trying to deploy later. Always make sure your local repo is up-to-date with your remote repo!

git add .
git commit -m "updated something"
git push

FORCE SSL

Are you forcing https? In your /config/environments/production.rb make sure you’re forcing ssl as required by Heroku.

Rails.application.configure do
    config.force_ssl = true
end

NOW TRY TO DEPLOY

Then push your app to Heroku via git push heroku master

Debugging Deploy Issues

HEROKU LOGS

Your first mvp for debugging why your Heroku app you just pushed isn’t behaving properly. Carefully read any error codes and actually figure out exactly what they mean so that you can fix them.

heroku logs --trace

PRECOMPLILE ERRORS

Getting a “Heroku upload-Precompiling assets failed error” or notice asset:precompile error in your Heroku logs while trying to deploy? Your app is crashing at compile which could mean a missing parenthesis or a whole host of other syntax errors could be the culprit here. Best way to find out is to checkout the stack trace by running this first:

heroku run rails console

Still getting precompile errors? Go to your /config/application.rb and ensure you’ve added this line.

config.assets.initialize_on_precompile = false

“SOMETHING WENT WRONG” ERRORS

You tried deploying but are getting a “Something went wrong” error. Yet your Heroku logs don’t tell you anything.

heroku run rake db:migrate

ENVIRONMENT VARIABLES

At first I was a bit confused by the Heroku documentation because it seemed to mention two different types of global variables. However, if you see any mention of ‘Config Variables’ or ‘Environment Variables’ they’re actually the same thing. If your app uses secure logins or external APIs you’ll need to move your local environment variables from your .env file directly to the cloud server. The easiest way to do so is through the Herkou dashboard. Navigate to your app in the dashboard and navigate to ‘settings’ then click on ‘Reveal Config Vars’ button to display all of your secret environment variables. At this point you’ll want to simply add your .env stored variables here. NOTE: Don’t forget to change your callback urls if you’re using OAuth to login.

DEPLOYING A NEW VERSION

First, you’ll want to compile your assets so that your css and asset changes appear properly. Run this in your terminal while within your app directory:

rake assets:precompile

Next, commit your changes to GitHub as normal.

Then you’ll login to Heroku via the command line terminal using:

heroku login

Last but not least, push the changes to Heroku

git push heroku master