Skip to content

Commit

Permalink
Merge pull request #302 from JuanitoFatas/update-heroku-guide
Browse files Browse the repository at this point in the history
Update Heroku guide for Rails 5
  • Loading branch information
hone authored Aug 31, 2016
2 parents 50b46ae + 001b9f8 commit b5e8e19
Showing 1 changed file with 79 additions and 72 deletions.
151 changes: 79 additions & 72 deletions _posts/2012-04-19-heroku.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,33 @@ permalink: heroku

### Get Heroku

Follow steps 1 through 3 of the [quickstart guide](https://devcenter.heroku.com/articles/quickstart) to sign up, install the toolbelt, and login.
Follow steps "Introduction" and "Set up" of the
[Getting Started on Heroku with Ruby][heroku-guide] to sign up, install the
Heroku CLI, and login.

__COACH__: Talk about the benefits of deploying to Heroku vs traditional servers.

[heroku-guide]: https://devcenter.heroku.com/articles/getting-started-with-ruby#introduction

### Preparing your app

#### Version Control Systems

We need to add our code to version control. You can do this by running the following in the terminal:
We need to add our code to version control. You can do this by running the
following in the terminal:

{% highlight sh %}
git init
echo "public/uploads" >> .gitignore
echo "tmp" >> .gitignore
echo "logs" >> .gitignore
git add .
git commit -m "initial commit"
{% endhighlight %}

__COACH__: This would be a good time to talk about version control systems and git, also explain the `.gitignore` and why we don't want these files included.
__COACH__: This would be a good time to talk about version control systems and git.

#### Updating our database

First, we need to get our database to work on Heroku, which uses a different database. Please change the following in the Gemfile:
First, we need to get our database to work on Heroku, which uses a different
database. Please change the following in the Gemfile:

{% highlight ruby %}
gem 'sqlite3'
Expand All @@ -52,110 +55,114 @@ end

Run `bundle install --without production` to setup your dependencies.

__COACH__: You can talk about RDBMS and the different ones out there, plus include some details on Heroku's dependency on PostgreSQL.


#### Adding rails_12factor

Next, we need to add `rails_12factor` entry into our Gemfile to make our app available on Heroku.

This gem modifies the way Rails works to suit Heroku, for example Logging is updated and the configuration for static assets (your images, stylesheets and javascript files) is tweaked to work properly within Heroku's systems.

Please change the following in the Gemfile:

{% highlight ruby %}
group :production do
gem 'pg'
end
{% endhighlight %}

to

{% highlight ruby %}
group :production do
gem 'pg'
gem 'rails_12factor'
end
{% endhighlight %}

After this run `bundle`, then commit the changes to Gemfile.lock to your repository:

{% highlight sh %}
git commit -a -m "Added rails_12factor gem and updated Gemfile.lock"
{% endhighlight %}

__COACH__: You can talk about logging on Heroku, as well as its other quirks.

__COACH__: You can talk about RDBMS and the different ones out there, plus
include some details on Heroku's dependency on PostgreSQL.

### Deploying your app

#### App creation

We need to create our Heroku app by typing `heroku create` in the terminal and see something like this:
We need to create our Heroku app by typing `heroku create` in the terminal and
see something like this:

{% highlight sh %}
Creating sheltered-refuge-6377... done, stack is cedar
http://sheltered-refuge-6377.herokuapp.com/ | [email protected]:sheltered-refuge-6377.git
Git remote heroku added
Creating app... done, ⬢ young-reaches-87845
https://young-reaches-87845.herokuapp.com/ | https://git.heroku.com/young-reaches-87845.git
{% endhighlight %}

In this case "sheltered-refuge-6377" is your app name.
In this case "young-reaches-87845" is your app name.

#### Pushing the code

Next we need to push our code to heroku by typing `git push heroku master`. You'll see push output like the following:
Next we need to push our code to heroku by typing `git push heroku master`.
You'll see push output like the following:

{% highlight sh %}
Initializing repository, done.
Counting objects: 101, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (91/91), done.
Writing objects: 100% (101/101), 22.68 KiB | 0 bytes/s, done.
Total 101 (delta 6), reused 0 (delta 0)

-----> Ruby app detected
-----> Compiling Ruby/Rails
-----> 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
Fetching gem metadata from https://rubygems.org/..........
Counting objects: 115, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (97/97), done.
Writing objects: 100% (115/115), 25.62 KiB | 0 bytes/s, done.
Total 115 (delta 10), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.2.4
remote: -----> Installing dependencies using bundler 1.11.2
remote: Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote: Fetching gem metadata from https://rubygems.org/..........
remote: Fetching version metadata from https://rubygems.org/...
remote: Fetching dependency metadata from https://rubygems.org/..
remote: Installing concurrent-ruby 1.0.2
...
-----> Launching... done, v6
http://sheltered-refuge-6377.herokuapp.com/ deployed to Heroku
remote: -----> Launching...
remote: Released v5
remote: https://young-reaches-87845.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/young-reaches-87845.git
* [new branch] master -> master
{% endhighlight %}

You'll know the app is done being pushed, when you see the "Launching..." text like above.

#### Migrate database

Next we need to migrate our database like we did locally during the workshop: `heroku run rake db:migrate`.
Next we need to migrate our database like we did locally during the workshop:

When that command is finished being run, you can hit the app based on the url. For this example app, you can go to <http://sheltered-refuge-6377.herokuapp.com/>. You can also type `heroku open` in the terminal to visit the page.
{% highlight sh %}
heroku run rake db:migrate
{% endhighlight %}

When that command is finished being run, you can hit the app based on the url.
For this example app, you can go to <https://young-reaches-87845.herokuapp.com/>.
You can also type `heroku open` in the terminal to visit the page.

#### Closing notes

Heroku's platform is not without its quirks. Applications run on Heroku live within an ephermeral environment — this means that (except for information stored in your database) any files created by your application will disappear if it restarts (for example, when you push a new version).
Heroku's platform is not without its quirks. Applications run on Heroku live
within an ephermeral environment — this means that (except for information
stored in your database) any files created by your application will disappear
if it restarts (for example, when you push a new version).

###### [Ephemeral filesystem][ephemeral-filesystem]

###### [Ephemeral filesystem](https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem)
> Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.
> Each dyno gets its own ephemeral filesystem, with a fresh copy of the most
> recently deployed code. During the dyno’s lifetime its running processes can
> use the filesystem as a temporary scratchpad, but no files that are written
> are visible to processes in any other dyno and any files written will be
> discarded the moment the dyno is stopped or restarted. For example, this
> occurs any time a dyno is replaced due to application deployment and
> approximately once a day as part of normal dyno management.
In the [App](/app) tutorial the ability to attach a file to the Idea record is added, which results in new files being written to your applications `public/uploads` folder. The ephemeral storage in Heroku can be seen with the following steps:
In the [App](/app) tutorial the ability to attach a file to the Idea record is
added, which results in new files being written to your applications
`public/uploads` folder. The ephemeral storage in Heroku can be seen with the
following steps:

1. Launch the app with `heroku open`
2. Add a new Idea with an image
3. Restart the application by running `heroku restart`
4. Go back to your Idea and reload the page - the image should no longer be visible

[ephemeral-filesystem]: https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem

##### Working around Ephemeral Storage

Obviously this doesn't seem to be useful if you were running a real life application, but there are ways to work around this which is commonly used by a lot of popular websites.
Obviously this doesn't seem to be useful if you were running a real life
application, but there are ways to work around this which is commonly used by
a lot of popular websites.

The most common method is to use an external asset host such as Amazon S3 (Simple Storage Service) or Rackspace CloudFiles. These services provide (for a low cost - usually less then $0.10 per GB) storage 'in the cloud' (meaning the files could potentially be hosted anywhere) which your application can use as persistent storage.
The most common method is to use an external asset host such as Amazon S3 (Simple
Storage Service) or Rackspace CloudFiles. These services provide (for a low cost
- usually less then $0.10 per GB) storage 'in the cloud' (meaning the files
could potentially be hosted anywhere) which your application can use as persistent storage.

While this functionality is a bit out of scope for this tutorial there are some resources available which you can use to find your way:
While this functionality is a bit out of scope for this tutorial there are some
resources available which you can use to find your way:

* [How to: Make Carrierwave work on Heroku](https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku)
* [Amazon S3 - The Beginner' Guide](http://www.hongkiat.com/blog/amazon-s3-the-beginners-guide/)
* [Amazon S3 The Beginner’s Guide](http://www.hongkiat.com/blog/amazon-s3-the-beginners-guide/)

As always if you require any more information or assistance your coaches will be able to assist.

0 comments on commit b5e8e19

Please sign in to comment.