Deploying Sinatra app from Github to Heroku

By | 15/10/2014

I recently received a question from one of my readers how to deploy something existing (on Github) to Heroku. He noticed that I was posting most of my code on Github, but he wanted to know how to get it off Github and deploy to Heroku.

In this post, we have created a REST API for our todo app. Let’s take this as an example. Here we go…

The code from that post can be found on Github (https://github.com/wiwa1978/blog-sinatra-todo-postgres-datamapper-structure-json). We use git clone to grab a copy for making some local changes.

wim@wim-mint ~/ $ git clone https://github.com/wiwa1978/blog-sinatra-todo-postgres-datamapper-structure-json.git
Cloning into 'Sinatra-Todo-app-with-Datamapper-using-Postgres-and-JSON'...
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (39/39), done.
remote: Total 43 (delta 0), reused 43 (delta 0)
Unpacking objects: 100% (43/43), done.
Checking connectivity... done.

You will see that a new directory has been created called ‘Sinatra-Todo-app-with-Datamapper-using-Postgres-and-JSON.gitCloning’. Go into that directory and do the following:

wim@wim-mint ~/Sinatra-Todo-app-with-Datamapper-using-Postgres-and-JSON $ heroku create todo-restserver
Creating todo-restserver... done, stack is cedar
http://todo-restserver.herokuapp.com/ | git@heroku.com:todo-restserver.git
Git remote heroku added

To add the postgres database, do the following

wim@wim-mint ~/Sinatra-Todo-app-with-Datamapper-using-Postgres-and-JSON $ heroku addons:add heroku-postgresql
Adding heroku-postgresql on todo-restserver... done, v4 (free)
Attached as HEROKU_POSTGRESQL_ONYX_URL
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pgbackups:restore.
Use `heroku addons:docs heroku-postgresql` to view documentation.

You also see in the previous CLI output, that Heroku makes available the database location in the environment variable “HEROKU_POSTGRESQL_ONYX_URL”. We can now change the database location in our application’s main.rb file:

configure :production do
  DataMapper.setup(
    :default,
    ENV['HEROKU_POSTGRESQL_ONYX_URL']
  )
end

This will tell our application to store all todo items in the Postgres database located at the URL as specified in the environment variable ENV[‘HEROKU_POSTGRESQL_ONYX_URL’].

wim@wim-mint ~/Sinatra-Todo-app-with-Datamapper-using-Postgres-and-JSON $ git push heroku master
Initializing repository, done.
Counting objects: 47, done.
Compressing objects: 100% (43/43), done.
Writing objects: 100% (47/47), 88.46 KiB | 0 bytes/s, done.
Total 47 (delta 3), reused 0 (delta 0)

-----> Ruby app detected
-----> Compiling Ruby/Rack
-----> Using Ruby version: ruby-2.0.0
-----> Installing dependencies using 1.6.3
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
       The source :rubygems is deprecated because HTTP requests are insecure.
       Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not.
       Fetching gem metadata from http://rubygems.org/.........
       Installing addressable 2.3.6
       Installing fastercsv 1.5.5
       Installing json_pure 1.8.1
       Installing multi_json 1.10.0
       Installing stringex 1.5.1
       Installing uuidtools 2.1.4
       Installing rack 1.5.2
       Installing json 1.8.1
       Using bundler 1.6.3
       Installing tilt 1.4.1
       Installing bcrypt-ruby 3.1.1
       Installing data_objects 0.10.14
       Installing rack-protection 1.5.3
       Installing dm-core 1.2.1
       Installing dm-aggregates 1.2.0
       Installing sinatra 1.4.5
       Installing dm-constraints 1.2.0
       Installing dm-migrations 1.2.0
       Installing dm-serializer 1.2.2
       Installing dm-timestamps 1.2.0
       Installing dm-transactions 1.2.0
       Installing dm-types 1.2.2
       Installing dm-validations 1.2.0
       Installing dm-do-adapter 1.2.0
       Installing sinatra-flash 0.3.0
       Installing datamapper 1.2.0
       Installing do_postgres 0.10.14
       Installing dm-postgres-adapter 1.2.0
       Your bundle is complete!
       Gems in the groups development and test were not installed.
       It was installed into ./vendor/bundle
       Bundle completed (8.50s)
       Cleaning up the bundler cache.

###### WARNING:
       You have not declared a Ruby version in your Gemfile.
       To set your Ruby version add this line to your Gemfile:
       ruby '2.0.0'
       # See https://devcenter.heroku.com/articles/ruby-versions for more information.

###### WARNING:
       No Procfile detected, using the default web server (webrick)
       https://devcenter.heroku.com/articles/ruby-default-web-server

-----> Discovering process types
       Procfile declares types -> (none)
       Default types for Ruby  -> console, rake, web

-----> Compressing... done, 13.7MB
-----> Launching... done, v6
       http://todo-restserver.herokuapp.com/ deployed to Heroku

To git@heroku.com:todo-restserver.git
 * [new branch]      master -> master

Going to http://todo-restserver.herokuapp.com/ will result in a “500 Internal Server Error”. This was somehow expected as we still need to create the database on Heroku:

wim@wim-mint ~/Sinatra-Todo-app-with-Datamapper-using-Postgres-and-JSON $ heroku run rake migrate
Running `rake migrate` attached to terminal... up, run.5342
The source :rubygems is deprecated because HTTP requests are insecure.
Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not.

If you now go to http://todo-restserver.herokuapp.com/, you will get the []. Again this was expected, as the database was just created and there are no records inserted yet.

In some of the next posts, we will create a REST client that consumes this REST webservice on http://todo-restserver.herokuapp.com.

Keep reading!