Firebase Cloud Functions Deployment

Requirements

  • Node.js v20 is required to deploy and use the cloud functions with the PRO plugin.

  • The cloud functions are now located in the functions folder under the integrate-firebase-PRO directory. There is no longer a folder named firebase-wordpress-functions.

If you are using the PRO version, the cloud functions are located in the functions folder under the integrate-firebase-PRO directory. If you want to manage the database, Firebase users, and custom functions, you should deploy the functions together with the plugin. Make sure that you have Node.js v20 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 v20 installed on your machine.

On Google Cloud Platform, go to your project and make sure Cloud Build API and Cloud Functions are enabled. And the account that you use to deploy has Service Account User and Cloud Functions Admin Role in order to deploy cloud functions to Firebase.

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 WordPress frontend.

// Generate two random tokens - save in notepad, you will need them later

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

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

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 18 runtime. Usage of these services will be billed in addition to existing pricing.

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

Firebase pricing plans

Install Packages & Deploy Cloud Functions

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

cd functions/
npm install

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

Start deploying firebase functions

firebase deploy --only functions --project project-id

During the deployment process, you will need to enter the information that created from previous step:

? Enter a string value for FRONTEND_TOKEN: YOUR_SECRET_TOKEN
? Enter a string value for DASHBOARD_TOKEN: YOUR_SECRET_TOKEN
? Enter a string value for REGIONS: us-central1
? Enter an integer value for USER_API_MININSTANCES: 0

After configuration, those information will be saved to an environment file in your folder:

i  functions: Created new local file .env.YOUR_PROJECT_ID to store param values. We suggest explicitly adding or excluding this file from version control.

If you want to change the region of the cloud function, you can update the environment file and redeploy the cloud functions.

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 dashboard 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

Troubleshooting

  • Make sure that your cloud run services has public access.

  • Make sure cloud run service account has Firebase Admin permission.