Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dev environment with FireStore & Cloud functions

I've been using FireStore together with Cloud functions extensively and so far everything has been great.

As my Swift app nears production launch, I would like to build a separate dev environment. Based on the documentation I was able to:

  • Create two separate projects
  • Link these two projects to my 2 targets using the various GoogleService p-list
  • I was also able to declare 2 service accounts for my Firebase Function environments and create two separate aliases.

It all seemed to work, except that my new Dev Cloud Function environment is not fully linked with my Dev FireStore DB but appears instead still linked to my initial FireStore DB instance - the cloud function is correctly triggered when I write something on it, but all the data retrieval function return empty (as if the .get() commands where targeting my "old" prod DB).

Could you let me know how I can "force" my Dev Cloud Functions environment to link with my Dev FireStore DB instance? I must have missed a step - for ex., I didn't re-initialize the CLI/ re-logged-in, etc. but only created a 2nd alias.

I am also getting this message: "Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions". It's weird as I am not using an external account (it's Firestore/ Cloud Functions/ FCM only).

Any documentation/ step-by-step guide you can point to would be super helpful!

Here is how I initialize my 2 apps:

const functions = require('firebase-functions');

const admin = require('firebase-admin');

var serviceAccount = require("./adminKey.json");
var serviceAccountDev = require("./adminKey-dev.json");

var secondEnvironment = admin.initializeApp({
  credential: admin.credential.cert(serviceAccountDev),
  databaseURL: "https://<app name>-dev.firebaseio.com"
}, 'dev instance');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<app name>.firebaseio.com"
});

var db = admin.firestore();

Thanks again, Francois

like image 218
Franc Avatar asked Feb 04 '26 06:02

Franc


1 Answers

Update based on the comments below and the update to your post with the part of the Cloud Function code where you initialize the App:

By doing the following:

var secondEnvironment = admin.initializeApp({
  credential: admin.credential.cert(serviceAccountDev),
  databaseURL: "https://<app name>-dev.firebaseio.com"
}, 'dev instance');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<app name>.firebaseio.com"
});

you are initializing the App with the Prod environment parameters for the two environments.

You should do as explained at the bottom of this documentation item: https://firebase.google.com/docs/functions/config-env#automatically_populated_environment_variables

which is:

If you need to initialize the Admin SDK with the default project configuration using service account credentials, you can load the credentials from a file and add them to FIREBASE_CONFIG like this:

serviceAccount = require('./serviceAccount.json');

const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG); adminConfig.credential = admin.credential.cert(serviceAccount); admin.initializeApp(adminConfig);

process.env.FIREBASE_CONFIG is an environment variable that is automatically populated in the function runtime.


(Initial answer, not fully relevant to your exact problem anymore...)

If I understand well, you just have to use the use command, in the CLI, to switch to the target Project for which you want to deploy your Cloud Function(s). This is in case the user you are logged in with is managing the two projects (dev and prod).

In case two different users manage the two projects, you have to use the logout command followed by a login command in order to login with the correct user (and possibly subsequently use the use command if the user manages several projects).

See the documentation here: https://firebase.google.com/docs/cli/#administrative_commands

like image 85
Renaud Tarnec Avatar answered Feb 06 '26 02:02

Renaud Tarnec