As said in the introduction of this chapter, we’re going to use Heroku to deploy our web application online. Since the modular workflow was described quite precisely above, we’ll not explain again the concepts you are already familiar with.
However, we will have a look at the practical aspect to push our application on Heroku.
If you don’t have a Heroku account yet, just go to the sign-up page. If you already have one, then it’s perfect!
Once signed in, you can now create a new application. We will, surprisingly, call ours blast-crm
! Feel free to pick the region of your choice for this one.
As we saw in the modular workflow, before bringing our application, we first have some tweaks to do.
You know how to do it, basically as we already did for some of our modules, you are going to build the gems and push them on your private gems server. Just don’t forget to have those at the same version, in our scenario, all of them are the version 0.1.0
.
For that just reach the version.rb
of one of the engines, such as the Contacts
one, update the file. For the Contacts
module for example, the file is located in engines/contacts/lib/blast/contacts
if you don’t remember.
To build the gems and push them to our private gems server, you know what to do:
gem build blast_contacts.gemspec
gem inabox blast_contacts-0.1.0.gem
rm blast_contacts-0.1.0.gem
cp -R blast_crm blast_crm_prod
engines
folder:Navigate inside blast_crm_prod
and remove the engines folder. We won’t be needing it for the production application:
cd blast_crm_prod && rm -r engines
Gemfile
source "http://blast:#{ENV['GEM_SERVER_KEY']}@[your_server]:9292"
.
.
.
gem 'blast_core', '0.1.0'
gem 'blast_contacts', '0.1.0'
gem 'blast_tasks', '0.1.0'
Ok, our application is almost ready to be pushed to Heroku, but we still have some additional tasks to do so that everything works. First, note that you have to move sqlite3
to the development and test group, otherwise Heroku will crash! We introduce here two new gems: pg
(PostgreSQL) for our database instead of sqlite3
(not supported by Heroku), and rails_12factor
, both are required to play with Heroku.
Let’s update the Gemfile
:
Gemfile
.
.
.
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
.
.
.
After all those modifications in the Gemfile
it’s time to install and update our gems via Bundle
.
bundle update
If you have never installed the pg
gem before, you might encounter a similar error:
...
Fetching pg 1.1.4
Installing pg 1.1.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
It’s perfectly normal, to build the gem the system requires the development libraries of PostgreSQL. On Debian based systems like Ubuntu you can install via the following command:
apt install libpq-dev
On MacOS using brew
:
brew install postgresql
You might need to install the Xcode Command Line Tools
before if they’re not present on your machine:
xcode-select --install
git add .
git commit -m 'Switch to production application'
We first have to install the heroku
command-line tools (known as heroku-cli
), you can find more information on how to install it depending on your system in the official documentation of the tool itself. On macOS it as simple as:
brew tap heroku/brew && brew install heroku
On Ubuntu 16+:
sudo snap install --classic heroku
Since we’ve created our Heroku dyno (“app”) via the web interface, we need to add a remote
entry to Git:
heroku git:remote -a blast-crm
You will be asked to enter your Heroku’s credentials, then your new git remote repository for Heroku should have been added:
git config --list | grep heroku
remote.heroku.url=https://git.heroku.com/blast-crm.git
remote.heroku.fetch=+refs/heads/*:refs/remotes/heroku/*
Or:
git remote
heroku
origin
Before finally pushing our production application online, we need to configure the environment variable for our private Geminbox’s secret key:
heroku config:set GEM_SERVER_KEY=A_VERY_SECRET_KEY
Alternatively, you can also do it through the web interface, in the settings of the created app on Heroku.
git push heroku master && heroku run rake db:migrate -a blast-crm
If you want to push a specific branch you’ve been working on, let’s say Chapter-22
, to Heroku, then the command line should be:
git push heroku Chapter-22:master && heroku run rake db:migrate -a blast-crm
Now navigate to the URL of your deployed application and you will see the BlastCRM app you worked so hard to create! Thanks to the heroku
command-line tools, you can also use heroku open
in the directory of your application to automatically open your browser to this URL.
In this chapter, we learned about the modular workflow and how to push our application to a production server.
There is no next step! We’ve taught you everything we know about building modular applications. We hope you enjoyed the ride and learned some new things along the way!