Firebase configuration for multiple projects / environments

I use Cloud features for Firebase with three different projects for development, testing and production. Each project has a service-account.json. When I deploy the sources in the environment, the initialization is as follows:

var serviceAccount = require("./service-account-dev.json"); firebase.initializeApp({ credential: firebase.credential.cert(serviceAccount), databaseURL: "https://nwDEV.firebaseio.com" }); 

This is a bit difficult to handle, because I have to change the code every time to deploy it to a different environment. Is there a way to have a common configuration, for example. in firebase.json or.firebasesrc, which allows you to integrate a service account and decides to deploy, which configuration to choose?

Otherwise, it is possible to determine in which environment the code is executed and load a specific service-account.json and set the databaseURL property?

+5
source share
2 answers

You can use environment variables. https://firebase.google.com/docs/functions/config-env

  • Select a project (you can use a list of firebase commands to see them): firebase use my-project-development

  • Set the environment variable firebase functions:config:set app.environment="dev"

  • In your functions file, use a conditional expression to select the file: const serviceAccount = functions.config().app.environment === 'dev' ? 'credentials-dev.json' : 'credentials-prod.json'; const serviceAccount = functions.config().app.environment === 'dev' ? 'credentials-dev.json' : 'credentials-prod.json';

Then you can use the file depending on the project:

 firebase.initializeApp({ credential: firebase.credential.cert(serviceAccount), databaseURL: "https://nwDEV.firebaseio.com" }); 
+3
source

From what I understand about your question, what you are looking for comes down to a solution for translating the cloud functions that you deploy into the appropriate settings, that is, production, development and testing, which, I believe, means that everyone of these, a unique project and therefore a database in your Firebase environment.

If the above is true, then the following should help.

Cloud-based Firebase and CLI functions can more generally be deployed to a specific project in a Firebase environment. To do this, run the command in the terminal in the cloud functions directory.

  $ firebase use -add 

This will allow you to select your additional project (for example, development) and assign it an alias (I recommend "development" if it is such). Then, when deploying your functions, you can choose which project (and therefore the database) to deploy using an alias.

  $ firebase use default # sets environment to the default alias $ firebase use development # sets environment to the development alias 

For more information see: https://firebase.googleblog.com/2016/07/deploy-to-multiple-environments-with.html

One thing you might have to do for this is to use the default configuration settings for the cloud features.

  $ admin.initializeApp(functions.config().firebase); 
0
source

Source: https://habr.com/ru/post/1267221/


All Articles