[RAILS] Find out about environment variables (memo)

I'm using this for some reason Examine deeply so that it can be properly verbalized

What are environment variables?

Environment variables are ** external service passwords **, ** API private keys **, etc. To prevent leakage of confidential information that I do not want to give to Github A mechanism that saves in the OS such as a server and passes it to the application → If you give it to Github as it is, an external service will be used Be careful because I have seen articles such as a large amount of billing ...

Variables used when the OS is running Required when executing a program, etc.

Setting method

What to do this time How to use the gem "** dotenv-rails **" Normally a command is needed to load .env, It seems that it will be automatically loaded by introducing a gem called "dotenv-rails"

There seems to be another way to describe it in "**. Bash_profile **"

Try setting using "Doten v-rails"

Install Gem

First, install Gem

Gem.file


gem 'dotenv-rails'

promise

bundle install

Create ".env file"

Create a file called ".env" in the root directory

I am creating with sample_app

~/environment/rails/sample_app
❯ pwd

/Users/username/environment/rails/sample_app
❯ touch .env

~/environment/rails/sample_app
❯ ls
Gemfile      Rakefile     config       lib          public       tmp
Gemfile.lock app          config.ru    log          storage      vendor
README.md    bin          db           package.json test

~/environment/rails/sample_app
❯ ls -A
.env          .ruby-version Rakefile      config.ru     package.json  tmp
.git          Gemfile       app           db            public        vendor
.gitignore    Gemfile.lock  bin           lib           storage
.history      README.md     config        log           test

→ I haven't even created a file and used the ls command! Don't rush You can see it by adding "-A" as an option.

Try setting environment variables

Describe the environment variables in the created .env file. Please edit with vim

.env


#Describe the key and password you want to use
KEY='*******'
SECRET_KEY='*******'
```

#### Edit gitignore
 To prevent unauthorized use of the information defined in the ".env file"
 ** Need to be removed from Git control **
 Therefore, set .gitignore


#### **`gitignore`**
```python

#Add the following
.env
```
 Now you don't have to worry about giving it to Git

##### If you put it under the control of Git
 If you have already pushed to git, use the following command
 You can remove ".env file" from git management

```
git rm --cached .env
```

#### Call environment variables

 Calling is the same as .bash_profile.


#### **`sample.rb`**
```rb

#Calling the set environment variable
ENV['KEY']
ENV['SECRET_KEY']
```

 You can also check with "** rails c **"

```rb
irb(main):002:0> ENV['KEY']
=> '*******'
```

## Reference article
https://railsguides.jp/configuring.html
https://wa3.i-3-i.info/word11027.html
https://qiita.com/yuichir43705457/items/7cfcae6546876086b849
http://e-words.jp/w/%E7%92%B0%E5%A2%83%E5%A4%89%E6%95%B0.html
https://qiita.com/hayatokunn/items/d8a9e9deec33e9022b4f
https://qiita.com/ryosuketter/items/ceb592dc6b23a20e51b5


Recommended Posts

Find out about environment variables (memo)
Find out about Docker
Find out about class methods
Find out about annotationProcessor for Gradle 5.0
Find out about instance methods and self
About Ruby variables
Java development environment memo
MEMO about Rails 6 series
[Personal memo] About Spring framework
MySQL 5.7 (Docker) environment construction memo
[Environment variables] What are rails environment variables?
Redmine (Docker) environment construction memo
About Java class variables class methods
About instance variables and attr_ *