[RUBY] Deploy your Rails app on Heroku

It will be a personal memorandum. All basic operations are done in the macOS terminal.

1. 1. Install Heroku CLI

Open a terminal and enter the following command.

% brew tap heroku/brew && brew install heroku

When the installation is complete, enter the following command to confirm the completion. Here you should see it in heroku/x.y.z output.

% heroku --version
heroku/7.0.0 (darwin-x64) node-v8.0.0

2. 2. Login

After installing the CLI, run the heroku login command. You will be asked for your email address and password, so enter them.

% heroku login
=> Enter your Heroku credentials.
#Enter your email address and press enter
  => Email:
#Enter your password and press enter
  => Password:

If Logged in as email address is output, login is successful.

3. 3. Create an application on Heroku

%cd Location of the app you want to deploy

When creating an app, the app name must be unique, so you'll get an error if it's already in use by someone else. Also, since the app name becomes part of the domain, you cannot use character strings that cannot be used in the domain. Pay attention to the above two points and enter the following command.

%heroku create application name

If you want to confirm that the settings are correct, enter the following.

% git config --list | grep heroku

After inputting, if anything other than fatal: not in a git directory is displayed, it is successful.

4. When using MySQL

On Heroku, the database setting used is PostgreSQL by default. To use MySQL, you can use MySQL on Heroku by adding an add-on provided by a database service called ClearDB.

Enter the following command to add the ClearDB add-on.

% heroku addons:add cleardb

If you use Ruby on Rails, you need to consider the gem that supports MySQL, and change that setting. First, enter the following command.

% heroku_cleardb=`heroku config:get CLEARDB_DATABASE_URL`

You have now stored the URL of your ClearDB database in the variable heroku_cleardb. Then enter the following command.

% heroku config:set DATABASE_URL=mysql2${heroku_cleardb:5}

5. Set up master.key on Heroku

On Heroku, set the value of master.key as an environment variable (one of the data sharing functions provided by the OS, "a variable that can be referenced from any directory file"). First, enter the following command.

% heroku config:set RAILS_MASTER_KEY=`cat config/master.key`

To see the Heroku environment variable list for correct settings, enter the following command:

% heroku config

After inputting, if there is an item RAILS_MASTER_KEY: , it is successful.

6. Add application to Heroku

You can add application information to Heroku by pushing a commit to Heroku. Enter the following command.

% git push heroku master

7. Run the migration file on Heroku

The database does not reflect the migration information, so keep heroku run in mind when performing the migration.

% heroku run rails db:migrate

8. Check application information

To see the application information reflected in Heroku in the previous steps, enter the following command:

% heroku apps:info

9. Open the application

% heroku open

Deployment is complete when the application opens without any problems.

Bonus 1: Check the log

If you run into problems that prevent your app from working properly, check the logs. By adding --tail, only the last 10 lines of the log can be displayed.

% heroku logs --tail --app application name

Bonus 2: What to do if the environment variables are not updated

If there is a file change history

If there are any differences in the file since you pushed it to Heroku, you can commit as usual and push that commit to Heroku. Execute the following code in order from the top.

% git add .
% git commit -m "Commit name that is easy to understand later"
% git push heroku master

If the file change history does not exist

If you don't have any files to change, but you want to reflect only the environment variables set in Heroku in the production environment, the above method will display "Everything up-to-date (already updated to the latest state)". Will be done. So we'll deliberately create an empty commit and push it to Heroku. Execute the following code in order from the top.

% git commit --allow-empty -m "Empty commit" 
% git push heroku master

Recommended Posts

Deploy your Rails app on Heroku
Deploy Rails on Docker to heroku
Deploy to Heroku [Ruby on Rails] Beginner
Deploy a Java web app on Heroku
Heroku app moving (rails)
Deploy RAILS on EC2
How to deploy jQuery on Rails
Incorporate Elasticsearch into your Rails app
How to deploy Bootstrap on Rails
Deploy a war file on Heroku
Deploy Flask's Docker image on Heroku
Redirect to your own domain with rack-rewrite on Heroku x Rails
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
Deploy to heroku with Docker (Rails 6, MySQL)
Try deploying a Rails app on EC2-Part 1-
Deploy Java Servlet app locally on Tomcat
Try deploying Rails app to EC2-Part 2 (Deploy)-
[Portfolio] Bookmark management app [Ruby on Rails]
Deploy a Tomcat-based Eclipse project on Heroku
Use Timecop in Rails web app to travel time on your browser
Tailwind on Rails
heroku: docker: deploy
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
Publish the app made with ruby on rails
Introducing New Relic to Rails apps on Heroku
Ssh login to the app server on heroku
Incorporate Docker into your Ruby on Rails portfolio!
[Rails MySQL] How to reset DB on heroku
Use pagy for pagination in your Rails app.
Deploy Line bot with rails5 + Docker + Heroku Note
Steps to deploy to Heroku
Try running an app made with Quarkus on Heroku
[Heroku] Associate AWS S3 with the deployed Rails app
Rails deploy with Docker
Test run on rails
Introducing Bugsnag on Heroku
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
rails heroku error log
Ruby on Rails Elementary
Ruby on Rails basics
[Rails] Heroku deployment flow
Yay! I'm on Heroku!
Yay! I'm on Rails!
Ruby On Rails Association
Error resolution on Heroku
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
About regular execution of rake task of rails application on heroku
How to deploy a kotlin (java) app on AWS fargate
Confirm public key ~ Register key on Github ~ Push Rails app to Github
How to deploy a Rails application on AWS (article summary)
Resolve ActiveRecord :: NoDatabaseError on rails6
Ruby on rails learning record -2020.10.03
Deploy Vapor Project to Heroku
Portfolio creation Ruby on Rails
Install Sidekiq pro on Rails
Ruby on rails learning record -2020.10.04
Make your own Rails validate
Introduce Rails/Basic authentication on Heroku.
ROS app development on Android
[Ruby on Rails] Debug (binding.pry)