Firebase Cloud Functions Deployment

If you are using PRO version, there is another folder named firebase-wordpress-functions. If you want to manage database, Firebase users and custom functions, you should deploy the functions together with the plugin. Make sure that you have Nodejs installed on your machine.

The cloud functions is only Admin Dashboard tasks, you don’t have to deploy the cloud functions for frontend interactions such as login, display or retrieve data.

If you want a hands-on guide, you can watch this tutorial: How to Deploy Cloud Functions

Prerequisite

In order to deploy cloud functions, you need to have Nodejs installed on your machine.

Then install firebase-tools packaged

npm install -g firebase-tools

SignIn and test firebase cli

firebase login

Since version 0.6.0, before deploying any functions, you should create two tokens for security purpose. One for Wordpress dashboard usage, the other is for Wordress frontend.

// Generate random token
node -e "console.log(require('crypto').randomBytes(20).toString('hex'))"

// Set your token to firebase configuration (dashboard token)
firebase functions:config:set api.dashboard_token=your-secret-key --project project-id

// Set your token to firebase configuration (frontend token)
firebase functions:config:set api.frontend_token=your-secret-key --project project-id

// Check your api token
firebase functions:config:get api --project project-id

Change Your Plan to Blaze

Why will I need a billing account to use the Node.js 10 runtime for Cloud Functions for Firebase?

Because of updates to its underlying architecture planned for August 17, 2020, Cloud Functions for Firebase will rely on some additional paid Google services: Cloud Build, Container Registry, and Cloud Storage. These architecture updates will apply for functions deployed to the Node.js 10 runtime. Usage of these services will be billed in addition to existing pricing.

../_images/pay-as-you-go.png

Firebase pricing plans

Change Cloud Functions Regions

Default functions will be deployed to us-central1. If you don’t want to change the regions, please skip this part.

In case you want to change the deploy regions to asia-east2, you can set a regions configuration for the firebase.

// Set deploy regions for `asia-east2`
firebase functions:config:set regions.0=asia-east2 --project project-id

// Set deploy regions for `asia-east2` and `us-central1`
firebase functions:config:set regions.0=asia-east2 regions.1=us-central1 --project project-id

After configuration, please check your environment to make sure that you have the region in the functions config.

firebase functions:config:get regions --project project-id

// The result should looks like this for two regions (asia-east2 & us-central1)
[
    "asia-east2",
    "us-central1"
]

Install Packages & Deploy Cloud Functions

Install packages and build functions. I’m using Yarn, you can use npm if you want.

cd functions/
yarn OR npm install

The code will go to functions folder, then installs packages with yarn / npm.

Start deploying firebase functions

cd functions
yarn deploy --project project-id
// OR
firebase deploy --only functions --project project-id

The deployment result should look like this

✔  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (103.29 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating Node.js 10 (Beta) function api-user(us-central1)...
i  functions: updating Node.js 10 (Beta) function api-database(us-central1)...
✔  functions[api-user(us-central1)]: Successful update operation.
✔  functions[api-database(us-central1)]: Successful update operation.

✔  Deploy complete!

Project Console: https://console.firebase.google.com/project/project-id/overview
✨  Done in 77.56s.

After that, you should update your Firebase setting with the dashboad token and frontend token and firebase functions url (e.g. https://us-central1-project-id.cloudfunctions.net)

../_images/firebase-setting.png

Firebase setting

Just to verify that everything works, you can find three cloud functions in your firebase console after the deployment.

../_images/firebase-cloud-functions.png

Firebase cloud functions