- Read Tutorial
- Watch Guide Video
Continuing from the last few videos related to deployment, we have completed the first four tasks, namely,
Create a local git repository
Create your initial commit
Create a remote repository on github
Push your local code to the remote repository
Now, we are going to move to subsequent steps.
The next step is to sign up for a Heroku account. It's free and the sign up process is simple and self-explanatory. Once you've signed up for the Heroku account, install the Heroku CLI toolbet from https://toolbelt.heroku.com. This is also a fairly straightforward process.
In the next step, we are going to create the application on Heroku. Go to your taski
command line and type:
heroku create eductechional-taski
and this will create the application for you on Heroku and name it taski
.
You can also check this by typing the command
git remote -v
This will give you a list of all the remote URLs.
Next, I want to push my code to Heroku, and to do this, use the command:
git push heroku master
Essentially, this code goes to github and looks at all the code in the Master branch. This code then gets pushed to our Heroku application. If you look at the code, git push
is pushing code from git, the destination is heroku
and the branch is master
.
If you watch your terminal as this code gets executed, you can see that it has detected that this is Ruby and has identified the version. It is also installing all the gems that we have used in our application.
This installation can take anywhere from a few minutes to about half an hour, depending on the speed of your connection and the size of your application.
During this installation, Heroku detected an error, so the push was not successful. The error is Heroku does not recognize sqlite database, so we have to change it to postgres database. Here is the error message:
Besides database, bad CSS files are another common mistake that can cause the application to fail. So, if your deployment fails, these are the two aspects that you should check first.
Now, fixing this error is fairly simple. Go to your Gemfile and you can see the code:
gem 'sqlite3'
Remove this line and replace it with:
gem 'pg'
Then, go to your console and type bundle install
and this will install the postgres gem for you. Check the status on github with the command git status
. This will show that the Gemfile is modified. Also, I updated the README.md
file to include this task of changing the database, and so my README.md
file was also changed in git.
As a next step, commit these files with the command:
git add . git ci -m 'Switched database to postgres'
This will commit the files to github repository. Now push the files to github with the command git push
. It may ask for your username and password.
Once the files are pushed to git, next push these files to Heroku with the command:
git push Heroku master
This time the push failed too, and we have the following error message:
The error message is the aws_access_key_id
and the aws_secret_access_key
are missing. Before fixing this error, I'm going to update the README.md
file with the message Integrate config variables on Heroku
.
To fix this error, type the command:
heroku config:set AWS_ACCESS_KEY_ID = "Your key ID heroku config:set AWS_SECRET_ACCESS_KEY = "Your secret access key heroku config:set AWS_BUCKET = "Your production bucket name
I'm going to pause here because I don't want anyone to know my secret keys. You can also go to application.yml
file in config directory and copy the values from there.
If you want to check to see if the config variables are live on Heroku, type:
heroku config
and it should print out all your config variables.
Let's now push our README.md
file that I updated to git with the code:
git add . git ci -m 'Updated readme with instructions'
Then, run the command:
git push
Next, let's push it to heroku again with the command
git push heroku master
This time it was successful.
You can also see that it gave us the URL where our application is installed to make it easy for us to access. However, before accessing the application on this URL, we have to do one last step, which is to migrate our database. To do this, let's type the command:
heroku run rake db:migrate
This command looks at the schema file and creates a database for us. Not doing this step will cause your application to fail, and it's a common mistake that many developers do. So, once this step is completed, your screen should look like this.
Now, we are ready to check the application on our browser. Go to https://edutechional-taski.herokuapp.com
and this is what it should look like:
One thing about Heroku is they are highly concerned about their performance, so if your application is not used for an hour or for a day, it can shut the application down. So, when you start again, your application can take a longer time to load. There are many ways around it. One is to have a paid account, and another is to have a service called "New Relic" installed on your system, so you can ask this service to ping your URL once every few minutes.
Play around with the functionality to see if everything is working, which I think should be fine. One thing I notice is the alerts are displayed twice.
You can fix this by going to the view page and removing the concerned alert message.
So, congratulations! You have your application on Heroku now. Before we end, I'd like to tell you that I kept the error messages intact because I want you to know how to fix them. As a developer, you will encounter errors day in and day out, so it's important you read the error message, understand it and fix accordingly.