🐈

Firebase CLIコマンドメモ

2021/01/15に公開

Firebaseにログインする

$ firebase login

※ 明示的にログアウトしない限りログインし続ける点に注意

初期化を実行する

$ firebase init
captain-blue@MacBook-Pro react-study % firebase init

     ######## #### ########  ######## ########     ###     ######  ########
     ##        ##  ##     ## ##       ##     ##  ##   ##  ##       ##
     ######    ##  ########  ######   ########  #########  ######  ######
     ##        ##  ##    ##  ##       ##     ## ##     ##       ## ##
     ##       #### ##     ## ######## ########  ##     ##  ######  ########

You're about to initialize a Firebase project in this directory:

  /Users/captain-blue/GitHub/react-study

? Which Firebase CLI features do you want to set up for this folder? Press Space to select features, then Enter to confirm your choices. Fires
tore: Deploy rules and create indexes for Firestore, Functions: Configure and deploy Cloud Functions, Hosting: Configure and deploy Firebase H
osting sites

=== 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
? Select a default Firebase project for this directory: react-study-10047 (react-study)
i  Using project react-study-10047 (react-study)

=== 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
? File firestore.rules already exists. Do you want to overwrite it with the Firestore Rules from the Firebase Console? Yes

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
? File firestore.indexes.json already exists. Do you want to overwrite it with the Firestore Indexes from the Firebase Console? Yes

=== Functions Setup

A functions directory will be created in your project with a Node.js
package 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
? File functions/package.json already exists. Overwrite? Yes
✔  Wrote functions/package.json
? File functions/.eslintrc.js already exists. Overwrite? Yes
✔  Wrote functions/.eslintrc.js
? File functions/tsconfig.json already exists. Overwrite? Yes
✔  Wrote functions/tsconfig.json
? File functions/src/index.ts already exists. Overwrite? Yes
✔  Wrote functions/src/index.ts
? File functions/.gitignore already exists. Overwrite? Yes
✔  Wrote functions/.gitignore
? Do you want to install dependencies with npm now? No

=== Hosting Setup

Your public directory is the folder (relative to your project directory) that
will contain Hosting assets to be uploaded with firebase deploy. If you
have a build process for your assets, use your build's output directory.

? What do you want to use as your public directory? build
? Configure as a single-page app (rewrite all urls to /index.html)? Yes
? Set up automatic builds and deploys with GitHub? Yes
? File build/index.html already exists. Overwrite? Yes
✔  Wrote build/index.html

i  Detected a .git folder at /Users/captain-blue/GitHub/react-study
i  Authorizing with GitHub to upload your service account to a GitHub repository's secrets store.

Visit this URL on this device to log in:
https://github.com/login/oauth/authorize?client_id=89cf50f02ac6aaed3484&state=833435043&redirect_uri=http%3A%2F%2Flocalhost%3A9005&scope=read%3Auser%20repo%20public_repo

Waiting for authentication...

✔  Success! Logged into GitHub as captain-blue210

? For which GitHub repository would you like to set up a GitHub workflow? captain-blue210/react-study

✔  Created service account github-action-307723545 with Firebase Hosting admin permissions.
✔  Uploaded service account JSON to GitHub as secret FIREBASE_SERVICE_ACCOUNT_REACT_STUDY_10047.
i  You can manage your secrets at https://github.com/captain-blue210/react-study/settings/secrets.

? Set up the workflow to run a build script before every deploy? Yes
? What script should be run before every deploy? yarn ci && yarn deploy

✔  Created workflow file /Users/captain-blue/GitHub/react-study/.github/workflows/firebase-hosting-pull-request.yml
? Set up automatic deployment to your site's live channel when a PR is merged? Yes
? What is the name of the GitHub branch associated with your site's live channel? main

✔  Created workflow file /Users/captain-blue/GitHub/react-study/.github/workflows/firebase-hosting-merge.yml

i  Action required: Visit this URL to revoke authorization for the Firebase CLI GitHub OAuth App:
https://github.com/settings/connections/applications/89cf50f02ac6aaed3484
i  Action required: Push any new workflow file(s) to your repo

i  Writing configuration info to firebase.json...
i  Writing project information to .firebaserc...

✔  Firebase initialization complete!

ドキュメントの一括削除

$ firebase firestore:delete -r <コレクション名>

Cloud Functionsのみをデプロイする

$ firebase deploy --only functions

ローカルで関数を動かす

GOOGLE_APPLICATION_CREDENTIALS=./src/your-secret-key.json firebase serve --only functions

GOOGLE_APPLICATION_CREDENTIALS=./src/your-secret-key.json firebase functions:shell

firebase serveはHTTP関数のみ
Functions Shellの場合はHTTP関数以外も実行できる

Cloud Functionsで使う環境構成変数を設定する

$ firebase functions:config:set slack.secret=xxxxxxxxxxxxxxx

使うとき

import * as functions from "firebase-functions";

const secret = functions.config().slack.secret;

設定した環境変数を確認する

$ firebase functions:config:get

firestore.indexes.jsonに最新の情報を反映する

$ firebase firestore:indexes > firestore.indexes.json

インデックスをデプロイする

$ firebase deploy --only firestore:indexes

プロジェクトを確認する

$ firebase projects:list

プロジェクトエイリアスを確認する

$ firebase use

プロジェクトエイリアスを追加する

$ firebase use --add

Firebaseのエミュレータを起動する

$ firebase emulators:start

特定のエミュレータのみ起動する

$ firebase emulators:start --only firestore

$ firebase deploy --only hosting:stocky-blue210

Discussion