2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Firebase App Hosting】Firebase App Hostingにシークレット情報を設定する

Posted at

はじめに

以前、Firebase App Hostingでデプロイに挑戦しました。
Next.js内で定義している.envに定義している情報をFirebaseでの設定方法がわからなかったため、調べたことをまとめてみました。

Firebase App Hostingでデプロイする方法はこちらになります

シークレット情報の登録方法

Next.jsの.envに定義していた情報は下記です。
今回は、下記のQIITA_API_TOKENをFirebaseに登録してみます。

.env
QIITA_API_URL=https://qiita.com/api/v2/authenticated_user/items
QIITA_API_TOKEN=アクセストークン
X_MICROCMS_API_KEY=アクセストークン
MICROCMS_URL=https://7qacxfbef6.microcms.io/api/v1/blog
LINKPREVIEW_API_KEY=APIキー
API_URL=https://tech-blog--tech-blog-1eb1a.asia-east1.hosted.app/

firebasetoolを使ってCLIで環境変数を登録

プロジェクト配下のターミナル上でfirebase apphosting:secrets:set 登録したい変数名を実行

$ firebase apphosting:secrets:set 登録したい変数名

Enter a value for ~で登録したい値の入力、Enter

image.png

その後、

image.png

? Is this secret for production or only local testing?

本番用ですか?テスト用ですか?の質問にProductionを選択

? To use this secret, your backend's service account must be granted access. Would you like to grant access now? (Y/n)

このシークレットを使うために、バックエンドのサービスアカウントにアクセス権限を付与する必要があります。今すぐ権限を付与しますか? の質問にYesを選択

image.png

Would you like to add this secret to apphosting.yml?の質問にYesを選択

.envで定義した環境変数名QIITA_API_TOKENを登録

公式ドキュメントによるとapphosting.ymlでAPIキーなどの設定をしてくださいとの記載がありました。

サードパーティの API キーや調整可能な設定など、ビルドプロセスに追加の構成が必要になることがあります。App Hosting では、apphosting.yaml で環境構成を提供し、プロジェクトのこのタイプのデータを保存および取得できます。

追加してくれます。

image.png

URLなどは自分で追加しました。

apphosting.yaml
  # Grant access to secrets in Cloud Secret Manager.
  # See https://firebase.google.com/docs/app-hosting/configure#secret-parameters
env:
  - variable: API_URL
    value: https://tech-blog--tech-blog-1eb1a.asia-east1.hosted.app/
  - variable: QIITA_API_URL
    value: https://qiita.com/api/v2/authenticated_user/items
  - variable: MICROCMS_URL
    value: https://7qacxfbef6.microcms.io/api/v1/blog
  - variable: QIITA_API_TOKEN
    secret: qiitaApiToken
  - variable: X_MICROCMS_API_KEY
    secret: microCmsApiKey
  - variable: LINKPREVIEW_API_KEY
    secret: linkPreviewApiKey

おわりに

答えはドキュメントにちゃんと書いてあるということを再認識しました。

参考

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?