|
| 1 | +--- |
| 2 | +title: App deployment |
| 3 | +--- |
| 4 | + |
| 5 | +We're now going to look at how to get the Sinatra app online, a process known as *deployment*. We're going to deploy the app to [Heroku](heroku.com), which offers easy hosting for a variety of web frameworks - all you need to do is push up a git repository, and they take care of everything else! |
| 6 | + |
| 7 | +You should have set up a heroku account and uploaded your ssh keys for homework. |
| 8 | + |
| 9 | +### Preparation |
| 10 | + |
| 11 | +There are a few files `sinatra_c3s2` that we need if we're going to deploy the app to heroku: |
| 12 | + |
| 13 | +* config.ru |
| 14 | +* Gemfile |
| 15 | + |
| 16 | +`config.ru` contains the lines |
| 17 | + |
| 18 | +{% highlight ruby %} |
| 19 | +require './app.rb' |
| 20 | +run Sinatra::Application |
| 21 | +{% endhighlight %} |
| 22 | + |
| 23 | +These lines are to tell Heroku which file to find your application (`app.rb`) and how to run it (using Sinatra). |
| 24 | + |
| 25 | +The `Gemfile` is a way of specifying which gems your project uses. Heroku needs to know this so it knows which libraries it needs to run your app. The `Gemfile` is also really helpful way of remembering yourself which gems you need and is invaluable when working with others. |
| 26 | + |
| 27 | +{% highlight ruby %} |
| 28 | +# Gemfile |
| 29 | + |
| 30 | +source 'https://rubygems.org' |
| 31 | + |
| 32 | +gem 'sinatra' |
| 33 | +{% endhighlight %} |
| 34 | + |
| 35 | +Along with rubygems, ruby has a tool called bundler. Bundler helps you manage your ruby gems. If you run `bundle install` it will look in the `Gemfile` and install any gems that you don't already have. |
| 36 | + |
| 37 | + $ bundle install |
| 38 | + |
| 39 | + will also create a `Gemfile.lock` which specifies the exact version of the gems (e.g. the sinatra library) that you are using. This means that heroku (and anyone else you're collaborating with using git) now knows exactly which gems you are using and can pick to use the same versions. You need to check this in to your repository: |
| 40 | + |
| 41 | + $ git add --all |
| 42 | + $ git commit -m "Added Gemfile.lock" |
| 43 | + |
| 44 | +### Deploying to Heroku |
| 45 | + |
| 46 | +Once your app is prepared, the first thing you need to do is create a new empty heroku application. To do this you use the `heroku create` command: |
| 47 | + |
| 48 | + $ heroku create |
| 49 | + Creating arcane-gorge-2129... done, stack is cedar |
| 50 | + http://arcane-gorge-2129.herokuapp.com/ | [email protected]:arcane-gorge-2129.git |
| 51 | + Git remote heroku added |
| 52 | + |
| 53 | +You'll see that it created an app for you. In my case the app is called `arcane-gorge-2129` and can be found at http://arcane-gorge-2129.herokuapp.com. It also added a git remote for you. |
| 54 | + |
| 55 | + $ git remote -v |
| 56 | + heroku [email protected]:arcane-gorge-2129.git (fetch) |
| 57 | + heroku [email protected]:arcane-gorge-2129.git (push) |
| 58 | + origin [email protected]:code61/sinatra_project_1.git (fetch) |
| 59 | + origin [email protected]:code61/sinatra_project_1.git (push) |
| 60 | + |
| 61 | +### A bit about git remotes |
| 62 | + |
| 63 | + |
| 64 | +### Deploying |
| 65 | + |
| 66 | +You deploy your repository by pushing to this remote. Heroku will serve whatever is in the `master` branch. Here we push the `master` branch up to the `heroku` remote: |
| 67 | + |
| 68 | + $ git push heroku master |
| 69 | + Counting objects: 11, done. |
| 70 | + Delta compression using up to 2 threads. |
| 71 | + Compressing objects: 100% (6/6), done. |
| 72 | + Writing objects: 100% (11/11), 1.08 KiB, done. |
| 73 | + Total 11 (delta 1), reused 0 (delta 0) |
| 74 | + |
| 75 | + -----> Ruby/Rack app detected |
| 76 | + -----> Using Ruby version: ruby-1.9.3 |
| 77 | + -----> Installing dependencies using Bundler version 1.3.2 |
| 78 | + Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment |
| 79 | + Fetching gem metadata from https://rubygems.org/.......... |
| 80 | + Fetching gem metadata from https://rubygems.org/.. |
| 81 | + Installing rack (1.5.2) |
| 82 | + Installing rack-protection (1.5.0) |
| 83 | + Installing tilt (1.4.1) |
| 84 | + Installing sinatra (1.4.3) |
| 85 | + Using bundler (1.3.2) |
| 86 | + Your bundle is complete! It was installed into ./vendor/bundle |
| 87 | + Cleaning up the bundler cache. |
| 88 | + -----> Discovering process types |
| 89 | + Procfile declares types -> (none) |
| 90 | + Default types for Ruby/Rack -> console, rake, web |
| 91 | + |
| 92 | + -----> Compiled slug size: 25.0MB |
| 93 | + -----> Launching... done, v3 |
| 94 | + http://arcane-gorge-2129.herokuapp.com deployed to Heroku |
| 95 | + |
| 96 | + To [email protected]:arcane-gorge-2129.git |
| 97 | + * [new branch] master -> master |
| 98 | + |
| 99 | +After you push, heroku automatically updates the app and launches it for you. You can now see the app by visiting the url (in this case http://arcane-gorge-2129.herokuapp.com). |
| 100 | + |
| 101 | +{% exercise %} |
| 102 | +Deploy your `sinatra_c3s2` app to Heroku: |
| 103 | +1. Install your bundle to get a `Gemfile.lock`. In your `sintara_c3s2` directory run: |
| 104 | + |
| 105 | + $ bundle install |
| 106 | + |
| 107 | +2. Add your work (and new `Gemfile.lock`) to your repository: |
| 108 | + |
| 109 | + $ git add --all |
| 110 | + $ git commit -m "Added Gemfile.lock" |
| 111 | + |
| 112 | +3. Create a new heroku app: |
| 113 | + |
| 114 | + $ heroku create |
| 115 | + |
| 116 | +4. Push your work to Heroku |
| 117 | + |
| 118 | + $ git push heroku master |
| 119 | + |
| 120 | + (for subsequent updates you should be ok with just `git push heroku`). |
| 121 | +5. Visit the url that heroku provide, to check that your app is running! |
| 122 | +{% endexercise %} |
0 commit comments