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!
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.
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!
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.
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
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.
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
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
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 ...
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
When deploying additional functions etc. to the production environment in the future, you can easily deploy by commit → push → step 7.
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