LoginSignup
0
0

【credentials.yml.enc】環境変数を定義する方法

Last updated at Posted at 2023-06-02

この記事の目的

ポートフォリオを作成する上で、YoutubeAPIを導入したく前段階にAPIkeyを環境変数で定義しようとしています。

開発環境
ruby 3.1.2
Rails 6.1.7.3

導入前

各々必要なAPIkeyを用意しよう

やり方

まず、アプリケーションを作成しよう

ターミナル.
 rails new アプリ名

そしてconfigディレクトリに「credentials.yml.enc」ファイルと「master.key」があるか確認する。

credentialsを編集しよう

ターミナル.
EDITOR="vi" bin/rails credentials:edit

すると、下記の記載が出ると思います。
「i」を押すと、-----insert----になり編集が可能になります。

# aws:
#   access_key_id: 123
#   secret_access_key: 345

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base:##########
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
"/tmp/10374.credentials.yml" 9L, 360B    

ここにAPIkeyを定義していきます。

注意点

# aws:
#   access_key_id: 123
#   secret_access_key: 345

これはあくまで、見本?のため「#」でコメントアウトしています。そのため、自分のAPIkeyに「#」はいらない

# aws:
#   access_key_id: 123
#   secret_access_key: 345

google:
    api_key: ##########

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base:##########
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                            
~                                                                                                                                           
"/tmp/10374.credentials.yml" 9L, 360B   

これで、googleというオブジェクト名のapi_keyが定義できました。
保存するときは、『esc』キーを押してから、『:wq』を押して保存します。

定義したものを呼び出してみよう

rails c

でコンソールを開きます。

[1] pry(main)> Rails.application.credentials.google[:api_key]
=>##########

[2] pry(main)>  Rails.application.credentials.dig(:google, :api_key)
=>##########

以上の方法のどちらでも呼び出せます。

コード内に埋め込む場合は、以下になります。

production:
  adapter: mysql2
  encoding: utf8mb4
  database: ⚪︎⚪︎⚪︎_production
  username: root
  api_key: <%= Rails.application.credentials[:google][:api_key] %>

参考にさせてもらった記事

たった30分でわかるcredentials.yml.enc[rails5.2] - 環境変数を定義する使い方 -
【Rails】credentials.yml でシークレットキーを登録し、呼び出す方法

最後に

以上で定義は完了になります。 ここまででだいぶ手こずってしまったので記事にして残しておこうと思いました。間違いがあれば、コメントしてもらえると助かります。

また、Rails6以降からは、Multi Environment Credentials というものが導入されたので、本来のやり方とは違うかもしれません、、、

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0