How to setup, test, and continuously deploy Firebase Cloud Function v1.0
To learn how to get started with Cloud Functions for Firebase by having a look at their Getting Started Guide, trying the quickstart samples and looking at the documentation.
-
Install Node.js v8.10.0 or greater
-
Install Node.js dependencies
This will install everything required to setup, test, and deploy this repository:
npm install
- Go to API Manager -> Credentials
- Click "New Credentials", and create a service account or click here
- Download the JSON for this service account, and set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to point to the file containing the JSON credentials.
Set the GOOGLE_APPLICATION_CREDENTIALS
environment variable:
Linux:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_file.json
Windows:
set GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account_file.json
Windows (PowerShell):
$env:GOOGLE_APPLICATION_CREDENTIALS="/path/to/service_account_file.json"
Read more about Google Cloud Platform Authentication.
Authenticate the Firebase CLI
npm run firebase:login
> [email protected] firebase:login /home/ubuntu/workspace/firebase-functions-unittest-cd
> npm run firebase -- login --no-localhost
> [email protected] firebase /home/ubuntu/workspace/firebase-functions-unittest-cd
> firebase "login" "--no-localhost"
? Allow Firebase to collect anonymous CLI usage and error reporting information? Yes
Visit this URL on any device to log in:
https://accounts.google.com/o/oauth2/auth?client_id=...
? Paste authorization code here: 4/my-auth-code
✔ Success! Logged in as [email protected]
npm run firebase:init:functions
> [email protected] firebase:init:funcs /home/ubuntu/workspace/firebase-functions-unittest-cd
> npm run firebase -- init functions
> [email protected] firebase /home/ubuntu/workspace/firebase-functions-unittest-cd
> firebase "init" "functions"
######## #### ######## ######## ######## ### ###### ########
## ## ## ## ## ## ## ## ## ## ##
###### ## ######## ###### ######## ######### ###### ######
## ## ## ## ## ## ## ## ## ## ##
## #### ## ## ######## ######## ## ## ###### ########
You're about to initialize a Firebase project in this directory:
/home/ubuntu/workspace/firebase-functions-unittest-cd
=== 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.
? Select a default Firebase project for this directory: (Use arrow keys)
[don't setup a default project]
❯ My Firebase Project (myproject-198914)
[create a new project]
=== 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
❯ TypeScript
? Do you want to use TSLint to catch probable bugs and enforce style? Yes
? File functions/package.json already exists. Overwrite? No
i Skipping write of functions/package.json
? File functions/tslint.json already exists. Overwrite? No
? File functions/tsconfig.json already exists. Overwrite? No
i Skipping write of functions/tslint.json
i Skipping write of functions/tsconfig.json
? File functions/src/index.ts already exists. Overwrite? No
i Skipping write of functions/src/index.ts
? Do you want to install dependencies with npm now? Yes
npm notice created a lockfile as package-lock.json. You should commit this file.
added 570 packages from 469 contributors in 29.528s
i Writing configuration info to firebase.json...
i Writing project information to .firebaserc...
✔ Firebase initialization complete!