Skip to content

Instantly share code, notes, and snippets.

@LeeDDHH
Last active September 12, 2021 12:01
Show Gist options
  • Save LeeDDHH/bfdc604cd6a12eaa1478c296087291fc to your computer and use it in GitHub Desktop.
Save LeeDDHH/bfdc604cd6a12eaa1478c296087291fc to your computer and use it in GitHub Desktop.
firebase-cliのあれこれを集める

Firebase CLI インストール

npm install -g firebase-tools


Firebase CLIでログインする

firebase login
firebase login:ci

Firebaseで管理しているプロジェクト一覧を表示する

firebase projects:list

Firebase プロジェクトを初期化する

初期化

firebase init

使う機能を複数選択する

? Which Firebase features do you want to set up for this directory? Press Space to select features, then Enter to confirm your choices. (Press <space> to select, <a> to toggle all, <i> to invert selection)
 ◯ Realtime Database: Configure a security rules file for Realtime Database and (optionally) provision default instance
 ◯ Firestore: Configure security rules and indexes files for Firestore
 ◯ Functions: Configure a Cloud Functions directory and its files
 ◯ Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys
 ◯ Hosting: Set up GitHub Action deploys
 ◯ Storage: Configure a security rules file for Cloud Storage
 ◯ Emulators: Set up local emulators for Firebase products
 ◯ Remote Config: Configure a template file for Remote Config

プロジェクトのセットアップ

=== Project Setup

First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now we'll just set up a default project.

? Please select an option:
  Use an existing project
  Create a new project
  Add Firebase to an existing Google Cloud Platform project
  Don't set up a default project

Firestore選択時

=== Firestore Setup

Firestore Security Rules allow you to define how and when to allow
requests. You can keep these rules in your project directory
and publish them with firebase deploy.

? What file should be used for Firestore Rules? firestore.rules

Firestore indexes allow you to perform complex queries while
maintaining performance that scales with the size of the result
set. You can keep index definitions in your project directory
and publish them with firebase deploy.

? What file should be used for Firestore indexes? firestore.indexes.json

Cloud Functions選択時

=== Functions Setup

A functions directory will be created in your project with sample code
pre-configured. Functions can be deployed with firebase deploy.

? What language would you like to use to write Cloud Functions? TypeScript
? Do you want to use ESLint to catch probable bugs and enforce style? Yes
✔  Wrote functions/package.json
✔  Wrote functions/.eslintrc.js
✔  Wrote functions/tsconfig.json
✔  Wrote functions/tsconfig.dev.json
✔  Wrote functions/src/index.ts
✔  Wrote functions/.gitignore
? Do you want to install dependencies with npm now? Yes

Firebase プロジェクトをローカルで実行する

  • ローカルでホストされるURLでFirebaseプロジェクトを表示してテストできる
  • 選択した機能のみをテストする場合
    • firebase serve --only テストする機能のフラグ
  • ローカルHTTP関数を使用してプロジェクトをエミュレートする場合
    • firebase serve --only functions

Firebase プロジェクトにデプロイする

  • Firebase CLIでデプロイの管理ができるFirebaseプロジェクト
    • Firebase Hosting サイトの新規リリース
    • 新規、更新済み、既存の Cloud Functions for Firebase
    • Firebase Realtime Database のルール
    • Cloud Storage for Firebase のルール
    • Cloud Firestore のルール
    • Cloud Firestore のインデックス
  • デプロイ用のコマンド
    • firebase deploy
  • コメントを追加してデプロイ
    • firebase deploy -m "コメント"

デプロイの割り当て

  • 可能性は低いけど、Firebaseのデプロイのレートやボリュームが制限された割り当てを超えることがある
    • 非常に多数の関数をデプロイすると、HTTP 429 Quota エラーが表示されることがある
  • 解決するためにやってみること
    • 部分デプロイを使用する
    • 特定のFirebaseサービスの割り当て量を
    • 1ユーザーの100秒あたりの書き込みリクエスト数という割り当てを適用する

特定の Firebase サービスをデプロイする

  • firebase deploy --only サービス名, [サービス名…]
  • サービス名前は、firebase.json構成ファイル内のキーに対応している

部分デプロイで利用できるサービスと機能

フラグの構文 デプロイされるサービスまたは機能
--only hosting Firebase Hosting のコンテンツ
--only database Firebase Realtime Database のルール
--only storage Cloud Storage for Firebase のルール
--only firestore Cloud Firestore のルールおよびインデックス
--only firestore:rules Cloud Firestore のルール
--only firestore:indexes Cloud Firestore のインデックス
--only functions Cloud Functions for Firebase(このフラグはより詳細な指定も可能です)

デプロイ前とデプロイ後にスクリプトで実行されるタスクを設定する

  • 設定は firebase.json に書く
    • predeploy デプロイ前のタスク
    • postdeploy デプロイ後のタスク
  • firebase deploy コマンドで、すべてのデプロイ前、後のタスクを実施する
  • デプロイ前、後のタスクともスクリプトの標準出力とエラー、ストリームをターミナルに出力する
    • 障害発生したら、以下の挙動をする
    • デプロイ前タスクで予期しない障害が起きたら、デプロイはキャンセルされる
    • なにかの理由でデプロイが失敗すると、デプロイ後タスクはトリガーされない

環境変数

  • デプロイ前、後タスクで使える環境変数
環境変数 変数内容
$GCLOUD_PROJECT アクティブプロジェクトのID
$PROJECT_DIR firebase.jsonファイルを含むルートディレクトリ
$RESOURCE_DIR (hosting、functionsのみ)デプロイするHosting、Cloud Functionsのリソースが格納されているディレクトリの場所

コマンド リファレンス

CLI 管理コマンド

コマンド 説明
help CLI、特定のコマンドに関するヘルプ情報を表示する
init Firebaseプロジェクトの初期化。firebase.json構成ファイルが現在のディレクトリに作成される
login Firebaseアカウントを使用してCLIの認証を行う
login:ci 非対話形式の環境で使用する認証トークンを生成する
logout FirebaseアカウントをCLIからログアウトする
open 関連するプロジェクトリソースをブラウザで開く
projects:list アクセス権のあるFirebaseプロジェクトの一覧を表示する
use CLIにアクティブなFirebaseプロジェクトを設定する

プロジェクト管理コマンド

コマンド 説明
projects:addfirebase Firebaseリソースを既存のGoogle Cloudプロジェクトに追加する
projects:create 新しいGoogle Cloudプロジェクトを作成し、そのプロジェクトにFirebase リソースを追加する

デプロイとローカルでの開発

コマンド 説明
deploy プロジェクトディレクトリからアクティブプロジェクトにコードとアセットをデプロイする
serve FirebaseHostingの構成を使用してローカルウェブサーバーを起動する

Authentication(ユーザー管理)コマンド

コマンド 説明
auth:export アクティブプロジェクトのユーザーアカウントをJSON、CSVファイルにエクスポートする
auth:import JSON、CSVファイルからアクティブなプロジェクトにユーザーアカウントをインポートする

Cloud Firestore コマンド

コマンド 説明
firestore:delete アクティブプロジェクトのデータベース内のドキュメントを削除する
CLIを使用すると、コレクション内のすべてのドキュメントを再帰的に削除できる

Cloud Functions for Firebase コマンド

コマンド 説明
functions:config:clone 別のプロジェクトの環境のクローンをアクティブなFirebaseプロジェクトに作成する
functions:config:get アクティブプロジェクトのCloud Functionsの既存の構成値を取得する
functions:config:set アクティブプロジェクトのCloud Functionsのランタイム構成値を保存する
functions:config:unset アクティブプロジェクトのランタイム構成から値を削除する
functions:log デプロイされたCloud Functions からログを読み取る

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment