loading...

Deploying to Heroku: HOW DOES IT WORK

mariel profile image mariel ・5 min read

I just got my Sinatra project from Flatiron deployed to Heroku. FINALLY. We did these back in November/December 2018? idk it was not long after we started.

Y'all. It is February 2020.

WE'RE DOING GREAT.

Anyway: I have never had an easy time deploying to Heroku. Ever. I screw it up six ways from Sunday and then I screw it up some more. BUT I GOT SOMETHING WORKING so I'm going to write down what I did and maybe, MAYBE, the next time I need to get a sqlite3-built app live I won't almost-cry.

Wait - what's a Heroku?

The actual definition on Heroku's website is:

Heroku is a platform as a service based on a managed container system, with integrated data services and a powerful ecosystem, for deploying and running modern apps. The Heroku developer experience is an app-centric approach for software delivery, integrated with today’s most popular developer tools and workflows.

In "I'm new here and need to get my first projects online" - Heroku is a platform that will let you get your apps online quickly, from the command line, and allow you to take your projects from folders or git repos to real, live, working apps.

For more detailed info:

What are we using it for?

We're using it to deploy small, one-off projects that are going to be live demos of what's on our resume/portfolio. They need to be alive and working so recruiters and interviewers can poke around in them, but we expect the volume otherwise to be very low if not zero.

But why?

Because it's easy and it's free. WOO.

Given the lightweight use we plan on, the cost is most likely to be zero. (But even if it's not - there's a "Hobby" level that's probably not prohibitively expensive.)

It's easy (-ish - we're getting to that) to deploy from the command line just by using one command.

How do I use the CLI?

You can install the Heroku Command Line Interface (CLI) via their website.

Since this post isn't about getting Heroku set up, I'm going to refer you to the above link and make a note that if you're a Windows user, you can get Ubuntu running on Windows Subsystem for Linux (WSL). The instructions I used for that are here (ignore Learn/Flatiron-specific instructions, such as installing certain gems, as needed). This matters because you can run the Ubuntu command on the Heroku page if you've got that set up.

LET'S SAY I got everything working, now what?

If you've got Heroku available via the command line, you're used to using git, AND you've got a repo you're ready to send out into the world:

very impressed

For the rest of this post, we're going to assume you're trying to deploy a Rails app, or something else that's using sqlite3 as the database in development. An alternate title for this post could have been:

SQLITE3 GOTTA GO, FAM.

Heroku uses PostrgreSQL as a databse, and gives a pretty good rundown of why. That page also gives detailed info re: using postgres from the beginning of your app build.

If you're like me, you're going to forget this is a problem until it's too late because wow this is why we can't have nice things.

What does my app need then, if I built it with SQLite3?

ALRIGHT. WE ONLY GOT ONE SHOT DO NOT MISS YOUR CHANCE to read the instructions carefully because okay technically you get more than one shot and you can figure all of this out from the error messages you'll get but come on ain't nobody got time for that.

eminem cracks knuckles

You're going to need a procfile

NEVER SMILE AT YOUR NEW PROCFILE NO YOU CANT GET TOO FRIENDLY WITH YOUR NEW PROCFILE

It's fine I'm fine.

A procfile "specifies the commands that are executed by the app on startup." This includes the web server, types of worker processes, and tasks to run before a new release is deployed.

Just add a new file to the root directory called "Procfile" - no extension, same as how you create a Gemfile.

Inside:

web: bundle exec rackup -p $PORT
release: bundle exec rake db:migrate

Organize your gems

Specifically, you're going to need a development group and a production group, since you want Heroku to use different databases in production and development.

However else you organize things, you need to at least do these 2:

group :development do 
  gem 'sqlite3', '~> 1.3.6'
end 

group :production do
  gem 'pg', '0.20'
end 

Add .yml file

Tbh I figured this one out after getting yelled at by some error messages and then looking at projects I knew had worked in the past. That being said, Heroku talks about database.yml but when you google you're going to get a lot of discussion re: .yml and .yaml. It also looks like after Rails 4.1, you won't need to do this. In my instance, I'm using Sinatra with ActiveRecord but just a note if you're reading this and using Rails.

Feel free to splain any of this in the comments since lbh databases and their issues are not my strong suit but pls be nice I AM DOING MY BEST OKAY.

Inside config/, add database.yml with the following:

development:
  adapter: sqlite3
  encoding: unicode
  database: db/development.swlite3
  pool: 5
production: 
  adapter: postgresql
  encoding: unicode
  pool: 5

Finally, update your environment

Inside config/environment.rb, take out the 3 lines re: ActiveRecord and specify that it should use your new database.yml file:

#ActiveRecord::Base.establish_connection(
#  :adapter => "sqlite3",
#  :database => "db/#{ENV['SINATRA_ENV'}.sqlite"
#)

set :database_file, "./database.yml"

YOU DID IT

A reminder: all of the above is because you used sqlite3 in development and Heroku won't use that in production. That part can kind of get lost in the "lol how does this work what is happening pls work."

If the above did what it's supposed to, all you should need to do now is....

drumroll

git push heroku master 

And then you should see your computer do a lot of thinking annnnd confirm the deploy was successful - you can have your Heroku account up at the same time to confirm/poke around in the logs.

THE END

Good job go team. KEEP UP THE GOOD WORK.

https://cdn.dribbble.com/users/3573/screenshots/1317971/acsprungle_go_forth_and_create_2014.gif

Posted on Dec 6 '18 by:

mariel profile

mariel

@mariel

Software engineer! <3 Lawyer! I love dogs, cookies, and learning new things.

Discussion

markdown guide
 

Holy wow - this whole post was entertaining as hell to read. Especially this mood:

Y'all. It is February 2020. WE'RE DOING GREAT.

Had me in tears of joy from how incredible this was. Love your writing style so much!