Skip to content

Instantly share code, notes, and snippets.

@hchood
Created December 6, 2014 22:43
Show Gist options
  • Save hchood/8a124723cff7cca4fd8f to your computer and use it in GitHub Desktop.
Save hchood/8a124723cff7cca4fd8f to your computer and use it in GitHub Desktop.
Deploying SInatra apps to Heroku

Deploying Sinatra apps to Heroku

Pre-requisites:

  1. Git repo in your app

  2. Create account on heroku.com

  • Add your SSH key to Heroku so you can push to your heroku remote repo. Hopefully you already have an SSH key for GitHub, in which case you can run the following:
    $ heroku keys:add
    Found the following SSH public keys:
    1) github_rsa.pub
    2) id_rsa.pub
    Which would you like to use with your Heroku account? 2
    Uploading SSH public key /Users/helenhood/.ssh/id_rsa.pub... done
    
    See here for more info.
  1. Heroku toolbelt installed (& logged in)
  • Download here and install
  • Login on the command line:
    $ heroku login
    Enter your Heroku credentials.
    Email: <your_email>
    Password (typing will be hidden):
    Authentication successful.
    

Overview of Heroku

Heroku runs on Git. We can create an app on heroku and connect it as essentially another remote repository to our local git repo.

Just like we would add a remote github repo to our app like so, we can add a remote repo named heroku that, when we push to it, will update our app on heroku.

STEP 1 - Add a config.ru file

Create a file called config.ru in your app/ directory like so:

# config.ru

require './app' # or './server', if you have a server.rb file instead of app.rb
run Sinatra::Application

STEP 2 - Create a Heroku app

In your app's root directory, run:

$ heroku create
Creating <SOME_RANDOM_WEIRD_NAME>... done, stack is cedar-14
https://<SOME_RANDOM_WEIRD_NAME>.herokuapp.com/ | [email protected]:<SOME_RANDOM_WEIRD_NAME>.git
Git remote heroku added

That will create a new app on your Heroku account and connect it to your local repo. If we run git remote -v (which will list, in long form, all of the remote repos connected to our local git repo), we should now see somthing like:

$ git remote -v
heroku  https://git.heroku.com/<SOME_RANDOM_WEIRD_NAME>.git (fetch)
heroku  https://git.heroku.com/<SOME_RANDOM_WEIRD_NAME>.git (push)

Now we can deploy our app by (1) pushing to our heroku repo and (2) creating our database and running any necessary migrations.

STEP 3 - Push to the heroku remote

$ git push heroku master

This step takes a while to complete.

STEP 4 - Migrate your database

The Heroku toolbelt allows us to run rake tasks like rake db:migrate on our production database. Read more on this here.

The database should already have been created. You just need to migrate:

$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.4761
== 20141205192204 CreateTchotchkes: migrating =================================
-- create_table(:tchotchkes)
-> 0.0851s
== 20141205192204 CreateTchotchkes: migrated (0.0854s) ========================

If you have a seeder file (in db/seeds.rb) that you're using to seed your database, you can run:

$ heroku run rake db:seed
Running `rake db:seed` attached to terminal... up, run.9852

STEP 5 - Check out your app!

Run heroku open to try opening your app in the browser.

Troubleshooting

If you just ran heroku open and you see an "Application Error" message, something has gone wrong! Oh no!

You can run heroku logs to get output about what went wrong. Using the --tail flag will allow you to leave the logs running while you navigate around your site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment