Chapter 22

Deploy your application online

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.

22.1. Create an account 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!

22.2. Create a new application on Heroku

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.

22.2.1. Preparations to do on BlastCRM

As we saw in the modular workflow, before bringing our application, we first have some tweaks to do.

Building and pushing our gems

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

Duplicate the folder of the application:

cp -R blast_crm blast_crm_prod

Remove the 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

Update the Gemfile

Listing 0.1: Gemfile using module gems 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'

22.2.2. Add Heroku specific gems

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:

Listing 0.2: Gemfile with Heroku gems Gemfile
.
.
.
group :development, :test do
  gem 'sqlite3'
end
group :production do
  gem 'pg'
  gem 'rails_12factor'
end
.
.
.

22.2.3. Bundle

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

22.2.4. Commit your changes

git add .
git commit -m 'Switch to production application'

22.2.5. Push your 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

22.2.6. Enjoy!

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.

22.3. Wrap Up

In this chapter, we learned about the modular workflow and how to push our application to a production server.

22.3.1. Next Step

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!