多个项目/环境的 Firebase 配置 [英] Firebase configuration for multiple projects/environments
问题描述
我将 Cloud Functions for Firebase 与三个不同的项目一起用于开发、测试和生产目的.每个项目都有一个 service-account.json.当我将源部署到环境时,初始化如下所示:
I'm using Cloud Functions for Firebase with three different projects for development, testing and production purposes. Each project has a service-account.json. When I deploy the sources to an environment, the initialization looks like this:
var serviceAccount = require("./service-account-dev.json");
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
databaseURL: "https://nwDEV.firebaseio.com"
});
这有点难以处理,因为每次我想部署到不同的环境时都必须更改代码.有没有办法进行整体配置,例如在 firebase.json 或 .firebasesrc 中,允许集成服务帐户并决定部署选择哪种配置?
This is a bit difficult to handle, because I have to change the code everytime I want to deploy to a different environment. Is there a way to have an overall configuration, e.g. in firebase.json or.firebasesrc, which allows to integrate the service-account and decides on deployment which configuration to choose?
否则有没有可能检测到代码在哪个环境下运行并加载特定的service-account.json并设置databaseURL-property?
Otherwise is there a possibility to detect under which environment the code is running and to load the specific service-account.json and to set the databaseURL-property?
推荐答案
可以使用环境变量.https://firebase.google.com/docs/functions/config-env
选择项目(可以使用命令
firebase projects:list
查看):firebase 使用我的项目开发
设置环境变量firebase 函数:config:set app.environment=dev"
在您的函数文件中,应用条件来选择文件:const serviceAccount = functions.config().app.environment === 'dev' ?'credentials-dev.json' : 'credentials-prod.json';
In your functions file, apply a conditional to choose the file:
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"
});
这篇关于多个项目/环境的 Firebase 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!