[RUBY] [Rails] Heroku deployment flow

memorandum

Deploying with Heroku I often forget it, so for recording Since it is a beginner, please point out if you make a mistake. Rails and MySQL are used in this environment. I hope it will be useful for beginners in programming!

Step 1 Install Heroku CLI

This is the first deployment only. For the second and subsequent times, please see from step 2.

Terminal

Enter the following command in the directory to be used
% brew tap heroku/brew && brew install heroku

This command will allow you to use the heroku command, which will allow you to log in to Heroku from your terminal.

Step 2 Log in to Heroku

Enter the following in the terminal
% heroku login --interactive
  => Enter your Heroku credentials.
#Enter your email address and press enter
  => Email:
#Enter your password and press enter
  => Password:

You are now logged in to Heroku!

Step 3 Introduce rails_12factor

Gem with assets to run Rails application on a server such as a production environment

#Add to Gemfile
group :production do
  gem 'rails_12factor'
end
gem installation
% bundle install
Commit because I edited it
% git add .
% git commit -m "gem rails_Addition of 12 factors"

Don't forget to commit and push each change as the deployment uses the data on the remote repository.

Step 4 Create an app on Heroku

Create an app on heroku with the heroku create command. heroku create App name you want to create

Input at the terminal
Example
% heroku create heroku-test01

Step 5 Get MySQL on Heroku

You can use MySQL on Heroku by adding the ClearDB add-on.

Execute the following command in the terminal
heroku addons:add cleardb
Enter the following in the terminal
#ClearDB database URL variable heroku_cleardb
% heroku_cleardb=`heroku config:get CLEARDB_DATABASE_URL`

#Reset database URL
% heroku config:set DATABASE_URL=mysql2${heroku_cleardb:5}

At this point, you can use MySQL. Before proceeding to step 4, let's take a brief look at the credentials.yml.enc and master.key files.

The credentials.yml.enc file is a file that handles information that you do not want to leak to the outside with rails.

master.key file A file is a file for duplicating the ciphertext of credentials.yml.enc. Since master.key is an important file, it is not handled by Git by default.

Step 5.5 Credentials.yml.enc is duplicated with master.key

The following command decrypts credentials.yml.enc with master.key and the contents can be confirmed.

#Execute the following command in the terminal
% EDITOR="vi" bin/rails credentials:edit

Step 6 Place master.key on Heroku

As I explained earlier, master.key cannot be handled by Git, so you need to set environment variables. heroku config: set environment variable name = "value" This allows you to set environment variables on Heroku and use master.key.

Enter the following in the terminal
heroku config:set RAILS_MASTER_KEY=`cat config/master.key`
Check the environment variable settings with the following command
% heroku config

Step 7 Push the app

I'm pushing the contents of the remote repository to an app on heroku with the git push heroku master command.

Execute in the following terminal
% git push heroku master

So far, you've successfully reflected your application on Heroku! However, the migration information is not reflected ...

Step 8 Perform migration on Heroku

The migration information is reflected on the Heroku DB with the heroku run rails db: migrate command.

Execute the following command in the terminal
% heroku run rails db:migrate
#You can check the information of the application deployed on Heroku with the following command
% heroku apps:info

Postscript

When deploying additional functions etc. to the production environment in the future, you can easily deploy by commit → push → step 7.

Summary

thank you for your hard work! This is the end of the deployment process! I think that Heroku is rarely used in the actual field, but it is a very convenient introduction to deployment because it is a tool that can be easily deployed for beginners including myself! I hope it helps someone.

Recommended Posts

[Rails] Heroku deployment flow
Rails Heroku deployment procedure
Rails (postgeresql. Ubuntu environment), heroku deployment
rails heroku error log
[Rails] gem devise installation flow
rails g model Overall flow
Created the first LINEbot on AWS Cloud9 (Rails + Heroku deployment)
[rails] heroku deployment error ActionView :: Template :: Error (Mysql2 :: Error: Table --- doesn't exist):
Launch Rails on EC2 (manual deployment)
rails AWS deployment is not reflected
Deploy Rails on Docker to heroku
Troublesome Rails environment construction flow [Windows 10]
[EC2 / Vue / Rails] EC2 deployment procedure for Vue + Rails
App update reflected after heroku deployment
[Rails] AWS deployment error encounter summary
Deploy your Rails app on Heroku
How to redo a deployment on Heroku
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
Deploy to heroku with Docker (Rails 6, MySQL)
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
Deploy to Heroku [Ruby on Rails] Beginner
[heroku] run rails db: migrate doesn't work
[Rails / Heroku] Error resolution procedure after push