Other Pages

Expand All

deploying_to_heroku.step

h2 do
  span "Begin by saving your code in a version control system called git: "
  a 'Add the Project to a Git Repo', href: 'http://docs.railsbridgeboston.org/intro-to-rails/add_the_project_to_a_git_repo'
  span "Then, if you haven't yet deployed to heroku, start at "
  a 'First-time setup', href: '#first-time'
  span ". Otherwise, start at "
  a 'Every time', href: '#every-time'
  span "."
end

a name: 'first-time'
situation "First-time setup" do
  step "Sign up for heroku" do
    message %{Go to <a href="https://signup.heroku.com/www-header">Heroku's webpage</a> and sign up for a free account.}
  end

  step "Create a heroku application" do
    console "heroku create"
    message "This command will ask you for your credentials - type in your Heroku email and password, hitting Enter after each one."
    message "`heroku create` registers a new application on Heroku's system. You should see some output including your new app's URL, which will look like http://sushi-island-6128.herokuapp.com/ ."
  end

  step "Create a Heroku key" do
    message "Creating a Heroku key will let Heroku know who is creating the app."
    console "heroku keys:add"
    message <<-MARKDOWN
      The output will look like this:

      ```
      Could not find an existing public key.
      Would you like to generate one? [Yn]
      ```

      Type "Y" for "Yes" and hit "Enter".

      The output will look like this:

      ```
      Generating new SSH public key.
      Uploading SSH public key /home/vagrant/.ssh/id_rsa.pub... done
      ```

      Great, now you have a Heroku key!
    MARKDOWN
  end

  step "Edit the Gemfile" do
    important "Each application has its own `Gemfile`. Be sure you're opening the one inside your app's folder."

    message "Heroku will run our application slightly differently than our development computer does, which requires us to make a small change to our `Gemfile`."

    message "Open the file called `Gemfile` in Sublime Text (or your preferred editor) and find the line containing:"

    source_code :ruby, <<-RUBY
gem 'sqlite3'
    RUBY

    message "**Remove that line** and replace it with:"

    source_code :ruby, <<-RUBY
group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
  gem 'rails_12factor'
end
    RUBY
  end

  step "Apply the Gemfile changes" do
    console "bundle install --without production"
    message "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``."
  end

  step "Commit the Gemfile changes" do
    message "There are now changes to Gemfile and Gemfile.lock that need to be committed before we can push to heroku."
    console <<-SHELL
git add .
git commit -m "Changed Gemfile for heroku"
    SHELL
    tip "There is a period after the word add in the first line."
  end
end

a name: 'every-time'
situation "Every time" do
  step "Commit any pending changes to git" do
    message "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."
    console "git status"
    message "`git status` shows you any pending changes you've created. If it has no output, you're already ready to deploy! Otherwise..."

    console <<-SHELL
git add .
git commit -m "Some helpful message for your future self"
    SHELL
    message "Your commit message should reference whatever your outstanding changes are: something like 'added votes to the topics index'."
  end

  step "Push changes to heroku" do
    console "git push heroku master"
    message "This takes all changes you've committed locally and pushes them to heroku."
  end

  step "Run database migrations on Heroku" do
    console "heroku run rails db:migrate"
    message "This tells Heroku to run your migrations on its database, like running rails db:migrate locally. Heroku's database is separate from the one on your computer, which means it needs to be updated every time you make changes to the structure of your database.  It also means that you'll not see any of the data you entered into the sqlite3 database on your computer."
  end

  step "Visit your application" do
    console "heroku open"
    message "This displays the URL of your application to open in your browser."
  end
end

explanation do
  message <<-MARKDOWN
  First, we had to do some work to make Heroku happy with our application. This required updating the Gemfile and bundling.

  * 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.

  * 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.
  MARKDOWN

  message "You should be able to deploy your application any time it's in a good, working state. Your typical workflow will look like:"
  img src: "img/workflow.png", alt: "Diagram showing git workflow of making changes, committing them, and pushing to Heroku.", style: "border: none"
  ol do
    li { message "Add or change some code" }
    li { message "Commit your changes (`git commit`)" }
    li { message "(repeat)" }
  end
  message "Any time your changes are committed, you should feel free to `git push heroku master` and boom! Your changes are live!"
end