Going Live
Now that we got a working application, the world is ready to see it. Unfortunately, for now we can only run it locally, which is not a feasible way for us to show off our new shiny application.
Therefore, we must deploy it on a server. For these purposes we will resort to using Heroku, which is a SaaS that allows developers to host their applications in a cloud.
Preflight
Images
First, you'll need to update two lines in the app/assets/stylesheets/pages/landing.scss
file; otherwise the images will be broken when we deploy (bring online) your application.
Using ⌘ + F
, find the background: url(../img/intro-bg.jpg) no-repeat center center;
line and replace it with background: image-url("img/intro-bg.jpg") no-repeat center center;
.
Then find background: url(../img/intro-bg.jpg) no-repeat center center;
and also replace it with background: image-url("img/banner-bg.jpg") no-repeat center center;
If you have added other background urls, keep in mind to replace url
helper with image-url
for them as well.
Please also double check that your all stylesheets have .scss
extension. For example, app/assets/stylesheets/pages/landing.scss
.
Install Postgres
In order to install the production database, in your terminal run this command:
$ brew install postgresql
Gemfile
Second step will be adding the following gems to Gemfile
:
group :production do
gem 'pg'
gem 'thin'
gem 'rails_12factor'
end
The rails-12factor
gem will enable features such as static asset serving and logging on Heroku. The pg
gem is the Ruby interface to the PostgreSQL database. It is highly recommended to be used instead of SQLite, which is not intended as a production grade database. That is why we will also find gem 'sqlite3'
and move it to development and test groups only, like this:
gem 'sqlite3', group: [:development, :test]
After that run bundle install
in your console.
$ bundle install
Git
Next, in order to deploy we need some way to effectively manage our local files and changes. We are going to use Git VCS. It will provide us with a suite of tools for file storage and tracking of changes in those files.
We have to initialize a new git repository within our application's root folder by running:
$ git init
Then we have to track changes by using git add -A
command, which translates to "Git, please start tracking all the files in current repository". So run it from your terminal:
$ git add -A
Finally, you need to commit your changes and provide a description of what you have changed. The good practice is to keep it rather descriptive as in future it might help both you and your colleagues.
$ git commit -m "my very first commit"
Heroku CLI
To be able to deploy to Heroku we need to install its client application. This can be done via brew install heroku
on MacOS. Heroku CLI provides a set of tools that facilitate communication of our local machine with Heroku servers that host our application. So, let's install it by running in your terminal:
$ brew install heroku
Deploying on Heroku is a pretty straightforward procedure, but first we need to create a free account on their website.
Note: After you register at Heroku, don't forget your credentials since you will need them during the next steps.
Taking off
At this point of time we have everything we need and are able to proceed with the deploy.
Step #0: Sign In
Let's run heroku login
. This will prompt us with a login form, where we should enter our credentials.
$ heroku login
Enter your Heroku credentials:
Email: [email protected]
Password: ********
If our credentials are correct we will see the following:
Logged in as [email protected]
Note: We need to tell Heroku which account to use for hosting this application.
Step #1: Add Remote
Now that we are signed in, first thing we need to do is run heroku create <app_name>
in the root of our application. This command will create a new git remote named heroku
and will also produce an URL by which you may access your application.
Creating app... done, ⬢ salty-hollows-82713
https://salty-hollows-82713.herokuapp.com/ | https://git.heroku.com/salty-hollows-82713.git
Note: For example, I ran heroku create politiciantracker and now the URL to my application is http://politiciantracker.herokuapp.com.
Step #2: Deploy App
Now we can finally deploy the application to heroku. So let's do it, by running this command from terminal:
$ git push heroku master
This will say Heroku to apply everything we've done locally and launch the application.
Note: This command may produce a really long and cryptic output in your console, but this should not scare you in the least.
Our Multitool:heroku run
Heroku will run bundle install
automatically on each and every push, however if you got any migrations, you will need to trigger them manually. This can be done by means of the heroku run
command. It allows you to run (almost) any arbitrary command on the server.
To apply migrations we will tell server heroku run rake db:migrate
, this, in turn will apply changes to our schema.rb
and restart the application.
$ heroku run rake db:migrate
And... that is pretty much it.
Congrats! Today is the big day cause you have deployed your app online.
Making changes to already deployed app
Once you decide to make some changes to your app, as you already know, you will have to first track them, then stage them. Here are the three commands you should always keep in mind for such cases:
$ git add -A
$ git commit -m "message"
$ git push heroku master