You've learned a lot about how to build a Rails application over the last few weeks. Now let's 'go public' and share our apps with the world!
By the end of this, developers should be able to:
- Create a heroku app from the command line
- Push the latest code to heroku
- Migrate the production database
- No forking is necessary for this talk.
The first step is creating an account on Heroku, which you can do at https://www.heroku.com. You will be sent an activation email, so be sure to activate your account.
The Heroku Toolbelt can be found here. Download and install it.
Run heroku login
; you should be prompted for the credentials for your Heroku
account. Once you log in, if you're prompted to add the credentials to your
keychain, say yes.
Now you're setup to use heroku. Every time you want to deploy a new application, follow these steps:
- Create a heroku app from the command line (
heroku create
) - Add the
rails_12factor
gem to the production group in your Gemfile, if it doesn't already exist. - Make sure you have specified a ruby version in your Gemfile. It should
match the version referenced in
.ruby-version
, if that file exists. -
bundle install
to make sureGemfile.lock
is up-to-date. - Push the latest code to heroku (
git push heroku master
) - Migrate the production database (
herok run rake db:migrate
) - If you have seeds or examples, you may wish to run those with
heroku run
as well.
Let's look at each of these steps in detail.
Go to the root of your repo and run heroku create
. This will create a
strangely named app, and add a new remote repository to your repo called
heroku. You can look more closely at it by typing git remote -v
. You should
see something like this:
heroku git@heroku.com:agile-badlands-7658.git (fetch)
heroku git@heroku.com:agile-badlands-7658.git (push)
origin git@github.com:tdyer/wdi_4_rails_hw_tdd_hacker_news.git (fetch)
origin git@github.com:tdyer/wdi_4_rails_hw_tdd_hacker_news.git (push)
Add the following to your Gemfile, and run bundle install
.
group :production do
gem 'rails_12factor'
end
Next, you should specify the particular version of Ruby that your app is using, since it may sometimes cause issues (and in any case, Heroku will complain if you don't).
# Gemfile
source 'https://rubygems.org'
+ ruby '2.2.4'
Every time you edit your Gemfile
, you must run bundle install
afterward.
Always commit Gemfile
and Gemfile.lock
together, and always commit them
immediately after adding a new gem.
Only keep clean, working code on master
. After you work on a feature and
complete it, merge it into master
. Push your updated master
to GitHub, then
deploy to Heroku.
git checkout master
git merge my-feature # merge your working code
git push # update GitHub
git push heroku master # udpate heroku
Once you've deployed your code, you can safely run new migrations. You'll need to do this step every time you have new migrations.
heroku run rake db:migrate
If you have seeds or examples, or if you've updated seeds or examples, you should also run them on heroku.
heroku run rake db:seed
heroku run rake db:examples
Restart your application and check it out in the browser.
heroku restart
heroku open
You'll probably see something like this:
That's normal, unless you have defined a root route.
At any point, you can pick a new name for your app. It must be unique across all apps deployed to heroku.
heroku apps:rename newname
Your app will become immediately available at it's new subdomain,
newname.herokuapp.com
.
A full list of Heroku commands can be access by running heroku --help
; below
are some of the more common ones.
Behavior | Commands |
---|---|
heroku apps:info |
Get info about ALL of our Heroku apps. |
heroku apps:info --app {name_of_app} |
Get info about a specific Heroku app. |
heroku apps:open --app {name_of_app} |
Open any given Heroku app (other than the one we're currently working with.) |
heroku config |
Environmental variables in your current Heroku app. |
heroku logs |
Logs from the currently running app. |
heroku ps |
Processes running in your heroku application. |
heroku releases |
Each time you deploy to heroku you are creating a "release". This command shows all releases. |
heroku pg:info |
Access Postgres from within Heroku and show the heroku plan, connections, pg version, data size, tables. |
heroku pg:psql |
... and open a psql console. |
heroku run ... |
Run a program from within Heroku. |
One serious limitation of Heroku is that it provides an 'ephemeral filesystem'; if you save something, like an uploaded image file, it will disappear when your app is restarted or redeployed.
As an example, try running the following commands:
heroku run bash
touch happy.txt; echo 'is happy' > happy.txt
cat happy.txt
Then, hit Ctrl-D to get out of heroku bash shell. If you re-open the shell and
run ls -l
, happy.txt
will be missing!
The typical workaround is to save files in cloud storage such as Amazon S3; more on this in the near future.
Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.