今回はfirebaseのcloud functionsを使ってみたいなということで、ざっくりHelloWorldしてみました。その時の備忘録です。
環境
mac Catalina
参考にさせていただいた記事
https://qiita.com/azukiazusa/items/edd6ca9cba2d48c4c3e2
https://qiita.com/tdkn/items/2ed2b01f2656fc50da8c
nodebrewをインストール
まずは、nodeを入れる必要があるので、以下をターミナルで実行
(homebrewは入っていることが前提)
brew install nodebrew
インストールできるバージョンの確認
nodebrew ls-remote
インストール先のディレクトリ作成
mkdir -p ~/.nodebrew/src
インストール
Cloud FunctionsはNode.jsのversion10を使うので
10の最新を使うってことでいいのかな?
(あんまりわかってない。。知ってる方いらっしゃったら教えてくださいmm)
nodebrew install-binary v10.22.0
有効化
nodebrew use v10.22.0
パスを通す
export PATH="$HOME/.nodebrew/current/bin:$PATH" # <-追加
コンソールを再起動してバージョン確認
% node -v
v10.22.0
% npm -v
6.14.6
firebaseから、プロジェクトを作成する
https://console.firebase.google.com/
から、今回は、flutter-test-bd19fというプロジェクトIDで作成しました。
(flutter関係ないけど、、)
firebaseにログイン
funeasy@funeasy DevFunc % firebase login
i Firebase optionally collects CLI usage and error reporting information to help improve our products. Data is collected in accordance with Google's privacy policy (https://policies.google.com/privacy) and is not used to identify you.
? Allow Firebase to collect CLI usage and error reporting information? Yes
i To change your data collection preference at any time, run `firebase logout` and log in again.
Visit this URL on this device to log in:
https://accounts.google.com/o/oauth2/auth?xxx
Waiting for authentication...
✔ Success! Logged in as xxxxxx@gmail.com
新規function作成
functionsをおくディレクトリに移動して、以下を実行。
どのプロジェクトに対してのfunctionsか聞かれるので、先ほど作成した flutter-test-bd19f
を選択する。
funeasy@funeasy DevFunc % firebase init functions
######## #### ######## ######## ######## ### ###### ########
## ## ## ## ## ## ## ## ## ## ##
###### ## ######## ###### ######## ######### ###### ######
## ## ## ## ## ## ## ## ## ## ##
## #### ## ## ######## ######## ## ## ###### ########
You're about to initialize a Firebase project in this directory:
/Users/funeasy/Documents/DevFunc
=== 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: flutter-test-bd19f (flutter-test)
i Using project flutter-test-bd19f (flutter-test)
=== 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? JavaScript
? Do you want to use ESLint to catch probable bugs and enforce style? No
✔ Wrote functions/package.json
✔ Wrote functions/index.js
✔ Wrote functions/.gitignore
? Do you want to install dependencies with npm now? Yes
> protobufjs@6.10.1 postinstall /Users/funeasy/Documents/DevFunc/functions/node_modules/protobufjs
> node scripts/postinstall
npm notice created a lockfile as package-lock.json. You should commit this file.
added 253 packages from 205 contributors and audited 253 packages in 6.351s
29 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
i Writing configuration info to firebase.json...
i Writing project information to .firebaserc...
i Writing gitignore file to .gitignore...
✔ Firebase initialization complete!
funeasy@funeasy DevFunc % ls
firebase.json functions
funeasy@funeasy DevFunc %
node.jsのバージョンを確認
nodeは10で使うことを意味するコードが、最初から登録されていたので、大丈夫そう。
...
"engines": {
"node": "10"
},
...
index.jsを編集
ディレクトリ下に追加されたfunctions/index.js
を編集する。
const functions = require('firebase-functions');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
response.send("Hello from Firebase!");
});
ひとまず試したいので、helloWorldをコメントアウトしてみました。
ローカル環境でテスト
funeasy@funeasy DevFunc % firebase serve --only functions:helloWorld
✔ functions: Using node@10 from host.
i functions: Watching "/Users/funeasy/Documents/DevFunc/functions" for Cloud Functions...
⚠ It looks like you're trying to access functions.config().firebase but there is no value there. You can learn more about setting up config here: https://firebase.google.com/docs/functions/local-emulator
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
✔ functions[helloWorld]: http function initialized (http://localhost:5000/flutter-test-bd19f/us-central1/helloWorld).
(http://localhost:5000/flutter-test-bd19f/us-central1/helloWorld3).
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
i functions: Beginning execution of "helloWorld"
> {"structuredData":true,"severity":"INFO","message":"Hello logs!"}
i functions: Finished "helloWorld" in ~1s
ちなみにFireStoreなどproduction serviceにアクセスするようなコードは、ローカルでは実行できないみたい。
こういうのをテストするときは、みなさまデプロイしながらやってるんだろうか。。?
デプロイする
funeasy@funeasy DevFunc % firebase deploy --only functions
=== Deploying to 'flutter-test-bd19f'...
i deploying functions
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
⚠ functions: missing required API cloudbuild.googleapis.com. Enabling now...
✔ functions: required API cloudfunctions.googleapis.com is enabled
Error: Cloud Functions deployment requires the pay-as-you-go (Blaze) billing plan. To upgrade your project, visit the following URL:
https://console.firebase.google.com/project/flutter-test-bd19f/usage/details
For additional information about this requirement, see Firebase FAQs:
https://firebase.google.com/support/faq#functions-runtime
支払いがBlazeでないとダメと怒られた。。
Blazeにアップグレードして再度実行。
funeasy@funeasy DevFunc % firebase deploy --only functions
=== Deploying to 'flutter-test-bd19f'...
i deploying functions
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔ functions: required API cloudfunctions.googleapis.com is enabled
⚠ functions: missing required API cloudbuild.googleapis.com. Enabling now...
✔ functions: required API cloudbuild.googleapis.com is enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (27.45 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: creating Node.js 10 function helloWorld(us-central1)...
✔ functions[helloWorld(us-central1)]: Successful create operation.
Function URL (helloWorld): https://us-central1-flutter-test-bd19f.cloudfunctions.net/helloWorld
✔ Deploy complete!
Project Console: https://console.firebase.google.com/project/flutter-test-bd19f/overview
できた〜!
Function URLにアクセスすると、無事 Hello from Firebase!
の文字がでました〜!