|
| 1 | +# Deploying To Heroku |
| 2 | + |
| 3 | +If you have already deployed your app to Heroku, go on to [Deploying to Heroku again]. |
| 4 | + |
| 5 | +## Step 1: Create a Heroku application |
| 6 | +Type this in the bash tab (aka terminal): |
| 7 | +```bash |
| 8 | +heroku create |
| 9 | +``` |
| 10 | +`heroku create` registers a new application on Heroku's system. You should see some output including your new app's URL. |
| 11 | + |
| 12 | +## Step 2: Edit the Gemfile |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +Each application has its own `Gemfile`. Be sure you're opening the one inside your app's folder. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +Heroku will run our application slightly differently than our development computer does, which requires us to make a small change to our `Gemfile`. |
| 21 | + |
| 22 | +Open the file called `Gemfile` in Sublime Text, or your preferred editor, and find the line containing: |
| 23 | + |
| 24 | +```ruby |
| 25 | +gem 'sqlite3' |
| 26 | +``` |
| 27 | + |
| 28 | +__Remove that line__ and replace it with: |
| 29 | + |
| 30 | +```ruby |
| 31 | +group :development, :test do |
| 32 | + gem 'sqlite3' |
| 33 | +end |
| 34 | + |
| 35 | +group :production do |
| 36 | + gem 'pg' |
| 37 | + gem 'rails_12factor' |
| 38 | +end |
| 39 | +``` |
| 40 | + |
| 41 | +## Step 3: Apply the Gemfile changes |
| 42 | +Type this in the terminal: |
| 43 | + |
| 44 | +```bash |
| 45 | +bundle install --without production |
| 46 | +``` |
| 47 | + |
| 48 | +Every time the `Gemfile` changes, you need to run `bundle install` for the changes to be processed. The processed version of the changes is stored in another file called Gemfile.lock. |
| 49 | + |
| 50 | +## Step 4: Commit the Gemfile changes |
| 51 | +There are now changes to `Gemfile` and `Gemfile.lock` that need to be committed before we can push to Heroku. |
| 52 | + |
| 53 | +Type this in the terminal: |
| 54 | + |
| 55 | +```bash |
| 56 | +git add Gemfile |
| 57 | +git add Gemfile.lock |
| 58 | +git commit -m "Changed Gemfile for Heroku" |
| 59 | +``` |
| 60 | + |
| 61 | +## Step 5: Commit any (other) pending changes to git |
| 62 | +Heroku will only receive the files we've committed into our local git repository. So we need to make sure all changed files have been committed. |
| 63 | + |
| 64 | +Type this in the terminal: |
| 65 | +```bash |
| 66 | +git status |
| 67 | +``` |
| 68 | +`git status` shows you any pending changes you've created. If it has no output, you're already ready to deploy! Otherwise... |
| 69 | + |
| 70 | +Type this in the terminal: |
| 71 | +```bash |
| 72 | +git add . |
| 73 | +git commit -m "Some helpful message for your future self" |
| 74 | +``` |
| 75 | +Your commit message should reference whatever your outstanding changes are: something like "Added votes to the topics index". |
| 76 | + |
| 77 | +## Step 6: Push changes to Heroku |
| 78 | +Type this in the terminal: |
| 79 | +```bash |
| 80 | +git push heroku master |
| 81 | +``` |
| 82 | +This takes all changes you've committed locally and pushes them to Heroku. |
| 83 | + |
| 84 | +## Step 7: Run database migrations on Heroku |
| 85 | +Type this in the terminal: |
| 86 | +```bash |
| 87 | +heroku run rake db:migrate |
| 88 | +``` |
| 89 | +This tells Heroku to run your migrations on its database, like running rake db:migrate locally. |
| 90 | + |
| 91 | +Heroku's database is separate from the one on your computer (or the one in cloud9), which means it needs to be updated every time you make changes to the structure of your database. |
| 92 | + |
| 93 | +It also means that you'll not see any of the data you entered into the sqlite3 database on your computer. |
| 94 | + |
| 95 | +## Step 8: Visit your application |
| 96 | +Type this in the terminal: |
| 97 | +``` |
| 98 | +heroku apps:info |
| 99 | +``` |
| 100 | + |
| 101 | +This will display a listing of information about your Heroku application. One useful bit of information is your application's `Web URL`. You can copy this URL, open up a new browser tab and paste it into the address bar to see your application running on Heroku. |
| 102 | + |
| 103 | +## Explanation |
| 104 | +First, we had to do some work to make Heroku happy with our application. This required updating the `Gemfile` and bundling. |
| 105 | + |
| 106 | +* The `Gemfile` is a list of all the Ruby libraries your application needs. What we've declared here is that we want to use the `sqlite3` library while we're developing on our computer (the development group) but when deploying to Heroku (the production group) we want to use the pg library, which is made for the type of database that Heroku uses. |
| 107 | + |
| 108 | +* Bundler is how Ruby projects keep track of the gems that they use. We told Bundler what we wanted to use in the `Gemfile`, now we need to make sure those gems are installed. Since we don't have the type of database Heroku does, we skip the production gems. Don't worry though! Bundler still logs them so Heroku will install them when they get your code. |
| 109 | + |
| 110 | +You should be able to deploy your application any time it's in a good, working state. Your typical workflow will look like: |
| 111 | + |
| 112 | + 1. Add or change some code |
| 113 | + 1. Test your changes locally (or on cloud9) |
| 114 | + 1. Commit your changes (git commit) |
| 115 | + 1. Deploy changes to Heroku (optional) |
| 116 | + 1. (repeat) |
| 117 | + |
| 118 | +Any time your changes are committed, you should feel free to `git push heroku master` and boom! Your changes are live! |
0 commit comments